查看: 333|回复: 0

[手机开发] Android使用RecycleView实现拖拽交换item位置

发表于 2018-1-6 09:59:19

本文实例为大家分享了RecycleView实现拖拽交换item位置的具体代码,供大家参考,具体内容如下

老规矩,先来一张效果图:

相比起ListView而言,RecycleView实现拖拽交换位置的效果要简单很多,因为通过SDK中的ItemTouchHelper工具类可以轻松的实现这种效果,并且一套代码支持所有布局方式;而ListView的话则需要通过生成View的缓存镜像设置到ImageView中,然后通过WindowManager来操作该ImageView,具体怎么实现这里就不展开讲解了.回归到ItemTouchHelper话题上,这个工具类我们需要关心的方法只有一个,即:

  1. public void attachToRecyclerView(@Nullable RecyclerView recyclerView) {}
复制代码

通过名字也可以知道其作用就是和RecyclerView 建立关系.
而我们真正需要关心的地方就是ItemTouchHelper的内部类ItemTouchHelper.Callback,它是一个抽象类,需要我们去实现下面这几个关键的抽象方法以及重写几个非抽象的方法:

  1. //决定拖拽/滑动的方向
  2. public abstract int getMovementFlags(RecyclerView recyclerView,
  3. ViewHolder viewHolder);
  4. //和位置交换有关,可用于实现drag功能
  5. public abstract boolean onMove(RecyclerView recyclerView,
  6. ViewHolder viewHolder, ViewHolder target);
  7. //和滑动有关,可用于实现swipe功能
  8. public abstract void onSwiped(ViewHolder viewHolder, int direction);
  9. //是否长按启用拖拽功能,默认是true
  10. public boolean isLongPressDragEnabled() {return true;}
  11. //是否支持滑动,默认true
  12. public boolean isItemViewSwipeEnabled() {return true;}
  13. //和目标View的状态改变有关,例如drag,swipe,ide
  14. public void onSelectedChanged(ViewHolder viewHolder, int actionState) {}
  15. //和移除View的状态有关,通常用于清除在onSelectedChanged,onChildDraw中对View设置的动画
  16. public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) {}
复制代码

当创建完ItemTouchHelper.Callback的实现类,我这里称之为SimpleItemTouchHelperCallback后,还需要将数据的变化以及View的状态通知到RecycleView的Adapter中,为了达到解耦的目的,通常可以通过定义接口来实现,在SimpleItemTouchHelperCallback的构造方法中传入该解耦接口的引用,并让RecycleView的Adapter实现该解耦的接口,这样就实现了这2个类的通信问题了.

来看看我定义的解耦接口:

  1. /**
  2. * 定义RecycleView的Adapter和SimpleItemTouchHelperCallback直接交互的接口方法
  3. * Created by mChenys on 2017/2/16.
  4. */
  5. public interface ItemTouchHelperAdapter {
  6. //数据交换
  7. void onItemMove(RecyclerView.ViewHolder source, RecyclerView.ViewHolder target);
  8. //数据删除
  9. void onItemDissmiss(RecyclerView.ViewHolder source);
  10. //drag或者swipe选中
  11. void onItemSelect(RecyclerView.ViewHolder source);
  12. //状态清除
  13. void onItemClear(RecyclerView.ViewHolder source);
  14. }
复制代码

完整的SimpleItemTouchHelperCallback代码如下:

  1. /**
  2. * 处理RecycleView的选中,拖拽移动,拖拽删除的实现类
  3. * Created by mChenys on 2017/2/16.
  4. */
  5. public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
  6. private ItemTouchHelperAdapter mAdapter;
  7. public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
  8. mAdapter = adapter;
  9. }
  10. @Override
  11. public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  12. //int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; //允许上下的拖动
  13. //int dragFlags =ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; //允许左右的拖动
  14. //int swipeFlags = ItemTouchHelper.LEFT; //只允许从右向左侧滑
  15. //int swipeFlags = ItemTouchHelper.DOWN; //只允许从上向下侧滑
  16. //一般使用makeMovementFlags(int,int)或makeFlag(int, int)来构造我们的返回值
  17. //makeMovementFlags(dragFlags, swipeFlags)
  18. int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; //允许上下左右的拖动
  19. return makeMovementFlags(dragFlags, 0);
  20. }
  21. @Override
  22. public boolean isLongPressDragEnabled() {
  23. return true;//长按启用拖拽
  24. }
  25. @Override
  26. public boolean isItemViewSwipeEnabled() {
  27. return false; //不启用拖拽删除
  28. }
  29. @Override
  30. public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
  31. //通过接口传递拖拽交换数据的起始位置和目标位置的ViewHolder
  32. mAdapter.onItemMove(source, target);
  33. return true;
  34. }
  35. @Override
  36. public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
  37. //移动删除回调,如果不用可以不用理
  38. // mAdapter.onItemDissmiss(viewHolder);
  39. }
  40. @Override
  41. public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
  42. super.onSelectedChanged(viewHolder, actionState);
  43. if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
  44. //当滑动或者拖拽view的时候通过接口返回该ViewHolder
  45. mAdapter.onItemSelect(viewHolder);
  46. }
  47. }
  48. @Override
  49. public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  50. super.clearView(recyclerView, viewHolder);
  51. if (!recyclerView.isComputingLayout()) {
  52. //当需要清除之前在onSelectedChanged或者onChildDraw,onChildDrawOver设置的状态或者动画时通过接口返回该ViewHolder
  53. mAdapter.onItemClear(viewHolder);
  54. }
  55. }
  56. }
复制代码

RecycleView.Adapter实现类代码

  1. /**
  2. * Created by mChenys on 2017/2/15.
  3. */
  4. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements ItemTouchHelperAdapter {
  5. ...
  6. @Override
  7. public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  8. ...
  9. }
  10. @Override
  11. public void onBindViewHolder(final MyAdapter.MyViewHolder holder, int position) {
  12. ...
  13. }
  14. @Override
  15. public int getItemCount() {
  16. //注意:这里最少有一个,因为有多了一个添加按钮
  17. return null == mData ? 1 : mData.size() + 1;
  18. }
  19. @Override
  20. public void onItemMove(RecyclerView.ViewHolder source,
  21. RecyclerView.ViewHolder target) {
  22. int fromPosition = source.getAdapterPosition();
  23. int toPosition = target.getAdapterPosition();
  24. if (fromPosition < mData.size() && toPosition < mData.size()) {
  25. //交换数据位置
  26. Collections.swap(mData, fromPosition, toPosition);
  27. //刷新位置交换
  28. notifyItemMoved(fromPosition, toPosition);
  29. }
  30. //移动过程中移除view的放大效果
  31. onItemClear(source);
  32. }
  33. @Override
  34. public void onItemDissmiss(RecyclerView.ViewHolder source) {
  35. int position = source.getAdapterPosition();
  36. mData.remove(position); //移除数据
  37. notifyItemRemoved(position);//刷新数据移除
  38. }
  39. @Override
  40. public void onItemSelect(RecyclerView.ViewHolder viewHolder) {
  41. //当拖拽选中时放大选中的view
  42. viewHolder.itemView.setScaleX(1.2f);
  43. viewHolder.itemView.setScaleY(1.2f);
  44. }
  45. @Override
  46. public void onItemClear(RecyclerView.ViewHolder viewHolder) {
  47. //拖拽结束后恢复view的状态
  48. viewHolder.itemView.setScaleX(1.0f);
  49. viewHolder.itemView.setScaleY(1.0f);
  50. }
  51. public class MyViewHolder extends RecyclerView.ViewHolder {
  52. ...
  53. public MyViewHolder(View itemView) {
  54. super(itemView);
  55. ...
  56. }
  57. }
  58. }
复制代码

MainActivity的使用方式

  1. /**
  2. * Created by mChenys on 2017/2/16.
  3. */
  4. public class MainActivity extends AppCompatActivity {
  5. ...
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. ...
  10. mRecyclerView = (RecyclerView) findViewById(R.id.recycleView);
  11. //创建adapter
  12. MyAdapter myAdapter = new MyAdapter(this, mData);
  13. //设置默认的布局方式
  14. mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
  15. //设置adapter
  16. mRecyclerView.setAdapter(myAdapter);
  17. //创建SimpleItemTouchHelperCallback
  18. ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(myAdapter);
  19. //用Callback构造ItemtouchHelper
  20. ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
  21. //调用ItemTouchHelper的attachToRecyclerView方法建立联系
  22. touchHelper.attachToRecyclerView(mRecyclerView);
  23. }
  24. @Override
  25. public boolean onCreateOptionsMenu(Menu menu) {
  26. ...
  27. return super.onCreateOptionsMenu(menu);
  28. }
  29. @Override
  30. public boolean onOptionsItemSelected(MenuItem item) {
  31. ...
  32. return super.onOptionsItemSelected(item);
  33. }
  34. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报