当前位置:   article > 正文

RecyclerView实现item拖拽效果_recyclerview 拖动到另外一个recyclerview插入

recyclerview 拖动到另外一个recyclerview插入

使用android.support.v7.widget.helper 包下的ItemTouchHelper工具类
来处理RecyclerView的拖拽

 

1.创建ItemTouchHelper类
2.调用mItemTouchHelper.attachToRecyclerView(recyclerview)关联recyclerview

  1. recyclerview.setLayoutManager(gridLayoutManager);
  2. adapter = new DragAdapter(this,datas);
  3. recyclerview.setAdapter(adapter);
  4. mItemTouchHelper = new ItemTouchHelper(new DragItemHelperCallBack());
  5. mItemTouchHelper.attachToRecyclerView(recyclerview);

ItemTouchHelper的构造需要传入一个CallBack回调

  1. public class DragItemHelperCallBack extends ItemTouchHelper.Callback {
  2. @Override
  3. public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  4. int dragFlags = 0;
  5. if(recyclerView.getLayoutManager() instanceof GridLayoutManager){
  6. dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
  7. ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  8. }else {
  9. dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
  10. }
  11. // 支持左右滑动(删除)操作, swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END
  12. final int swipeFlags = 0;
  13. return makeMovementFlags(dragFlags,swipeFlags);
  14. }
  15. @Override
  16. public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
  17. //被按下拖拽时候的position
  18. int fromPosition = viewHolder.getAdapterPosition();
  19. //当前拖拽到的item的posiiton
  20. int toPosition = target.getAdapterPosition();
  21. //回调到adapter 当中处理移动过程中,数据变更的逻辑,以及更新UI
  22. if (recyclerView.getAdapter() instanceof IOnItemMoveListener) {
  23. IOnItemMoveListener listener = ((IOnItemMoveListener) recyclerView.getAdapter());
  24. listener.onItemMove(fromPosition, toPosition);
  25. }
  26. return true;
  27. }
  28. @Override
  29. public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
  30. }
  31. @Override
  32. public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
  33. // 不在闲置状态
  34. if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
  35. if (viewHolder instanceof IOnDragVHListener) {
  36. IOnDragVHListener itemViewHolder = (IOnDragVHListener) viewHolder;
  37. itemViewHolder.onItemSelected();
  38. }
  39. }
  40. super.onSelectedChanged(viewHolder, actionState);
  41. }
  42. @Override
  43. public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  44. if(viewHolder instanceof IOnDragVHListener){
  45. IOnDragVHListener itemViewHolder = (IOnDragVHListener) viewHolder;
  46. itemViewHolder.onItemFinish();
  47. }
  48. super.clearView(recyclerView, viewHolder);
  49. }
  50. }

拖拽中会不断调用onMove方法,通过viewHolder和target获取到被按下拖拽时候的position和当前拖拽到的item的posiiton,然后接口回调 listener.onItemMove到recyclerView的Adapter当中处理数据变化的逻辑

Adapter当中的处理

  1. @Override
  2. public void onItemMove(int fromPosition, int toPosition) {
  3. /**
  4. * 拖拽过程中的数据处理
  5. * 1.先记录原来位置的数据
  6. * 2.删除原来位置的数据
  7. * 3.在拖拽到的位置插入原来的数据
  8. */
  9. String fromString = dataList.get(fromPosition);
  10. dataList.remove(fromPosition);
  11. dataList.add(toPosition,fromString);
  12. //通知数据发生移动
  13. notifyItemMoved(fromPosition, toPosition);
  14. }

onSelectedChanged方法和clearView方法是当发生拖拽的时候和拖拽完成的时候调用,可以在这里给我们对应的布局设置不同的颜色显示

 

  1. //拖拽过程中调用
  2. @Override
  3. public void onItemSelected() {
  4. tv_content.setBackgroundResource(R.color.textColorPrimary);
  5. }
  6. //拖拽完成调用
  7. @Override
  8. public void onItemFinish() {
  9. tv_content.setBackgroundResource(R.color.viewBackground);
  10. }



 

 

 

 

 

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

闽ICP备14008679号