当前位置:   article > 正文

Recyclerview进阶——元素拖拽及侧滑删除_recyclerview拖拽

recyclerview拖拽

之前一直觉得列表的拖拽换位和侧滑删除是很难实现的,不过之后参考了下几篇博客后发,发现还是很简单的,因为suppor早就封装好了,最主要的是继承ItemTouchHelper.Callback,并对其方法

 

效果图如下:

步骤如下:

1、新建类并继承ItemTouchHelper.Callback,在其对应方法设置相应操作包括,不同布局的拖拽和侧滑删除,选择效果

2、Adapter中实现自定义的监听回调,对位置交换和删除动作做对应的数据操作和界面的刷新

3、主代码中 新建ItemTouchHelper 并关联RecyclerView即可

代码如下:

1、新建类并继承ItemTouchHelper.Callback,在其对应方法设置相应操作

  1. public interface ItemTouchHelperAdapter {
  2. //数据交换
  3. void onItemMove(int fromPosition,int toPosition);
  4. //数据删除
  5. void onSwiped(int position);
  6. }
  1. public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
  2. private ItemTouchHelperAdapter mAdapter;
  3. private boolean dragEnabled; //能否拖拽
  4. private boolean swipeEnabled; //能否侧滑
  5. public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter, boolean dragEnabled, boolean swipeEnabled) {
  6. mAdapter = adapter;
  7. this.dragEnabled = dragEnabled;
  8. this.swipeEnabled = swipeEnabled;
  9. }
  10. //同来设置 拖拽移动,或移动删除
  11. @Override
  12. public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  13. RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  14. if (layoutManager instanceof GridLayoutManager) {// GridLayoutManager
  15. // flag如果值是0,相当于这个功能被关闭
  16. int dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN;
  17. int swipeFlag = 0;
  18. // create make
  19. return makeMovementFlags(dragFlag, swipeFlag);
  20. } else if (layoutManager instanceof LinearLayoutManager) {// linearLayoutManager
  21. LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
  22. int orientation = linearLayoutManager.getOrientation();
  23. int dragFlag = 0;
  24. int swipeFlag = 0;
  25. // 为了方便理解,相当于分为横着的ListView和竖着的ListView
  26. if (orientation == LinearLayoutManager.HORIZONTAL) {// 如果是横向的布局
  27. swipeFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
  28. dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  29. } else if (orientation == LinearLayoutManager.VERTICAL) {// 如果是竖向的布局,相当于ListView
  30. dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
  31. swipeFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  32. }
  33. return makeMovementFlags(dragFlag, swipeFlag);
  34. }
  35. return 0;
  36. }
  37. //是否能拖动
  38. @Override
  39. public boolean isLongPressDragEnabled() {
  40. return dragEnabled;
  41. }
  42. //是否能侧滑
  43. @Override
  44. public boolean isItemViewSwipeEnabled() {
  45. return swipeEnabled;
  46. }
  47. //拖动
  48. @Override
  49. public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
  50. Log.e(TAG, "onMove");
  51. // if (viewHolder.getAdapterPosition() == 0) {
  52. //第一个无法拖拽
  53. // return false;
  54. // }
  55. mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
  56. return true;
  57. }
  58. //左右滑动
  59. @Override
  60. public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
  61. mAdapter.onSwiped(viewHolder.getAdapterPosition());
  62. Log.e(TAG, "onSwiped");
  63. }
  64. //选中项操作
  65. @Override
  66. public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
  67. super.onSelectedChanged(viewHolder, actionState);
  68. Log.e(TAG, "onSelectedChanged");
  69. if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
  70. if (viewHolder != null)
  71. viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
  72. }
  73. }
  74. //释放选中项操作
  75. @Override
  76. public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
  77. super.clearView(recyclerView, viewHolder);
  78. Log.e(TAG, "clearView");
  79. viewHolder.itemView.setBackgroundColor(0);
  80. }
  81. }

2、Adapter中实现自定义的监听回调

  1. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> implements ItemTouchHelperAdapter {
  2. private Context mContext;
  3. private List<String> mList;
  4. public MyAdapter(Context context, List<String> list) {
  5. mContext = context;
  6. mList = list;
  7. }
  8. @NonNull
  9. @Override
  10. public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
  11. return new Holder(LayoutInflater.from(mContext).inflate(R.layout.item_test, viewGroup, false));
  12. }
  13. @Override
  14. public void onBindViewHolder(@NonNull final Holder holder, final int i) {
  15. holder.tvF.setText(mList.get(i));
  16. holder.tvS.setText(mList.get(i));
  17. holder.delete.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. //移除数据
  21. mList.remove(i);
  22. notifyDataSetChanged();
  23. }
  24. });
  25. }
  26. @Override
  27. public int getItemCount() {
  28. return mList.size();
  29. }
  30. @Override
  31. public void onItemMove(int fromPosition, int toPosition) {
  32. //交换位置
  33. Collections.swap(mList, fromPosition, toPosition);
  34. notifyItemMoved(fromPosition, toPosition);
  35. }
  36. @Override
  37. public void onSwiped(int position) {
  38. //移除数据
  39. mList.remove(position);
  40. notifyItemRemoved(position);
  41. }
  42. static class Holder extends RecyclerView.ViewHolder {
  43. private TextView tvF;
  44. private TextView tvS;
  45. private ImageView delete;
  46. public Holder(@NonNull View itemView) {
  47. super(itemView);
  48. tvF = itemView.findViewById(R.id.tv_first);
  49. tvS = itemView.findViewById(R.id.tv_second);
  50. delete = itemView.findViewById(R.id.delete);
  51. }
  52. }
  53. }

3、主代码中 新建ItemTouchHelper 并关联RecyclerView

  1. public class MainActivity extends AppCompatActivity {
  2. public static final String TAG = "recyclerviewmove";
  3. private Button btnChange;
  4. private RecyclerView mRecyclerView;
  5. private MyAdapter mMyAdapter;
  6. private List<String> mList = new ArrayList<>();
  7. private boolean bGrid; //布局标识
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. for (int i = 0; i < 9; i++) {
  13. mList.add(i + "" + i + "" + i);
  14. }
  15. mRecyclerView = findViewById(R.id.recycler_view);
  16. btnChange = findViewById(R.id.btn_change);
  17. mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
  18. mMyAdapter = new MyAdapter(this, mList);
  19. mRecyclerView.setAdapter(mMyAdapter);
  20. //关联ItemTouchHelper和RecyclerView
  21. ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(mMyAdapter,true,true);
  22. ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
  23. touchHelper.attachToRecyclerView(mRecyclerView);
  24. btnChange.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. if (bGrid) {
  28. mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
  29. } else {
  30. mRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this, 4));
  31. }
  32. bGrid = !bGrid;
  33. }
  34. });
  35. }
  36. }

参考:RecyclerView进阶:使用ItemTouchHelper实现拖拽和侧滑删除_recyclerview把item可以拖拽到任意地方具体实现_程序员的自我反思的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/286713
推荐阅读
相关标签
  

闽ICP备14008679号