【RecyclerView】 十五、使用 ItemTouchHelper 实现 RecyclerView 拖动排序 ( ItemTouchHelper 简介 )

简介: 【RecyclerView】 十五、使用 ItemTouchHelper 实现 RecyclerView 拖动排序 ( ItemTouchHelper 简介 )

文章目录

一、ItemTouchHelper 简介

二、RecyclerView 相关资料





一、ItemTouchHelper 简介


官方文档 : https://developer.android.google.cn/reference/kotlin/androidx/recyclerview/widget/ItemTouchHelper


ItemTouchHelper 可以为 RecyclerView 添加 滑动删除效果 和 拖动效果 ;


ItemTouchHelper 需要与 RecyclerView 和 ItemTouchHelper.Callback 结合起来使用 ;



根据想要开发的功能 , 重写不同的方法 ;


如果是想要开发拖动效果相关的功能 , 重写 ItemTouchHelper.Callback 的 onMoved 方法 ;


   

public abstract boolean onMove(@NonNull RecyclerView recyclerView,
                @NonNull ViewHolder viewHolder, @NonNull ViewHolder target);


如果想要开发滑动相关效果 , 重写 ItemTouchHelper.Callback 的 onSwiped 方法 ;


public abstract void onSwiped(@NonNull ViewHolder viewHolder, int direction);



ItemTouchHelper 需要与 LayoutManager 布局管理器结合使用 ;


通过 继承 ItemTouchHelper.Callback 抽象类 , 或


实现 ItemTouchHelper.Callback 接口 ,


这两个操作 自定义 LayoutManager 布局管理器 , 可以达到最优化的效果 ;



看一下 Android 官方定义的 线性布局管理器 LinearLayoutManager , 就实现了 ItemTouchHelper.ViewDropHandler 接口 ;


public class LinearLayoutManager extends RecyclerView.LayoutManager implements
        ItemTouchHelper.ViewDropHandler, RecyclerView.SmoothScroller.ScrollVectorProvider {
}


默认情况下 , ItemTouchHelper 移动 item 组件的 translateX 或 translateY 属性 , 为其重新设置位置 ;


开发者可以自定义这些行为通过覆盖 ItemTouchHelper.Callback 的 onChildDraw 和 onChildDrawOver 方法 ;


大多数情况下只需要覆盖 onChildDraw 方法即可 ;


onChildDraw 方法原型 :


public class ItemTouchHelper extends RecyclerView.ItemDecoration
        implements RecyclerView.OnChildAttachStateChangeListener {
  public abstract static class Callback {
        /**
         * Called by ItemTouchHelper on RecyclerView's onDraw callback.
         * <p>
         * If you would like to customize how your View's respond to user interactions, this is
         * a good place to override.
         * <p>
         * Default implementation translates the child by the given <code>dX</code>,
         * <code>dY</code>.
         * ItemTouchHelper also takes care of drawing the child after other children if it is being
         * dragged. This is done using child re-ordering mechanism. On platforms prior to L, this
         * is
         * achieved via {@link android.view.ViewGroup#getChildDrawingOrder(int, int)} and on L
         * and after, it changes View's elevation value to be greater than all other children.)
         *
         * @param c                 The canvas which RecyclerView is drawing its children
         * @param recyclerView      The RecyclerView to which ItemTouchHelper is attached to
         * @param viewHolder        The ViewHolder which is being interacted by the User or it was
         *                          interacted and simply animating to its original position
         * @param dX                The amount of horizontal displacement caused by user's action
         * @param dY                The amount of vertical displacement caused by user's action
         * @param actionState       The type of interaction on the View. Is either {@link
         *                          #ACTION_STATE_DRAG} or {@link #ACTION_STATE_SWIPE}.
         * @param isCurrentlyActive True if this view is currently being controlled by the user or
         *                          false it is simply animating back to its original state.
         * @see #onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int,
         * boolean)
         */
        public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView,
                @NonNull ViewHolder viewHolder,
                float dX, float dY, int actionState, boolean isCurrentlyActive) {
            ItemTouchUIUtilImpl.INSTANCE.onDraw(c, recyclerView, viewHolder.itemView, dX, dY,
                    actionState, isCurrentlyActive);
        }
    }
}


onChildDrawOver 方法原型 :


public class ItemTouchHelper extends RecyclerView.ItemDecoration
        implements RecyclerView.OnChildAttachStateChangeListener {
  public abstract static class Callback {
        /**
         * Called by ItemTouchHelper on RecyclerView's onDraw callback.
         * <p>
         * If you would like to customize how your View's respond to user interactions, this is
         * a good place to override.
         * <p>
         * Default implementation translates the child by the given <code>dX</code>,
         * <code>dY</code>.
         * ItemTouchHelper also takes care of drawing the child after other children if it is being
         * dragged. This is done using child re-ordering mechanism. On platforms prior to L, this
         * is
         * achieved via {@link android.view.ViewGroup#getChildDrawingOrder(int, int)} and on L
         * and after, it changes View's elevation value to be greater than all other children.)
         *
         * @param c                 The canvas which RecyclerView is drawing its children
         * @param recyclerView      The RecyclerView to which ItemTouchHelper is attached to
         * @param viewHolder        The ViewHolder which is being interacted by the User or it was
         *                          interacted and simply animating to its original position
         * @param dX                The amount of horizontal displacement caused by user's action
         * @param dY                The amount of vertical displacement caused by user's action
         * @param actionState       The type of interaction on the View. Is either {@link
         *                          #ACTION_STATE_DRAG} or {@link #ACTION_STATE_SWIPE}.
         * @param isCurrentlyActive True if this view is currently being controlled by the user or
         *                          false it is simply animating back to its original state.
         * @see #onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int,
         * boolean)
         */
        public void onChildDrawOver(@NonNull Canvas c, @NonNull RecyclerView recyclerView,
                ViewHolder viewHolder,
                float dX, float dY, int actionState, boolean isCurrentlyActive) {
            ItemTouchUIUtilImpl.INSTANCE.onDrawOver(c, recyclerView, viewHolder.itemView, dX, dY,
                    actionState, isCurrentlyActive);
        }
    }
}





二、RecyclerView 相关资料


官方文档 :


使用 RecyclerView 创建动态列表 : https://developer.android.google.cn/guide/topics/ui/layout/recyclerview


高级 RecyclerView 自定义 : https://developer.android.google.cn/guide/topics/ui/layout/recyclerview-custom


RecyclerView 官方文档 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView


RecyclerView.Adapter 官方文档 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.Adapter


RecyclerView.ViewHolder 官方文档 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder


RecyclerView.ItemDecoration 官方文档 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.ItemDecoration


GridLayoutManager 官方文档 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/GridLayoutManager


LinearLayoutManager 官方文档 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/LinearLayoutManager


StaggeredGridLayoutManager 官方文档 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/StaggeredGridLayoutManager


ItemTouchHelper 官方文档 : https://developer.android.google.cn/reference/kotlin/androidx/recyclerview/widget/ItemTouchHelper


ItemTouchHelper.Callback 官方文档 : https://developer.android.google.cn/reference/kotlin/androidx/recyclerview/widget/ItemTouchHelper.Callback



代码示例 :


GitHub 源码地址 : https://github.com/han1202012/001_RecyclerView


博客源码快照 : https://download.csdn.net/download/han1202012/14984775


( 使用 Android Studio 打开 )


目录
相关文章
|
6月前
|
XML 数据格式
RecyclerView使用示例(瀑布流)
RecyclerView使用示例(瀑布流)
55 0
|
Android开发 开发者
RecyclerView定制:通用ItemDecoration及全展开RecyclerView的实现
RecyclerView定制:通用ItemDecoration及全展开RecyclerView的实现
448 0
RecyclerView定制:通用ItemDecoration及全展开RecyclerView的实现
利用RecyclerView实现无限轮播广告条2
利用RecyclerView实现无限轮播广告条
利用RecyclerView实现无限轮播广告条1
利用RecyclerView实现无限轮播广告条
|
Android开发
同一页面实现recycleView三种布局【recycleView + adapter】
同一页面实现recycleView三种布局【recycleView + adapter】
151 0
同一页面实现recycleView三种布局【recycleView + adapter】
RecycleView的操作(自定义SnapHelper、ItemDecoration)
RecycleView的操作(自定义SnapHelper、ItemDecoration)
314 0
|
API
为RecyclerView添加下拉刷新功能
在之前的文章中,我们实现了带有header和footer功能的WrapRecyclerView。 现今App中列表的下拉刷新和上拉加载已经是一种习惯了,这两个操作也确实方便很多。 为RecyclerView添加这个功能可以通过多种方法,这里我选用了一种简单的做法。基于pulltorefresh这个库。
183 0
|
Android开发
【Android 事件分发】ItemTouchHelper 实现拖动排序(一)
【Android 事件分发】ItemTouchHelper 实现拖动排序(一)
538 0
【Android 事件分发】ItemTouchHelper 实现拖动排序(一)
|
Android开发
【Android 事件分发】ItemTouchHelper 实现拖动排序(三)
【Android 事件分发】ItemTouchHelper 实现拖动排序(三)
187 0
【Android 事件分发】ItemTouchHelper 实现拖动排序(三)
|
Android开发
【Android 事件分发】ItemTouchHelper 简介 ( 拖动/滑动事件 | ItemTouchHelper.Callback 回调 )(一)
【Android 事件分发】ItemTouchHelper 简介 ( 拖动/滑动事件 | ItemTouchHelper.Callback 回调 )(一)
532 0
【Android 事件分发】ItemTouchHelper 简介 ( 拖动/滑动事件 | ItemTouchHelper.Callback 回调 )(一)