Android一步步带你在RecyclerView上面实现"拖放"和"滑动删除"功能

Android一步步带你在RecyclerView上面实现"拖放"和"滑动删除"功能,第1张

概述先给大家展示下大概效果图:Android上面有许多的教程,库和示例,在RecyclerView上面实现\"拖放\"和\"滑动删除\"功能.尽管有更新,更好的方法可用,但是大多数人依然使用旧的View.OnDragListener和RomanNurik的SwipeTo

先给大家展示下大概效果图:

AndroID上面有许多的教程,库和示例,在RecyclerVIEw上面实现"拖放"和"滑动删除"功能. 尽管有更新,更好的方法可用,但是大多数人依然使用旧的View.OnDragListener和Roman Nurik的SwipeToDismiss方式. 除了经常使用GestureDetector和onIntercepttouchEvent之外,几乎很少有人使用新的API,要不然的话,实现就复杂. 事实上真的有十分简单的方式在RecyclerVIEw上面添加这两个功能. 它只要求一个类,而且这个类已经是AndroID支持包的一部分.

ItemTouchHelper

itemtouchhelper是一个强大的通用程序,在RecyclerVIEw上面添加"拖放"和"滑动删除"时,你所需要做的所有事情,它都会负责处理. 它是RecyclerVIEw.Itemdecoration的子类,这意味着它可以轻易地添加到任何已经存在的LayoutManager和Adapter上面! 它不会影响添加到item上的动画,并且支持类别严格的"拖",以及"放"时的动画,还可以支持更多.

准备:

首先,我们所需要的是添加RecyclerVIEw的依赖:

 compile 'com.androID.support:recyclervIEw-v7:25.3.0'

使用itemtouchhelper和itemtouchhelper.Callback:

为了使用itemtouchhelper,你将创建一个itemtouchhelper.Callback,这是一个接口,允许你监听"move"和"swipe"事件,而且你可以通过Callback来控件已选中vIEw的状态,并且可以改变该vIEw的默认动画. 如果只是想要一个基础实现,你可以使用SimpleCallback这个帮助类,但是为了学习Callback的工作原理,我们将会自己实现一个.

为了激活基本的"拖放"和"滑动删除",我们必须覆盖的主要方法是:

getMovementFlags(RecyclerVIEw,VIEwHolder)onMove(RecyclerVIEw,VIEwHolder,VIEwHolder)onSwiped(VIEwHolder,int)

我们也要使用这两个方法:

isLongPressDragEnabled()isItemVIEwSwipeEnabled()

我们一个一个地看一下:

@OverrIDepublic int getMovementFlags(RecyclerVIEw recyclerVIEw,RecyclerVIEw.VIEwHolder vIEwHolder) { int dragFlags = itemtouchhelper.UP | itemtouchhelper.DOWN; int swipeFlags = itemtouchhelper.START | itemtouchhelper.END; return makeMovementFlags(dragFlags,swipeFlags);}

itemtouchhelper允许你轻易地决定事件的方向.你必须实现getMovementFlags(RecyclerVIEw,RecyclerVIEw.VIEwHolder)方法来指明"拖"和"滑动"所支持的方向,并且使用itemtouchhelper.makeMovementFlags(int,int)来构建返回标签. 在此我们在两个不同的方向激活"拖"和"滑动".

@OverrIDepublic boolean isLongPressDragEnabled() { return true;}

itemtouchhelper能够用来实现"没有滑动的拖动"或者"没有拖动的滑动",所以你必须精确地指明想要支持的动作. 如果你想要在RecyclerVIEw的item上支持"长按启动拖放"事件,你就必须实现isLongPressDragEnabled()返回true. 此外,itemtouchhelper.startDrag(RecyclerVIEw.VIEwHolder)可以从" *** 作"中启动"拖放",这一点会在之后详述.

@OverrIDepublic boolean isItemVIEwSwipeEnabled() { return true;}

要想要vIEw内部的任意触摸事件都可以启动"滑动"动作,就简单地在isItemVIEwSwipeEnabled()返回true. 此外,itemtouchhelper.startSwipe(RecyclerVIEw.VIEwHolder)能够手动地启动"滑动"事件.

然后,onMove()和onSwiped()方法需要实现,来通知负责更新基础数据的东西. 所以,首先,我们要创建一个接口,以允许我们传递"拖放"和"滑动删除"事件的回调.

public interface itemtouchhelperAdapter { voID onItemmove(int fromposition,int toposition); voID onItemdismiss(int position);}

从当前示例来讲,要实现这些的最简单的方式,是将我们的RecyclerVIEw.Adapter实现这个接口:

public class Recyclerlistadapter extends   RecyclerVIEw.Adapter<ItemVIEwHolder>   implements itemtouchhelperAdapter {// ... code from gist@OverrIDepublic voID onItemdismiss(int position) { mItems.remove(position); notifyItemRemoved(position);}@OverrIDepublic boolean onItemmove(int fromposition,int toposition) { if (fromposition < toposition) {  for (int i = fromposition; i < toposition; i++) {   Collections.swap(mItems,i,i + 1);  } } else {  for (int i = fromposition; i > toposition; i--) {   Collections.swap(mItems,i - 1);  } } notifyItemmoved(fromposition,toposition); return true;}

调用notifyItemRemoved(int)和notifyItemmoved(int,int)是非常重要的,由此,Adapter会更新数据. 请注意,这也很重要,我们改变item的position是在每一次vIEw被切换到新的index,而不是在"放"事件之后.

现在我们回来构建SimpleitemtouchhelperCallback,因为我们依然必须覆盖onMove()和onSwiped()方法. 首先,为Adapter添加构建器和变量:

private final itemtouchhelperAdapter mAdapter;public SimpleitemtouchhelperCallback(  itemtouchhelperAdapter adapter) { mAdapter = adapter;}

然后覆盖剩下的事件并通知Adapter:

@OverrIDepublic boolean onMove(RecyclerVIEw recyclerVIEw,RecyclerVIEw.VIEwHolder vIEwHolder,RecyclerVIEw.VIEwHolder target) { mAdapter.onItemmove(vIEwHolder.getAdapterposition(),target.getAdapterposition()); return true;}@OverrIDepublic voID onSwiped(RecyclerVIEw.VIEwHolder vIEwHolder,int direction) { mAdapter.onItemdismiss(vIEwHolder.getAdapterposition());}

这个Callback应该看起来像这样:

public class SimpleitemtouchhelperCallback extends itemtouchhelper.Callback { private final itemtouchhelperAdapter mAdapter; public SimpleitemtouchhelperCallback(itemtouchhelperAdapter adapter) {  mAdapter = adapter; } @OverrIDe public boolean isLongPressDragEnabled() {  return true; } @OverrIDe public boolean isItemVIEwSwipeEnabled() {  return true; } @OverrIDe public int getMovementFlags(RecyclerVIEw recyclerVIEw,VIEwHolder vIEwHolder) {  int dragFlags = itemtouchhelper.UP | itemtouchhelper.DOWN;  int swipeFlags = itemtouchhelper.START | itemtouchhelper.END;  return makeMovementFlags(dragFlags,swipeFlags); } @OverrIDe public boolean onMove(RecyclerVIEw recyclerVIEw,VIEwHolder vIEwHolder,VIEwHolder target) {  mAdapter.onItemmove(vIEwHolder.getAdapterposition(),target.getAdapterposition());  return true; } @OverrIDe public voID onSwiped(VIEwHolder vIEwHolder,int direction) {  mAdapter.onItemdismiss(vIEwHolder.getAdapterposition()); }}

当Callback准备好之后,我们创建itemtouchhelper并调用attachToRecyclerVIEw(RecyclerVIEw)方法:

itemtouchhelper.Callback callback =  new SimpleitemtouchhelperCallback(adapter);itemtouchhelper touchHelper = new itemtouchhelper(callback);touchHelper.attachToRecyclerVIEw(recyclerVIEw);

当你运行的时候,结果应该看起来像这样:

总结

这是一个itemtouchhelper极简单的实现. 但是我们应该清楚,在RecyclerVIEw上面实现基本的"拖放"和"滑动删除",使用第三方和库是完全没有必要的.

示例代码请点击这里.

以上所述是小编给大家介绍的AndroID一步步带你在RecyclerVIEw上面实现"拖放"和"滑动删除"功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android一步步带你在RecyclerView上面实现"拖放"和"滑动删除"功能全部内容,希望文章能够帮你解决Android一步步带你在RecyclerView上面实现"拖放"和"滑动删除"功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1146567.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存