当前位置:   article > 正文

Android RecyclerView实现拖拽排序_android recyclerview 拖动排序

android recyclerview 拖动排序

布局文件

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity">
  7. <androidx.recyclerview.widget.RecyclerView
  8. android:id="@+id/rv_picture"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:transcriptMode="alwaysScroll"/>
  12. </RelativeLayout>

item_pic.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="124dp"
  3. android:layout_height="124dp"
  4. android:background="@color/white">
  5. <ImageView
  6. android:id="@+id/iv_picture"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:scaleType="fitXY"
  10. android:padding="2dp"/>
  11. </RelativeLayout>

适配器PicAdapter.class

  1. public class PicAdapter extends RecyclerView.Adapter<PicAdapter.ViewHolder> {
  2. private Context context;
  3. private List<PicBean> picBeans;
  4. public PicAdapter(Context context, List<PicBean> picBeans) {
  5. this.context = context;
  6. this.picBeans = picBeans;
  7. }
  8. @Override
  9. public PicAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  10. View view = LayoutInflater.from(context).inflate(R.layout.item_pic, parent, false);
  11. return new ViewHolder(view);
  12. }
  13. @Override
  14. public void onBindViewHolder(PicAdapter.ViewHolder holder, int position) {
  15. Log.d("dddd","onBindViewHolder");
  16. holder.iv_picture.setImageResource(picBeans.get(position).getImage());
  17. }
  18. @Override
  19. public int getItemCount() {
  20. return picBeans.size();
  21. }
  22. public class ViewHolder extends RecyclerView.ViewHolder {
  23. public ImageView iv_picture;
  24. public ViewHolder(View itemView) {
  25. super(itemView);
  26. iv_picture = (ImageView) itemView.findViewById(R.id.iv_picture);
  27. }
  28. }
  29. }

编写实现拖拽的帮助类MyItemTouchHelper.class,继承父类ItemTouchHelper.Callback,重写onMove{}方法,处理长按拖拽操作,对位置进行更改

  1. public class MyItemTouchHelper extends ItemTouchHelper.Callback{
  2. private List<PicBean> pictureBeans;
  3. private PicAdapter recycleViewAdapter;
  4. MainActivity activity;
  5. public MyItemTouchHelper(MainActivity activity, List<PicBean> pictureBeans, PicAdapter recycleViewAdapter) {
  6. Log.d("dddd","into MyItemTouchHelper");
  7. this.pictureBeans = pictureBeans;
  8. this.recycleViewAdapter = recycleViewAdapter;
  9. this.activity = activity;
  10. }
  11. @Override
  12. public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
  13. final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  14. final int swipeFlags = 0;
  15. return makeMovementFlags(dragFlags, swipeFlags);
  16. }
  17. @Override
  18. public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
  19. recyclerView.getParent().requestDisallowInterceptTouchEvent(true);
  20. //得到当拖拽的viewHolder的Position
  21. int fromPosition = viewHolder.getAdapterPosition();
  22. //拿到当前拖拽到的item的viewHolder
  23. int toPosition = target.getAdapterPosition();
  24. if (fromPosition < toPosition) {
  25. for (int i = fromPosition; i < toPosition; i++) {
  26. Collections.swap(pictureBeans, i, i + 1);
  27. }
  28. } else {
  29. for (int i = fromPosition; i > toPosition; i--) {
  30. Collections.swap(pictureBeans, i, i - 1);
  31. }
  32. }
  33. recycleViewAdapter.notifyItemMoved(fromPosition, toPosition);
  34. Log.d("dddd"," fromPosition = "+fromPosition+" toPosition"+toPosition);
  35. return true;
  36. }
  37. @Override
  38. public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
  39. }
  40. /**
  41. * 长按选中Item时修改颜色
  42. *
  43. * @param viewHolder
  44. * @param actionState
  45. */
  46. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  47. @Override
  48. public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
  49. //if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
  50. //viewHolder.itemView.setBackground(getDrawable(R.drawable.card_drag_selected));
  51. //}
  52. super.onSelectedChanged(viewHolder, actionState);
  53. }
  54. /**
  55. * 手指松开的时候还原颜色
  56. * @param recyclerView
  57. * @param viewHolder
  58. */
  59. @RequiresApi(api = Build.VERSION_CODES.M)
  60. @Override
  61. public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  62. super.clearView(recyclerView, viewHolder);
  63. //viewHolder.itemView.setBackground(getDrawable(R.drawable.card));
  64. }
  65. /**
  66. * 重写拖拽不可用
  67. * @return
  68. */
  69. @Override
  70. public boolean isLongPressDragEnabled() {
  71. return true;
  72. }
  73. }

主界面MainActivity.class

  1. public class MainActivity extends AppCompatActivity {
  2. private RecyclerView recyclerView;
  3. private com.example.dragdemo.PicAdapter picAdapter;
  4. private List<com.example.dragdemo.PicBean> picBeans;
  5. private ItemTouchHelper mItemTouchHelper;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. //模拟数据
  11. picBeans = new ArrayList<>();
  12. int image = 0;
  13. for (int i = 0;i<12;i++){
  14. com.example.dragdemo.PicBean picBean = new com.example.dragdemo.PicBean();
  15. if (i % 3 == 0){
  16. image = R.color.black;
  17. }else if (i % 3 == 1){
  18. image = R.color.purple_200;
  19. }else if (i % 3 == 2){
  20. image = R.color.teal_700;
  21. }
  22. Log.d("dddd","image = "+image);
  23. picBean.setImage(image);
  24. picBeans.add(picBean);
  25. }
  26. Log.d("dddd","picBeans.size() = "+picBeans.size());
  27. recyclerView = (RecyclerView)findViewById(R.id.rv_picture);
  28. GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
  29. recyclerView.setLayoutManager(gridLayoutManager);
  30. recyclerView.setNestedScrollingEnabled(true);
  31. //创建并设置适配器
  32. picAdapter = new PicAdapter(this,picBeans);
  33. recyclerView.setAdapter(picAdapter);
  34. //添加拖拽事件
  35. mItemTouchHelper = new ItemTouchHelper(new MyItemTouchHelper(this,picBeans,picAdapter));
  36. mItemTouchHelper.attachToRecyclerView(recyclerView);
  37. }
  38. }

到这里,我们的RecyclerView拖拽排序功能就完成啦 

接下来可以看一下效果:

 

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

闽ICP备14008679号