当前位置:   article > 正文

Android实现RecyclerView侧滑删除和长按拖拽-ItemTouchHelper_android recyclerview itemtouchhelper点击

android recyclerview itemtouchhelper点击
转:http://blog.csdn.net/kaifa1321/article/details/51817621
http://blog.csdn.net/u010687392/article/details/47950199

list_item.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:background="#F9F9F9"
  5. android:layout_height="40dp">
  6. <TextView
  7. android:id="@+id/iv_item"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_centerVertical="true"
  11. android:gravity="center" />
  12. <ImageView
  13. android:layout_width="match_parent"
  14. android:layout_height="1dp"
  15. android:layout_alignParentBottom="true"
  16. android:background="#88888888" />
  17. </RelativeLayout>
adapter
  1. package example.com.itemtouchhelper;
  2. import android.content.Context;
  3. import android.support.v7.widget.RecyclerView;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. import java.util.List;
  9. /**
  10. * Created by Administrator on 2016/12/6.
  11. */
  12. public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
  13. private Context context;
  14. private List<String> datas;
  15. private final LayoutInflater mInflater;
  16. public ListAdapter(Context context, List<String> datas) {
  17. this.context = context;
  18. this.datas = datas;
  19. mInflater = LayoutInflater.from(context);
  20. }
  21. @Override
  22. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  23. return new ViewHolder(mInflater.inflate(R.layout.list_item, parent, false));
  24. }
  25. @Override
  26. public void onBindViewHolder(ViewHolder holder, int position) {
  27. System.out.println("======="+position);
  28. holder.iv_item.setText(datas.get(position));
  29. }
  30. @Override
  31. public int getItemCount() {
  32. return datas.size();
  33. }
  34. public static class ViewHolder extends RecyclerView.ViewHolder {
  35. private TextView iv_item;
  36. public ViewHolder(View itemView) {
  37. super(itemView);
  38. iv_item = (TextView) itemView.findViewById(R.id.iv_item);
  39. }
  40. }
  41. }

mainActivity
  1. //1.设置适配器
  2. LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  3. final ListAdapter adapter = new ListAdapter(this, datas);
  4. recyclerView.setLayoutManager(layoutManager);
  5. recyclerView.setAdapter(adapter);
  6. //2.设置ItemTouchHelper
  7. //0则不执行拖动或者滑动
  8. ItemTouchHelper.SimpleCallback mCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
  9. @Override
  10. public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
  11. int fromPosition = viewHolder.getAdapterPosition();
  12. int toPosition = target.getAdapterPosition();
  13. if (fromPosition < toPosition) {
  14. //分别把中间所有的item的位置重新交换
  15. for (int i = fromPosition; i < toPosition; i++) {
  16. Collections.swap(datas, i, i + 1);
  17. }
  18. } else {
  19. //分别把中间所有的item的位置重新交换
  20. for (int i = fromPosition; i > toPosition; i--) {
  21. Collections.swap(datas, i, i - 1);
  22. }
  23. }
  24. adapter.notifyItemMoved(fromPosition, toPosition);
  25. //返回true表示执行拖动
  26. return true;
  27. }
  28. @Override
  29. public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
  30. final int position = viewHolder.getAdapterPosition();
  31. new AlertDialog.Builder(MainActivity.this)
  32. .setCancelable(false)
  33. .setMessage("你确定删除该项吗?")
  34. .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  35. @Override
  36. public void onClick(DialogInterface dialogInterface, int i) {
  37. adapter.notifyDataSetChanged();
  38. }
  39. })
  40. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  41. @Override
  42. public void onClick(DialogInterface dialogInterface, int i) {
  43. datas.remove(position);
  44. adapter.notifyDataSetChanged();
  45. Toast.makeText(MainActivity.this, "删除" + position, Toast.LENGTH_SHORT).show();
  46. }
  47. }).show();
  48. }
  49. @Override
  50. public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
  51. super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
  52. final float alpha = 1 - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
  53. if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
  54. //滑动时改变Item的透明度
  55. viewHolder.itemView.setAlpha(alpha);
  56. viewHolder.itemView.setTranslationX(dX);
  57. }
  58. //防止item复用出现问题,如果大家不理解下面这段代码,可以自行注释效果,
  59. if (alpha <= 0) {
  60. viewHolder.itemView.setAlpha(1);
  61. viewHolder.itemView.setTranslationX(dX);
  62. }
  63. }
  64. @Override
  65. public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
  66. super.onSelectedChanged(viewHolder, actionState);
  67. //当选中Item时候会调用该方法,重写此方法可以实现选中时候的一些动画逻辑
  68. Log.v("zxy", "onSelectedChanged");
  69. }
  70. @Override
  71. public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  72. super.clearView(recyclerView, viewHolder);
  73. //当动画已经结束的时候调用该方法,重写此方法可以实现恢复Item的初始状态
  74. Log.v("zxy", "clearView");
  75. }
  76. };
  77. new ItemTouchHelper(mCallback).attachToRecyclerView(recyclerView);
  78. }

SimpleCallback构造方法需要我们传入两个参数:

1、dragDirs - 表示拖拽的方向,有六个类型的值:LEFT、RIGHT、START、END、UP、DOWN
2、swipeDirs - 表示滑动的方向,有六个类型的值:LEFT、RIGHT、START、END、UP、DOWN
【注】:如果为0,则表示不触发该操作(滑动or拖拽)

然后onMove和onSwiped分别是拖拽和滑动时回调的方法

=====================

temTouchHelper简单使用

要想通过ItemTouchHelper实现交互动画,只需要以下步骤:

  1. 定义一个ItemTouchHelper.Callback回调类,用于实现具体交互效果
  2. 创建一个ItemTouchHelper对象,并传入Callback对象
  3. 调用ItemTouchHelper对象的attachToRecyclerView方法,将ItemTouchHelper对象绑定到指定RecyclerView控件上。

代码如下:

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. final List<String> datas = new ArrayList<>();
  7. for (int i = 0; i < 20; i++) {
  8. datas.add(String.valueOf(i));
  9. }
  10. RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
  11. //1.设置适配器
  12. LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  13. RecycleViewAdapter adapter = new RecycleViewAdapter(this, datas);
  14. recyclerView.setLayoutManager(layoutManager);
  15. recyclerView.setAdapter(adapter);
  16. //2.设置ItemTouchHelper
  17. ItemTouchHelper.Callback callback = new MyItemTouchHelperCallback(adapter);
  18. ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
  19. itemTouchHelper.attachToRecyclerView(recyclerView);
  20. }
  21. }
adpater
  1. package example.com.itemtouchhelper;
  2. import android.content.Context;
  3. import android.support.v7.widget.RecyclerView;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. import java.util.Collections;
  9. import java.util.List;
  10. /**
  11. * Created by Administrator on 2016/12/6.
  12. */
  13. public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> implements ItemTouchMoveListener {
  14. private Context context;
  15. private List<String> datas;
  16. private final LayoutInflater mInflater;
  17. public RecycleViewAdapter(Context context, List<String> datas) {
  18. this.context = context;
  19. this.datas = datas;
  20. mInflater = LayoutInflater.from(context);
  21. }
  22. @Override
  23. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  24. return new ViewHolder(mInflater.inflate(R.layout.list_item, parent, false));
  25. }
  26. @Override
  27. public void onBindViewHolder(ViewHolder holder, int position) {
  28. System.out.println("=======" + position);
  29. holder.iv_item.setText(datas.get(position));
  30. }
  31. @Override
  32. public int getItemCount() {
  33. return datas.size();
  34. }
  35. public static class ViewHolder extends RecyclerView.ViewHolder {
  36. private TextView iv_item;
  37. public ViewHolder(View itemView) {
  38. super(itemView);
  39. iv_item = (TextView) itemView.findViewById(R.id.iv_item);
  40. }
  41. }
  42. /**
  43. * 当Item上下拖动会调用该方法
  44. *
  45. * @param fromPosition
  46. * @param toPosition
  47. * @return
  48. */
  49. @Override
  50. public boolean onItemMove(int fromPosition, int toPosition) {
  51. Collections.swap(datas, fromPosition, toPosition);
  52. notifyItemMoved(fromPosition, toPosition);
  53. return true;
  54. }
  55. /**
  56. * 当Item左右滑动时调用该方法
  57. *
  58. * @param position
  59. * @return
  60. */
  61. @Override
  62. public boolean onItemRemove(int position) {
  63. datas.remove(position);
  64. notifyItemRemoved(position);
  65. return true;
  66. }
  67. }

MyItemTouchHelperCallback
  1. package example.com.itemtouchhelper;
  2. import android.graphics.Canvas;
  3. import android.graphics.Color;
  4. import android.support.v7.widget.RecyclerView;
  5. import android.support.v7.widget.helper.ItemTouchHelper;
  6. /**
  7. * Created by Administrator on 2016/12/6.
  8. */
  9. public class MyItemTouchHelperCallback extends ItemTouchHelper.Callback {
  10. private ItemTouchMoveListener mItemTouchMoveListener;
  11. public MyItemTouchHelperCallback(ItemTouchMoveListener listener) {
  12. this.mItemTouchMoveListener = listener;
  13. }
  14. /**
  15. * 设置RecyclerView支持拖动和滑动的方向
  16. */
  17. @Override
  18. public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  19. //支持上下拖动
  20. int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
  21. //支持左右滑动
  22. int swipeFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
  23. int flags = makeMovementFlags(dragFlags, swipeFlag);
  24. return flags;
  25. }
  26. /**
  27. * 当上下拖动的时候调用该方法
  28. */
  29. @Override
  30. public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
  31. if (viewHolder.getItemViewType() != target.getItemViewType()) {
  32. // 当item的类型不一样的时候不能交换
  33. return false;
  34. }
  35. boolean result = mItemTouchMoveListener.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
  36. // boolean result = mItemTouchMoveListener.onItemMove(viewHolder.getLayoutPosition(), target.getLayoutPosition());
  37. return result;
  38. }
  39. /**
  40. * 当左右滑动的时候调用该方法
  41. */
  42. @Override
  43. public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
  44. mItemTouchMoveListener.onItemRemove(viewHolder.getAdapterPosition());
  45. }
  46. /**
  47. * 选中状态改变时监听
  48. */
  49. @Override
  50. public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
  51. if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
  52. //不是空闲状态
  53. viewHolder.itemView.setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.colorPrimary));
  54. }
  55. super.onSelectedChanged(viewHolder, actionState);
  56. }
  57. /**
  58. * 恢复item状态
  59. */
  60. @Override
  61. public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  62. viewHolder.itemView.setBackgroundColor(Color.parseColor("#F9F9F9"));
  63. super.clearView(recyclerView, viewHolder);
  64. }
  65. /**
  66. * holde ItemView绘制,属性动画
  67. */
  68. @Override
  69. public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
  70. //在左右滑动时,让item的透明度随着移动而改变,并缩放
  71. float alpha = 1 - Math.abs(dX) / viewHolder.itemView.getWidth();
  72. if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
  73. viewHolder.itemView.setAlpha(alpha);
  74. viewHolder.itemView.setScaleX(alpha);
  75. viewHolder.itemView.setScaleY(alpha);
  76. }
  77. //防止item复用出现问题,如果大家不理解下面这段代码,可以自行注释效果,
  78. if (alpha <= 0) {
  79. viewHolder.itemView.setAlpha(1);
  80. viewHolder.itemView.setScaleX(1);
  81. viewHolder.itemView.setScaleY(1);
  82. }
  83. super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
  84. }
  85. }

  1. package example.com.itemtouchhelper;
  2. /**
  3. * Created by Administrator on 2016/12/6.
  4. */
  5. public interface ItemTouchMoveListener {
  6. /**
  7. * 当Item上下拖动时调用
  8. */
  9. boolean onItemMove(int fromPosition, int toPosition);
  10. /**
  11. * 当item左右滑动时调用
  12. */
  13. boolean onItemRemove(int position);
  14. }







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

闽ICP备14008679号