【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 打开 )


目录
相关文章
|
前端开发 Android开发 定位技术
【Android】android镜像翻转
Android镜像翻转指的是将屏幕进行水平的翻转,达到所有内容显示都会反向的效果,就像是在镜子中看到的界面一样。这种应用的使用场景相对比较受限,主要用在一些需要使用Android手机界面进行镜面投影的地方,比如说车载手机hud导航之类的。 镜像翻转的效果如下:      镜像水平翻转前后效果 在没办法对硬件进行直接翻转的时候,只能从代码进行着手。最先想到的方法是一种比较弱的实现方案
5106 0
|
1月前
|
缓存 Ubuntu 安全
Ubuntu 日常使用指南:新手必知的 10 个实用技巧
注意事项:所有操作需谨慎执行,关键数据建议通过 deja-dup 工具备份。本文技巧均测试于Ubuntu 24.04 LTS,不同版本可能存在差异。
|
8月前
|
人工智能 算法 测试技术
StockMixer:上海交大推出预测股票价格的 MLP 架构,通过捕捉指标、时间和股票间的复杂相关性,预测下一个交易日的收盘价
StockMixer 是上海交通大学推出的基于多层感知器的股票价格预测架构,通过指标、时间和股票混合实现高效预测。
482 11
StockMixer:上海交大推出预测股票价格的 MLP 架构,通过捕捉指标、时间和股票间的复杂相关性,预测下一个交易日的收盘价
|
Android开发 Windows
mac下Android Studio 快捷键(持续更新)
mac下Android Studio 快捷键(持续更新)
650 1
|
存储 数据采集 数据可视化
基于Python flask+MySQL+echart的电影数据分析可视化系统
该博客文章介绍了一个基于Python Flask框架、MySQL数据库和ECharts库构建的电影数据分析可视化系统,系统功能包括猫眼电影数据的爬取、存储、展示以及电影评价词云图的生成。
674 1
|
关系型数据库 MySQL
【问题处理】—— Mysql : You can‘t specify target table for update in FROM clause
【问题处理】—— Mysql : You can‘t specify target table for update in FROM clause
1271 1
|
API Android开发
[Android]图片加载库Glide
[Android]图片加载库Glide
219 0
|
持续交付 图形学 开发者
【Unity实战系列】Unity的下载安装以及汉化教程
【Unity实战系列】Unity的下载安装以及汉化教程
586 0
|
API iOS开发
AppStore 打包上传后提示“二进制文件无效”
搜索了2个小时,上传了5个版本后,终于解决了。网上是这样说的: http://stackoverflow.com/questions/26163856/invalid-swift-support-invalid-implementation-of-swift 1、重启Xcode,clean build 2、重启Mac OSX 3、换个新版本Xcode 最后解决答案是: 不要用私有Api,如果工程里面导入了Reveal.framework 要删除掉重新打包上传。
4254 0
|
XML Android开发 数据格式
Android RecyclerView的notify方法和动画的刷新详解(一)
Android RecyclerView的notify方法和动画的刷新详解
331 0