当前位置:   article > 正文

RecyclerView实现拖动调整Item位置,侧滑删除Item功能_recycleview.setonitemmovelistener

recycleview.setonitemmovelistener

实现长按条目调整位置和侧滑删除功能,核心是ItemTouchHelper类,直接贴代码了。

DefaultItemTouchHelper
  1. public class DefaultItemTouchHelper extends ItemTouchHelper {
  2. private DefaultItemTouchHelperCallback mDefaultItemTouchHelperCallback;
  3. private OnItemMoveListenter itemMoveListenter;
  4. public DefaultItemTouchHelper() {
  5. this(new DefaultItemTouchHelperCallback());
  6. }
  7. private DefaultItemTouchHelper(DefaultItemTouchHelperCallback callback) {
  8. super(callback);
  9. this.mDefaultItemTouchHelperCallback = callback;
  10. }
  11. public void setItemMoveListenter(OnItemMoveListenter itemMoveListenter) {
  12. mDefaultItemTouchHelperCallback.setCallback(itemMoveListenter);
  13. }
  14. /**
  15. * 是否可以滑动删除
  16. * @param b
  17. */
  18. public void setItemViewSwipeEnabled(boolean b) {
  19. mDefaultItemTouchHelperCallback.setItemViewSwipeEnabled(b);
  20. }
  21. /**
  22. *是否可以长按
  23. */
  24. public void setLongPressDragEnabled(boolean b) {
  25. mDefaultItemTouchHelperCallback.setLongPressDragEnabled(b);
  26. }
  27. }
DefaultItemTouchHelperCallback
  1. public class DefaultItemTouchHelperCallback extends ItemTouchHelper.Callback {
  2. private OnItemMoveListenter callback;
  3. private boolean isLongPressDragEnabled;
  4. private boolean isItemViewSwipeEnabled;
  5. public void setCallback(OnItemMoveListenter callback) {
  6. this.callback = callback;
  7. }
  8. /**
  9. * 返回一个复合标志,该标志定义每个状态下启用的移动方向
  10. * @param recyclerView
  11. * @param viewHolder
  12. * @return
  13. */
  14. @Override
  15. public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
  16. RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  17. if (layoutManager instanceof GridLayoutManager) {
  18. LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
  19. if (linearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
  20. int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  21. int swipeFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
  22. return makeMovementFlags(dragFlags, swipeFlags);
  23. } else {
  24. int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  25. int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  26. return makeMovementFlags(dragFlags, swipeFlags);
  27. }
  28. } else if (layoutManager instanceof LinearLayoutManager) {
  29. LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
  30. if (linearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
  31. int dragFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  32. int swipeFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
  33. return makeMovementFlags(dragFlags, swipeFlags);
  34. } else {
  35. int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
  36. int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  37. return makeMovementFlags(dragFlags, swipeFlags);
  38. }
  39. }
  40. return makeMovementFlags(0,0);
  41. }
  42. /**
  43. * 当ItemTouchHelper希望将拖动的项从原来的位置移动到新位置
  44. * @param recyclerView
  45. * @param viewHolder
  46. * @param target
  47. * @return
  48. */
  49. @Override
  50. public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
  51. if (callback!=null){
  52. return callback.onItemMove(viewHolder,target);
  53. }
  54. return false;
  55. }
  56. /**
  57. * 当用户滑动ViewHolder时调用
  58. * @param viewHolder
  59. * @param direction
  60. */
  61. @Override
  62. public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
  63. if (callback!=null){
  64. callback.onItemSwipe(viewHolder,direction);
  65. }
  66. }
  67. /**
  68. * 拖动过程中状态变化的监听
  69. * ItemTouchHelper.ACTION_STATE_IDLE--闲置状态
  70. * ItemTouchHelper.ACTION_STATE_SWIPE--刷新状态
  71. * ItemTouchHelper.ACTION_STATE_DRAG--拖动状态
  72. * @param viewHolder
  73. * @param actionState
  74. */
  75. @Override
  76. public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
  77. super.onSelectedChanged(viewHolder, actionState);
  78. if (callback!=null && actionState != ItemTouchHelper.ACTION_STATE_IDLE){
  79. callback.onSelectedChanged(viewHolder,actionState);
  80. }
  81. }
  82. /**
  83. * 完成交互后,调用
  84. * @param recyclerView
  85. * @param viewHolder
  86. */
  87. @Override
  88. public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
  89. super.clearView(recyclerView, viewHolder);
  90. if (callback!=null ){
  91. callback.onSelectedChanged(viewHolder,ItemTouchHelper.ACTION_STATE_IDLE);
  92. }
  93. }
  94. public void setLongPressDragEnabled(boolean canDrag) {
  95. this.isLongPressDragEnabled = canDrag;
  96. }
  97. @Override
  98. public boolean isLongPressDragEnabled() {
  99. return isLongPressDragEnabled;
  100. }
  101. public void setItemViewSwipeEnabled(boolean canSwipe) {
  102. this.isItemViewSwipeEnabled = canSwipe;
  103. }
  104. @Override
  105. public boolean isItemViewSwipeEnabled() {
  106. return isItemViewSwipeEnabled;
  107. }
  108. }
OnItemMoveListenter
  1. public interface OnItemMoveListenter {
  2. boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder);
  3. void onItemSwipe(RecyclerView.ViewHolder srcHolder, int direction);
  4. void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState);
  5. }
SwipeRecycleViewActivity
  1. public class SwipeRecycleViewActivity extends AppCompatActivity {
  2. private SwipeRecyclerView mRecyclerView;
  3. private RecycleItemAdapter mAdapter;
  4. private ArrayList<String> strings;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_swipe_recycle_view);
  9. mRecyclerView = findViewById(R.id.recycle_view);
  10. mRecyclerView.setLayoutManager(createLayoutManager());
  11. mRecyclerView.addItemDecoration(createItemDecoration());
  12. // 长按拖拽,默认关闭
  13. mRecyclerView.setLongPressDragEnabled(true);
  14. // 滑动删除,默认关闭
  15. mRecyclerView.setItemViewSwipeEnabled(true);
  16. mRecyclerView.setOnItemMoveListener(new OnItemMoveListenter() {
  17. @Override
  18. public boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) {
  19. if (srcHolder.getItemViewType() != targetHolder.getItemViewType()) {
  20. return false;
  21. }
  22. int fromPosition = srcHolder.getAdapterPosition();
  23. int toPosition = targetHolder.getAdapterPosition();
  24. Collections.swap(strings, fromPosition, toPosition);
  25. mAdapter.notifyItemMoved(fromPosition, toPosition);
  26. // 返回true表示处理了并可以换位置,返回false表示你没有处理并不能换位置
  27. return true;
  28. }
  29. @Override
  30. public void onItemSwipe(RecyclerView.ViewHolder srcHolder, int direction) {
  31. //左滑、右滑 删除,如果要一个方向删除,getMovementFlags方法中处理
  32. int adapterPosition = srcHolder.getAdapterPosition();
  33. mAdapter.notifyItemRemoved(adapterPosition);
  34. }
  35. @Override
  36. public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
  37. if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
  38. // 拖拽的时候添加一个背景
  39. viewHolder.itemView.setBackgroundColor(ContextCompat.getColor(SwipeRecycleViewActivity.this, R.color.white_pressed));
  40. } else if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
  41. } else if (actionState == ItemTouchHelper.ACTION_STATE_IDLE) {
  42. // 在手松开的时候还原背景
  43. ViewCompat.setBackground(viewHolder.itemView, ContextCompat.getDrawable(SwipeRecycleViewActivity.this, R.color.white));
  44. }
  45. }
  46. });
  47. makeDataList();
  48. mAdapter = new RecycleItemAdapter(this,strings);
  49. mRecyclerView.setAdapter(mAdapter);
  50. }
  51. private ArrayList<String> makeDataList(){
  52. strings = new ArrayList<>();
  53. for (int i=0;i<10;i++){
  54. strings.add("条目-"+i);
  55. }
  56. return strings;
  57. }
  58. /**
  59. * 布局管理类
  60. *
  61. * @return
  62. */
  63. protected RecyclerView.LayoutManager createLayoutManager() {
  64. return new LinearLayoutManager(this);
  65. }
  66. /**
  67. * 条目效果类
  68. * @return
  69. */
  70. protected RecyclerView.ItemDecoration createItemDecoration() {
  71. return new SimplePaddingDecoration(this);
  72. }
  73. /**
  74. * 添加条目的间隔效果
  75. */
  76. public class SimplePaddingDecoration extends RecyclerView.ItemDecoration {
  77. private int dividerHeight;
  78. public SimplePaddingDecoration(Context context) {
  79. dividerHeight = 5;
  80. }
  81. @Override
  82. public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  83. super.getItemOffsets(outRect, view, parent, state);
  84. //类似加了一个bottom padding
  85. outRect.bottom = dividerHeight;
  86. }
  87. }
  88. }
RecyclerItemAdapter
  1. public class RecyclerItemAdapter extends RecyclerView.Adapter<RecyclerItemAdapter.ViewHolder> {
  2. private ArrayList<String> mData;
  3. private Context mContext;
  4. public RecyclerItemAdapter(Context mContext, ArrayList<String> mData) {
  5. this.mData = mData;
  6. this.mContext = mContext;
  7. }
  8. @NonNull
  9. @Override
  10. public RecyclerItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  11. ViewHolder viewHolder = new ViewHolder(View.inflate(mContext, R.layout.item_drag_touch, null));
  12. return viewHolder;
  13. }
  14. @Override
  15. public void onBindViewHolder(@NonNull RecyclerItemAdapter.ViewHolder holder, int position) {
  16. holder.tvTitle.setText(mData.get(position));
  17. }
  18. @Override
  19. public int getItemCount() {
  20. return mData.size();
  21. }
  22. static class ViewHolder extends RecyclerView.ViewHolder{
  23. private TextView tvTitle;
  24. public ViewHolder(@NonNull View itemView) {
  25. super(itemView);
  26. tvTitle=itemView.findViewById(R.id.tv_title);
  27. }
  28. }
  29. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".activity.RecycleViewDragItemActivity">
  8. <androidx.recyclerview.widget.RecyclerView
  9. android:id="@+id/recycle_view"
  10. android:background="@color/white"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"/>
  13. </LinearLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:layout_marginTop="5dp"
  7. android:layout_marginBottom="5dp"
  8. android:background="@drawable/select_white"
  9. android:padding="15dp">
  10. <TextView
  11. android:id="@+id/tv_title"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_centerVertical="true"
  15. android:layout_marginLeft="10dp"
  16. android:textSize="18dp"/>
  17. </RelativeLayout>
  1. <color name="white_pressed">#FFCFCFCF</color>
  2. <color name="white">#fff</color>

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号