Android recyclerview实现拖拽排序和侧滑删除
作者:天安门前扫地僧
Recyclerview现在基本已经替代Listview了,RecyclerView也越来越好用了 当我们有实现条目的拖拽排序和侧滑删除时 可以直接时候Recyclerview提供的API就可以直接实现了
先贴上主要代码
private void initveiw() { ArrayList<String> items = new ArrayList<>(Arrays.asList("itme1", "item2", "itme3", "item4", "item5", "item6", "item7", "item8", "itme9", "item10", "itme11", "item12", "item13", "item14", "item15", "item16")); recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); SimpleAdapter adapter = new SimpleAdapter(items); recyclerView.setAdapter(adapter); ItemTouchHelper helper = new ItemTouchHelper(new MyItemTouchCallback(adapter)); helper.attachToRecyclerView(recyclerView); } public class MyItemTouchCallback extends ItemTouchHelper.Callback{ private SimpleAdapter adapter; public MyItemTouchCallback(SimpleAdapter adapter) { this.adapter = adapter; } @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { int dragFlag; int swipeFlag; RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); if (manager instanceof GridLayoutManager){ dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; swipeFlag = 0; }else{ dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP; swipeFlag = ItemTouchHelper.END | ItemTouchHelper.START; } return makeMovementFlags(dragFlag,swipeFlag); } @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { int fromPosition = viewHolder.getAdapterPosition(); int toPosition = target.getAdapterPosition(); if (fromPosition < toPosition ){ for (int i = fromPosition ;i<toPosition ;i++){ Collections.swap(adapter.getDataList(),i,i+1); } }else{ for (int i= fromPosition; i>toPosition; i--){ Collections.swap(adapter.getDataList(),i ,i-1); } } recyclerView.getAdapter().notifyItemMoved(fromPosition,toPosition); return true; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { int position = viewHolder.getAdapterPosition(); if (direction == ItemTouchHelper.END | direction==ItemTouchHelper.START){ adapter.getDataList().remove(position); adapter.notifyItemRemoved(position); } } @Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { super.onSelectedChanged(viewHolder, actionState); if (actionState==ItemTouchHelper.ACTION_STATE_DRAG){ viewHolder.itemView.setBackgroundColor(Color.BLUE); } } @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); viewHolder.itemView.setBackgroundColor(0); } }
步骤:
创建 ItemTouchHelper 对象时候,需要我们传入一个实现了 ItemTouchHelper.Callback 接口的对象。而排序和删除的逻辑都封装在了这个 ItemTouchHelper.Callback 的对象里面了。
private void initveiw() { ArrayList<String> items = new ArrayList<>(Arrays.asList("itme1", "item2", "itme3", "item4", "item5", "item6", "item7", "item8", "itme9", "item10", "itme11", "item12", "item13", "item14", "item15", "item16")); recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); SimpleAdapter adapter = new SimpleAdapter(items); recyclerView.setAdapter(adapter); ItemTouchHelper helper = new ItemTouchHelper(new MyItemTouchCallback(adapter)); helper.attachToRecyclerView(recyclerView); }
实现ItemTouchHelper.Callback 接口后有三个方法需要重写:
getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) :设置滑动类型的标记。需要设置两种类型的 flag ,即 dragFlags 和 swipeFlags ,分别代表着拖拽标记和滑动标记。最后需要调用 makeMovementFlags(dragFlags,
swipeFlags)方法来合成返回。
onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) :当用户拖拽列表某个 item 时会回调。很明显,拖拽排序的代码应该在这个方法中实现。
onSwiped(RecyclerView.ViewHolder viewHolder, int direction) :当用户滑动列表某个 item 时会回调。所以侧滑删除的代码应该在这个方法中实现。
下面是重写的几个方法:
第一个,getMovementFlags方法
@Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { int dragFlag; int swipeFlag; RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); if (manager instanceof GridLayoutManager){ dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; swipeFlag = 0; }else{ dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP; swipeFlag = ItemTouchHelper.END | ItemTouchHelper.START; } return makeMovementFlags(dragFlag,swipeFlag); }
这个方法里面根据LayoutManager分了两种情况,根据自己的情况去分
在GridLayoutManager中只能上下左右拖拽但是不能侧滑删除,所以swipFlag = 0;swipeFlag的值ItemTouchHelper.END是右滑删除,ItemTouchHelper.START是左滑删除
最后调用makeMovementFlags方法合成返回
第二个,onMove方法
@Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { int fromPosition = viewHolder.getAdapterPosition(); int toPosition = target.getAdapterPosition(); if (fromPosition < toPosition ){ for (int i = fromPosition ;i<toPosition ;i++){ Collections.swap(adapter.getDataList(),i,i+1); } }else{ for (int i= fromPosition; i>toPosition; i--){ Collections.swap(adapter.getDataList(),i ,i-1); } } recyclerView.getAdapter().notifyItemMoved(fromPosition,toPosition); return true; }
这个方法是用户在拖拽 item 的时候调用。所以关于列表排序的代码应该写在这里。方法参数中的 viewHolder 代表的是用户当前拖拽的 item ,而 target 代表的是被用户拖拽所覆盖的那个 item 。所以在 onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) 方法中的逻辑就是把 fromPosition 至 toPosition 为止改变它们的位置。
第三个,onSwiped方法
@Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { int position = viewHolder.getAdapterPosition(); if (direction == ItemTouchHelper.END | direction==ItemTouchHelper.START){ adapter.getDataList().remove(position); adapter.notifyItemRemoved(position); } }
这个方法是在用户侧滑的时候调用的 ,在里面对adapter的数据进行删除就可以了
最后两个方法
@Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { super.onSelectedChanged(viewHolder, actionState); if (actionState==ItemTouchHelper.ACTION_STATE_DRAG){ viewHolder.itemView.setBackgroundColor(Color.BLUE); } } @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); viewHolder.itemView.setBackgroundColor(0); } }
就是优化的方法,第一个是选择条目改变状态,第二个是手指抬起之后恢复条目状态
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。