当前位置:   article > 正文

Android中RecyclerView实现拖拽的功能_android 实现recyclerview图片拖拽到外部

android 实现recyclerview图片拖拽到外部

功能效果图类似于APP的桌面的图标拖动,各位阅读者就不截图了,直接贴代码了。

 

继承ItemTouchHelper重写方法

  1. private MyAdapter myAdapter ;
  2. private ItemTouchHelper mItemTouchHelper;
  3. public class MyItemTouchHelper extends ItemTouchHelper.Callback{
  4. @Override
  5. public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
  6. if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
  7. final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  8. final int swipeFlags = 0;
  9. return makeMovementFlags(dragFlags, swipeFlags);
  10. } else {
  11. final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
  12. final int swipeFlags = 0;
  13. return makeMovementFlags(dragFlags, swipeFlags);
  14. }
  15. }
  16. @Override
  17. public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
  18. //得到当拖拽的viewHolder的Position
  19. int fromPosition = viewHolder.getAdapterPosition();
  20. //拿到当前拖拽到的item的viewHolder
  21. int toPosition = target.getAdapterPosition();
  22. if (fromPosition < toPosition) {
  23. for (int i = fromPosition; i < toPosition; i++) {
  24. Collections.swap(list, i, i + 1);
  25. }
  26. } else {
  27. for (int i = fromPosition; i > toPosition; i--) {
  28. Collections.swap(list, i, i - 1);
  29. }
  30. }
  31. myAdapter.notifyItemMoved(fromPosition, toPosition);
  32. return true;
  33. }
  34. @Override
  35. public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
  36. }
  37. /**
  38. * 长按选中Item的时候开始调用
  39. *
  40. * @param viewHolder
  41. * @param actionState
  42. */
  43. @Override
  44. public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
  45. if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
  46. viewHolder.itemView.setBackgroundColor(Color.GRAY);
  47. }
  48. super.onSelectedChanged(viewHolder, actionState);
  49. }
  50. /**
  51. * 手指松开的时候还原
  52. * @param recyclerView
  53. * @param viewHolder
  54. */
  55. @RequiresApi(api = Build.VERSION_CODES.M)
  56. @Override
  57. public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  58. super.clearView(recyclerView, viewHolder);
  59. viewHolder.itemView.setBackgroundColor(getColor(R.color.black_alpha_01));
  60. }
  61. /**
  62. * 重写拖拽不可用
  63. * @return
  64. */
  65. @Override
  66. public boolean isLongPressDragEnabled() {
  67. return true;
  68. }
  69. }

适配器如下

 

  1. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
  2. List<MainTabNewBean.ResultdataBean.ListBean> list ;
  3. public MyAdapter (List<MainTabNewBean.ResultdataBean.ListBean> list){
  4. this.list = list ;
  5. }
  6. @NonNull
  7. @Override
  8. public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  9. return new MyViewHolder(LayoutInflater.from(NewMainTableActivity.this).inflate(R.layout.item_new_main,parent,false));
  10. }
  11. @Override
  12. public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
  13. holder.tvMainText.setText(list.get(position).getSubjectTitle());
  14. Glide.with(NewMainTableActivity.this).load(list.get(position).getSubjectIcon()).into(holder.ivMainImg);
  15. holder.tvMainText.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. // NewMainTableActivity.this.startActivity(new Intent(MainActivity.this,VideoNomarlActivity.class));
  19. }
  20. });
  21. }
  22. @Override
  23. public int getItemCount() {
  24. return list.size();
  25. }
  26. public class MyViewHolder extends RecyclerView.ViewHolder{
  27. public ImageView ivMainImg ;
  28. public TextView tvMainText ;
  29. public LinearLayout llMainItemBac ;
  30. public MyViewHolder(@NonNull View itemView) {
  31. super(itemView);
  32. ivMainImg = itemView.findViewById(R.id.ivMainImg);
  33. tvMainText = itemView.findViewById(R.id.tvMainText);
  34. llMainItemBac = itemView.findViewById(R.id.llMainItemBac);
  35. }
  36. }
  37. }

item_new_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:background="@color/black_alpha_01"
  6. android:baselineAligned="false"
  7. android:orientation="vertical" >
  8. <LinearLayout
  9. android:id="@+id/llMainItemBac"
  10. android:layout_margin="0.5dp"
  11. android:padding="4dp"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:background="@color/white_color"
  15. android:gravity="center">
  16. <RelativeLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:background="@color/clear_color"
  20. android:gravity="center">
  21. <ImageView
  22. android:id="@+id/ivMainImg"
  23. android:layout_width="40dip"
  24. android:layout_height="40dip"
  25. android:layout_centerHorizontal="true"
  26. android:scaleType="fitXY"
  27. />
  28. <TextView
  29. android:id="@+id/tvMainText"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:layout_below="@id/ivMainImg"
  33. android:layout_marginLeft="10dip"
  34. android:layout_marginRight="10dip"
  35. android:layout_marginTop="10dip"
  36. android:background="@color/clear_color"
  37. android:ellipsize="end"
  38. android:gravity="center_vertical|center_horizontal"
  39. android:textColor="@color/ua_dark_gray_text_color"
  40. android:textSize="16sp"
  41. android:text=""/>
  42. </RelativeLayout>
  43. </LinearLayout>
  44. </LinearLayout>

Activity中的实现

MainTabNewBean是根据自己的数据类型生成的,可以根据自己的需求去改

  1. rv = findViewById(R.id.rc_people_list);
  2. GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3) ;
  3. rv.setLayoutManager(gridLayoutManager);
  4. MainTabNewBean mainTabNewBean = new Gson().fromJson(s,MainTabNewBean.class);
  5. list = mainTabNewBean.getResultdata().getList();
  6. rv.setAdapter(myAdapter = new MyAdapter(list));
  7. Log.i("ewqeqw", "getDate: "+list.size());
  8. mItemTouchHelper = new ItemTouchHelper(new MyItemTouchHelper());
  9. mItemTouchHelper.attachToRecyclerView(rv);

 

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

闽ICP备14008679号