自己写一个
public class SimpleItemTouchCallBack extends ItemTouchHelper.Callback {
private TouchCallBack mCallBack;
//左滑删除的
private boolean mSwipeEnable = true;
public SimpleItemTouchCallBack(TouchCallBack mCallBack) {
this.mCallBack = mCallBack;
}
/**
* 返回可以滑动的方向,一般使用makeMovementFlags(int,int)
* 或makeFlag(int, int)来构造我们的返回值
*
* @param recyclerView
* @param viewHolder
* @return
*/
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
//允许上下拖拽
int drag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
//允许向左滑动
int swipe = ItemTouchHelper.LEFT;
//设置
return makeMovementFlags(drag, swipe);
}
/**
* 上下拖动item时回调,可以调用Adapter的notifyItemMoved方法来交换两个ViewHolder的位置,
* 最后返回true,
* 表示被拖动的ViewHolder已经移动到了目的位置
*
* @param recyclerView
* @param viewHolder
* @param target
* @return
*/
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
//通知适配器,两个子条目位置发生改变
mCallBack.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
/**
* 当用户左右滑动item时达到删除条件就会调用,一般为一半,条目继续滑动删除,否则弹回
*
* @param viewHolder
* @param direction
*/
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mCallBack.onItemDelete(viewHolder.getAdapterPosition());
}
/**
* 支持长按拖动,默认是true
*
* @return
*/
@Override
public boolean isLongPressDragEnabled() {
return super.isLongPressDragEnabled();
}
/**
* 支持滑动,即可以调用到onSwiped()方法,默认是true
*
* @return
*/
@Override
public boolean isItemViewSwipeEnabled() {
return mSwipeEnable;
}
/**
* 设置是否支持左滑删除
*
* @param enable
*/
public void setmSwipeEnable(boolean enable) {
this.mSwipeEnable = enable;
}
}
自己定义一个接口
public interface TouchCallBack {
//数据交换
void onItemMove(int fromPosition, int toPosition);
//数据删除
void onItemDelete(int position);
}
适配器,实现TouchCallBack
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>implements TouchCallBack {
private Context context;
private ArrayList<String> list;
public MyAdapter(Context context, ArrayList<String> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_item, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
String str = list.get(i);
viewHolder.tv.setText(str);
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onItemMove(int fromPosition, int toPosition) {
//交换位置
Collections.swap(list, fromPosition, toPosition);
//局部刷新(移动)
notifyItemMoved(fromPosition, toPosition);
}
@Override
public void onItemDelete(int position) {
//删除数据
list.remove(position);
//局部刷新(移除)
notifyItemRemoved(position);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
}
}
}
MainActivity里
public class MainActivity extends AppCompatActivity {
private RecyclerView mLv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mLv = (RecyclerView) findViewById(R.id.lv);
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 8; i++) {
list.add("安琪拉"+i);
}
MyAdapter adapter = new MyAdapter(this, list);
mLv.setAdapter(adapter);
mLv.setLayoutManager(new LinearLayoutManager(this));
// 拖拽移动和左滑删除
SimpleItemTouchCallBack simpleItemTouchCallBack = new SimpleItemTouchCallBack(adapter);
// 要实现侧滑删除条目,把 false 改成 true 就可以了
simpleItemTouchCallBack.setmSwipeEnable(false);
ItemTouchHelper helper = new ItemTouchHelper(simpleItemTouchCallBack);
helper.attachToRecyclerView(mLv);
}
}