【Android 事件分发】ItemTouchHelper 事件分发源码分析 ( 绑定 RecyclerView )
Android 事件分發 系列文章目錄
【Android 事件分發】事件分發源碼分析 ( 驅動層通過中斷傳遞事件 | WindowManagerService 向 View 層傳遞事件 )
【Android 事件分發】事件分發源碼分析 ( Activity 中各層級的事件傳遞 | Activity -> PhoneWindow -> DecorView -> ViewGroup )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 一 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 二 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 三 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 四 | View 事件傳遞機制 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 五 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 六 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 七 )
【Android 事件分發】ItemTouchHelper 簡介 ( 拖動/滑動事件 | ItemTouchHelper.Callback 回調 )
【Android 事件分發】ItemTouchHelper 實現側滑刪除 ( 設置滑動方向 | 啟用滑動操作 | 滑動距離判定 | 滑動速度判定 | 設置動畫時間 | 設置側滑觸發操作 )
【Android 事件分發】ItemTouchHelper 實現拖動排序 ( 設置滑動方向 | 啟啟用長按拖動功能 | 拖動距離判定 | 設置拖動觸發操作 )
【Android 事件分發】ItemTouchHelper 事件分發源碼分析 ( 綁定 RecyclerView )
文章目錄
- Android 事件分發 系列文章目錄
- 一、ItemTouchHelper 事件分發源碼分析入口
- 二、ItemTouchHelper 綁定 RecyclerView 源碼分析
- 1、ItemTouchHelper.attachToRecyclerView 方法分析
- 2、ItemTouchHelper.setupCallbacks 方法分析
- 3、RecyclerView.ItemDecoration 源碼分析
- 三、博客資源
一、ItemTouchHelper 事件分發源碼分析入口
ItemTouchHelper 使用時 , 是為 ItemTouchHelper 設置一個 RecyclerView 列表 , 不是給 RecyclerView 設置一個 ItemTouchHelper ;
//4. 添加拖動/滑動事件Callback callback = new Callback(adapter);mItemTouchHelper = new ItemTouchHelper(callback);mItemTouchHelper.attachToRecyclerView(recycler_view);因此 , 事件分發的核心處理邏輯 , 都在 ItemTouchHelper 中實現 , 要研究其中的事件分發原理 , 主要分析 ItemTouchHelper 中的源碼即可 ;
二、ItemTouchHelper 綁定 RecyclerView 源碼分析
1、ItemTouchHelper.attachToRecyclerView 方法分析
ItemTouchHelper.attachToRecyclerView 方法 , 用于將 ItemTouchHelper 與 RecyclerView 進行綁定 ; 以該方法為入口 , 進行源碼分析 ;
在初始化之前 , 判定該 RecyclerView 是否已經綁定 , 如果已經綁定 , 不再執行該綁定方法 ;
if (mRecyclerView == recyclerView) {// 判定是否已經綁定 , 如果已經綁定 , 不再執行綁定方法 return; // nothing to do}然后清空之前原有的回調 , 其中涉及到 destroyCallbacks 方法 , 該 destroyCallbacks 方法與 setupCallbacks 方法相對應 , 一個是設置 , 一個是銷毀 ;
if (mRecyclerView != null) {// 使用前 , 清空所有的回調 // 使用前重置 destroyCallbacks();}最后 , 設置當前的 mRecyclerView 成員為綁定的 RecyclerView 列表 , 并調用 setupCallbacks 方法 , 為 ItemTouchHelper 設置回調 ;
在 setupCallbacks 中 , 調用 RecyclerView.addOnItemTouchListener 方法 , 為 RecyclerView 設置了觸摸監聽器 , 該觸摸監聽器是定義在 ItemTouchHelper 中的成員變量 private final OnItemTouchListener mOnItemTouchListener ;
// 添加了每個條目上的觸摸監聽器 mOnItemTouchListener // 該監聽器是定義在 ItemTouchHelper 中的成員變量 mRecyclerView.addOnItemTouchListener(mOnItemTouchListener);ItemTouchHelper 相關源碼 :
public class ItemTouchHelper extends RecyclerView.ItemDecorationimplements RecyclerView.OnChildAttachStateChangeListener {private final OnItemTouchListener mOnItemTouchListener = new OnItemTouchListener() {}/*** Attaches the ItemTouchHelper to the provided RecyclerView. If TouchHelper is already* attached to a RecyclerView, it will first detach from the previous one. You can call this* method with {@code null} to detach it from the current RecyclerView.** @param recyclerView The RecyclerView instance to which you want to add this helper or* {@code null} if you want to remove ItemTouchHelper from the current* RecyclerView.*/public void attachToRecyclerView(@Nullable RecyclerView recyclerView) {if (mRecyclerView == recyclerView) {// 判定是否已經綁定 , 如果已經綁定 , 不再執行綁定方法 return; // nothing to do}if (mRecyclerView != null) {// 使用前 , 清空所有的回調 // 使用前重置 destroyCallbacks();}// 設置當前的 mRecyclerView 成員為綁定的 RecyclerView 列表mRecyclerView = recyclerView;if (recyclerView != null) {final Resources resources = recyclerView.getResources();mSwipeEscapeVelocity = resources.getDimension(R.dimen.item_touch_helper_swipe_escape_velocity);mMaxSwipeVelocity = resources.getDimension(R.dimen.item_touch_helper_swipe_escape_max_velocity);// 該方法是核心方法 // 為 ItemTouchHelper 綁定 ItemTouchHelper.Callback setupCallbacks();}}// 該方法與 destroyCallbacks 方法相對應private void setupCallbacks() {// 配置相關 ViewConfiguration vc = ViewConfiguration.get(mRecyclerView.getContext());mSlop = vc.getScaledTouchSlop();// 設置 RecyclerView 條目中的裝飾 , 可以在條目組件 底部 上層 繪制 Canvas 圖形 // ItemTouchHelper 繼承 RecyclerView.ItemDecorationmRecyclerView.addItemDecoration(this);// 添加了每個條目上的觸摸監聽器 mOnItemTouchListener // 該監聽器是定義在 ItemTouchHelper 中的成員變量 mRecyclerView.addOnItemTouchListener(mOnItemTouchListener);mRecyclerView.addOnChildAttachStateChangeListener(this);startGestureDetection();}// 該方法與 setupCallbacks 方法相對應// 清空所有的回調 , 重置 RecyclerView private void destroyCallbacks() {mRecyclerView.removeItemDecoration(this);mRecyclerView.removeOnItemTouchListener(mOnItemTouchListener);mRecyclerView.removeOnChildAttachStateChangeListener(this);// clean all attachedfinal int recoverAnimSize = mRecoverAnimations.size();for (int i = recoverAnimSize - 1; i >= 0; i--) {final RecoverAnimation recoverAnimation = mRecoverAnimations.get(0);mCallback.clearView(mRecyclerView, recoverAnimation.mViewHolder);}mRecoverAnimations.clear();mOverdrawChild = null;mOverdrawChildPosition = -1;releaseVelocityTracker();stopGestureDetection();} }2、ItemTouchHelper.setupCallbacks 方法分析
在 ItemTouchHelper.setupCallbacks 方法中 , 調用了
mRecyclerView.addItemDecoration(this);方法 , 為當前的 RecyclerView 設置條目裝飾 , 該裝飾可以在條目組件 底部 上層 繪制 Canvas 圖形 , 具體的方法如下 :
public class RecyclerView extends ViewGroup implements ScrollingView,NestedScrollingChild2, NestedScrollingChild3 {/*** Add an {@link ItemDecoration} to this RecyclerView. Item decorations can* affect both measurement and drawing of individual item views.** <p>Item decorations are ordered. Decorations placed earlier in the list will* be run/queried/drawn first for their effects on item views. Padding added to views* will be nested; a padding added by an earlier decoration will mean further* item decorations in the list will be asked to draw/pad within the previous decoration's* given area.</p>** @param decor Decoration to add* @param index Position in the decoration chain to insert this decoration at. If this value* is negative the decoration will be added at the end.*/public void addItemDecoration(@NonNull ItemDecoration decor, int index) {if (mLayout != null) {mLayout.assertNotInLayoutOrScroll("Cannot add item decoration during a scroll or"+ " layout");}if (mItemDecorations.isEmpty()) {setWillNotDraw(false);}// 將多個 ItemDecoration 添加到 mItemDecorations 集合中if (index < 0) {mItemDecorations.add(decor);} else {mItemDecorations.add(index, decor);}markItemDecorInsetsDirty();// 開始進行繪制 requestLayout();} }3、RecyclerView.ItemDecoration 源碼分析
ItemDecoration 是抽象類 , 核心邏輯必須由子類實現后才可以使用 ;
void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull State state) 方法是繪制方法 , 在該方法中調用了 void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent) 方法 ,
public abstract static class ItemDecoration {public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull State state) {onDraw(c, parent);}@Deprecatedpublic void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent) {}}參考 Android 官方提供的 RecyclerView.ItemDecoration 的實現類 DividerItemDecoration , 該類中重寫了 onDraw 方法 , 其中調用了 drawVertical 繪制垂直分割線 , 調用了 drawHorizontal 方法繪制水平分割線 ;
public class DividerItemDecoration extends RecyclerView.ItemDecoration {@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {if (parent.getLayoutManager() == null || mDivider == null) {return;}if (mOrientation == VERTICAL) {// 繪制垂直分割線drawVertical(c, parent);} else {// 繪制水平分割線drawHorizontal(c, parent);}} }在上述 drawVertical 和 drawHorizontal 方法中 , 利用 Canvas 進行繪圖 ;
在 RecyclerView.ItemDecoration 中 , 不僅僅只能繪制分割線 , 可以繪制任何圖形 , 圖片 , 顏色 , 與自定義組件繪制功能一樣強大 ;
三、博客資源
博客資源 :
- GitHub 地址 : https://github.com/han1202012/001_RecyclerView
總結
以上是生活随笔為你收集整理的【Android 事件分发】ItemTouchHelper 事件分发源码分析 ( 绑定 RecyclerView )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android 事件分发】ItemTo
- 下一篇: 【Google Play】创建并设置应用