日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/6/17 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【RecyclerView】 十五、使用 ItemTouchHelper 实现 RecyclerView 拖动排序 ( ItemTouchHelper 简介 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、ItemTouchHelper 簡介
  • 二、RecyclerView 相關資料





一、ItemTouchHelper 簡介



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

ItemTouchHelper 可以為 RecyclerView 添加 滑動刪除效果 拖動效果 ;

ItemTouchHelper 需要與 RecyclerViewItemTouchHelper.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 implementsItemTouchHelper.ViewDropHandler, RecyclerView.SmoothScroller.ScrollVectorProvider { }

默認情況下 , ItemTouchHelper 移動 item 組件的 translateX 或 translateY 屬性 , 為其重新設置位置 ;

開發者可以自定義這些行為通過覆蓋 ItemTouchHelper.CallbackonChildDrawonChildDrawOver 方法 ;

大多數情況下只需要覆蓋 onChildDraw 方法即可 ;

onChildDraw 方法原型 :

public class ItemTouchHelper extends RecyclerView.ItemDecorationimplements 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.ItemDecorationimplements 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 打開 )

總結

以上是生活随笔為你收集整理的【RecyclerView】 十五、使用 ItemTouchHelper 实现 RecyclerView 拖动排序 ( ItemTouchHelper 简介 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。