雖然很早之前使用CoordinatorLayout
時就認(rèn)識過nestedScrollingChild
和nestedScrollingParent
, 也看多很多博客,但每次看著就不知所云了,所以這篇文章,我們就以問題為線索,帶著問題找答案。
1. 誰實現(xiàn) NestedScrollingChild,誰實現(xiàn)NestedScrollingParent ?
在實際項目中,我們往往會遇到這樣一種需求,當(dāng)ViewA
還顯示的時候,往上滑動到viewA
不可見時,才開始滑動viewB
, 又或者向下滑動到viewB
不能滑動時,才開始向上滑動viewC
. 如果列表滑動、上拉加載和下拉刷新的view
都封裝成一個組件的話,那滑動邏輯就是剛剛這樣。而這其中列表就要實現(xiàn)nestedScrollingChild
, 最外層的Container
實現(xiàn)nestedScrollingParent
. 如果最外層的Container
希望在其它布局中仍然能夠?qū)⒒瑒邮录^續(xù)往上冒泡,那么container
在實現(xiàn)nestedScrollingParent
的同時也要實現(xiàn)nestedScrollingChild
。 如下示意圖所示。
所以這個問題的答案:
觸發(fā)滑動的組件或者接受到滑動事件且需要繼續(xù)往上傳遞的是nestedScrollingChild
.
是nestedScrollingChild
的父布局,且需要消費傳遞的滑動事件就是nestedScrollingParent
.
我們今天的最后也會給出如何利用nestedScrollingChild
和nestedScrollingParent
來自定義一個集上拉加載和下拉刷新的組件。
2. 滑動事件如何在二者之間傳遞和消費的?
2.1 你能一眼認(rèn)出這是child
還是parent
的api
嗎?
首先呢,我們要看一下nestedScrollingChild
和nestedScrollingParent
有哪些api
.
public interface NestedScrollingChild {
public void setNestedScrollingEnabled(boolean enabled);
public boolean isNestedScrollingEnabled();
public boolean startNestedScroll(int axes);
public void stopNestedScroll();
public boolean hasNestedScrollingParent();
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow);
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow);
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed);
public boolean dispatchNestedPreFling(float velocityX, float velocityY);
}
public interface NestedScrollingParent {
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes);
public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes);
public void onStopNestedScroll(View target);
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed);
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed);
public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed);
public int getNestedScrollAxes();
}
這里呢,我刪掉了注釋,我不想翻譯那些注釋放在上面的代碼中,這樣你就會將注意力放在我的注釋中,然后陷入了咬文嚼字,最后感嘆為什么每個字我都認(rèn)識,連在了一起我怎么就看不懂的自我否定中。 之所以把上面的代碼放出來,是為了我后面簡述時,你不用一邊看文章,一邊還要切過去看源碼看這個api
是屬于child
還是屬于parent
的。
其實這里給你一個分辨是child
和parent
的api
的一個小訣竅,因為child
是產(chǎn)生滑動的造勢者,所以它的api
都是以直接的動詞開頭,而parent
的滑動響應(yīng)是child
通知parent
的,所以都是以監(jiān)聽on
開頭,這樣就記住了。
parent
----> onXXXX()
child
-----> verbXXXX()
嗯,廢話了好像很多了,這里我們要回到問題上,滑動事件如何在child
和parent
之間傳遞和消費掉的呢?
2.2 滑動事件的是如何傳遞的
那既然能傳遞,說明這個滑動事件一定產(chǎn)生了,如何產(chǎn)生滑動事件?當(dāng)然是用戶手指在屏幕上滑動了呀。為了不說的這么枯燥,我們拿最熟悉熟悉的小伙伴RecyclerView
來作為nestedScrollingChild
講解。這里我引入的版本是:25.3.1
implementation 'com.android.support:recyclerview-v7:25.3.1'
2.2.1 滑動事件傳遞從哪里產(chǎn)生?
switch (action) {
case MotionEvent.ACTION_DOWN:
int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE;
if (canScrollHorizontally) {
nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL;
}
if (canScrollVertically) {
nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL;
}
startNestedScroll(nestedScrollAxis);
}
break;
}
這里,我們可以發(fā)現(xiàn),當(dāng)我的小手按在RecyclerView
上時,調(diào)用了nestedScrollingChild
的startNestedScroll(nestedScrollAxis)
, 這里我們再多讓我們的大腦接受一點信息,那就是這個方法的參數(shù):nestedScrollAxis
, 滑動的坐標(biāo)軸。 RecylerView
是不是既可以水平滑動,又可以縱向滑動,那這里就是傳遞的就是RecyclerView
可以滑動的坐標(biāo)軸。
發(fā)現(xiàn)了startNestedScroll(axis)
,看看走到了哪里。
@Override
public boolean startNestedScroll(int axes) {
return getScrollingChildHelper().startNestedScroll(axes);
}
這里,我們發(fā)現(xiàn)又出來一個類:NestedScrollingChildHelper
. 里面好像又有一些滑動的api
。讀到這里,不要怕,心態(tài)要穩(wěn)住,不要崩塌了。給自己吃顆定心丸,我能行。
我們先看一眼,這個childHelper
的這個方法干了啥?
public boolean startNestedScroll(int axes) {
if (hasNestedScrollingParent()) {
// Already in progress
return true;
}
if (isNestedScrollingEnabled()) {
ViewParent p = mView.getParent();
View child = mView;
while (p != null) {
if (ViewParentCompat.onStartNestedScroll(p, child, mView, axes)) {
mNestedScrollingParent = p;
ViewParentCompat.onNestedScrollAccepted(p, child, mView, axes);
return true;
}
if (p instanceof View) {
child = (View) p;
}
p = p.getParent();
}
}
return false;
}
所有的絕妙之處就在這個方法中,這個方法我是原封不動拷貝下來,聽我和你一句一句講解。
第一句: 判斷 mNestedScrollingParent
是不是 null
。 在NestedScrollingChildHelper
這個類,全類只有兩處給它賦值了,一個賦有值,就是上面代碼中的while
循環(huán)里面,一個是賦空值,在方法stopNestedScroll
,這個方法什么時候調(diào)用啊,在你美麗的小手離開屏幕的時候。所以只要你的小手在屏幕上,這個startedNestedScroll
這個方法只會調(diào)用一次。也就是通知parent
我美麗的小手指要滑動啦,通知過你,我就不通知了,哪個小仙女不是傲嬌的。
第二句: 判斷mIsNestedScrollingEnabled
是否要true
. 這個變量也是至關(guān)重要的,它的作用是 要不要向上冒泡滑動事件,所以說哪天小仙女不開心了,直接調(diào)用了:setNestedScrollingEnabled(false)
, 父布局是怎么都不知道小手指有沒有滑動的。
第三句+第四句:這里的p
就是父布局了,這里的mView
是在初始化這個類的時候,傳遞過來的,所以在RecyclerView
中,可以找到這句話:mScrollingChildHelper = new NestedScrollingChildHelper(this);
. 這里的mView
就是RecyclerView
這位小仙女啦。
第五句:進(jìn)入while
循環(huán)了,為什么這里要while
循環(huán),因為它要確保使命必達(dá),不管我的父布局有多深,我都要找到你,并通知到你。
第六句:if
里的邏輯說明,如果parent
監(jiān)聽到即將要在這個軸上有滑動事件,并且正是parent
需要的事件,那么就會調(diào)用onNestedScrollAccept
。 這里的ViewParentCompat.onStartNestedScroll(p, child, mView, axes)
會最終調(diào)用到實現(xiàn)nestedScrollingParent
組件中的onStartNestedScroll
方法,這個方法就是parent
判斷收到該滑動通知時,是不是天時地利人和,如果是,我就返回true
,后面一系列的小手指滑動都要告知我。如果返回false
,說明parent
此時在處理別的事情,后面小手指滑動的弧線再怎么優(yōu)美,都不要來煩我。
第七句:onNestedScrollAccepted
說明parent
正式接收了此child
也就是recyclerView
的滑動通知,最終會調(diào)用到parent
的onNestedScrollAccept
方法中,如果此parent
還實現(xiàn)了接口nestedScrollingChild
, 可以在這個方法繼續(xù)向parent
的parent
上報了。
所以整個流程可以概括為:通知
ACTION_DOWN
--> child.startNestedScroll
--> childHelper.startNestedScroll
--> parent.onStartNestedScroll
--> parent.onNestedScrollAccept
2.2.2 小手指滑動的時候,child
和parent
之間是如何通信的?
case MotionEvent.ACTION_MOVE: {
if (dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset)) {
dx -= mScrollConsumed[0];
dy -= mScrollConsumed[1];
vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]);
}
if (mScrollState == SCROLL_STATE_DRAGGING) {
mLastTouchX = x - mScrollOffset[0];
mLastTouchY = y - mScrollOffset[1];
if (scrollByInternal(canScrollHorizontally ? dx : 0,canScrollVertically ? dy : 0,
vtev)) {
getParent().requestDisallowInterceptTouchEvent(true);
}
break;
}
這里呢,有兩個重要的方法:dispatchNestedPreScroll
和 scrollByInternal
.
第一句: dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)
,這里的參數(shù)中只有dx
,dy
兩個參數(shù)在前面賦值了,而后面兩個參數(shù)在哪里操作的呢?這里我們留個問號?首先這個方法會走到NestedScrollingChildHelper
類中的方法:dispatchNestedPreScroll
調(diào)用ViewParentCompat.onNestedPreScroll(mNestedScrollingParent, mView, dx, dy, consumed);
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
ViewParentCompat.onNestedPreScroll(mNestedScrollingParent, mView, dx, dy, consumed);
}
最終目的地來到了parent
的onNestedPreScroll()
。所以我們可以大膽猜測,consumed
, offsetInwindow
, 是在parent
這里賦值的,當(dāng)然你可以不用賦值,不賦值的話,值也就是保留上一次的值。
dx -= mScrollConsumed[0];
dy -= mScrollConsumed[1];
dispatchNestedPreScroll()
這個方法返回true
后,發(fā)現(xiàn)重新計算了dx
,dy
, 在方法scrollByInternal()
方法中,用的是最新的dx,dy
值。說明當(dāng)小手指產(chǎn)生滑動位移的時候,先分發(fā)給parent
,讓parent
先消耗,并在方法中將parent
消耗的位移傳遞過來,那么剩下的位移,ok,那充當(dāng)child
的RecyclerView
內(nèi)部消費了。
if (scrollByInternal(canScrollHorizontally ? dx : 0,canScrollVertically ? dy : 0,vtev)) {
getParent().requestDisallowInterceptTouchEvent(true);
}
boolean scrollByInternal(int x, int y, MotionEvent ev) {
int unconsumedX = 0, unconsumedY = 0;
int consumedX = 0, consumedY = 0;
if (mAdapter != null) {
if (y != 0) {
consumedY = mLayout.scrollVerticallyBy(y, mRecycler, mState);
unconsumedY = y - consumedY;
}
}
if (dispatchNestedScroll(consumedX, consumedY, unconsumedX, unconsumedY, mScrollOffset)) {
// Update the last touch co-ords, taking any scroll offset into account
mLastTouchX -= mScrollOffset[0];
mLastTouchY -= mScrollOffset[1];
if (ev != null) {
ev.offsetLocation(mScrollOffset[0], mScrollOffset[1]);
}
mNestedOffsets[0] += mScrollOffset[0];
mNestedOffsets[1] += mScrollOffset[1];
}
return consumedX != 0 || consumedY != 0;
}
第二句:scrollByInternal()
就是內(nèi)部滑動消耗了,在這個方法里面,我們發(fā)現(xiàn)繼續(xù)往parent
分發(fā)了事件:dispatchNestedScroll(consumeX, consumeY, unconsumeX, unconsumeY)
, 把自己未消耗的滑動位移繼續(xù)移交給parent
,這個時候最終會走到parent
的方法:onNestedScroll()
。 在這里,如果parent
還實現(xiàn)了nestedScrollingChild
,可以將未消耗的滑動位移繼續(xù)移交給自己的parent
.
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
if(isNestedScrollingEnabled()) {
dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, mParentOffsetInWindow);
}
}
所以我們可以總結(jié)如下:通信
ACTION_MOVE
: 小手指滑動位移為:dy
--> childHelper.dispatchNestedPreScroll
(dy)
--> parent.onNestedPreScroll(dy)
, consumedY = parent.onNestedPreScroll(dy)
--> dy' = dy - consumeY
recyclerView.scrollByInternal(dy')
unconsumeY = dy' - recyclerView.scrollByInternal(dy')
--> parent.startNestedScroll(unconsumeY)
2.2.3 小手指滑累了,離開屏幕時,又有哪些事件傳遞?
case MotionEvent.ACTION_UP: {
if (!((xvel != 0 || yvel != 0) && fling((int) xvel, (int) yvel))) {
setScrollState(SCROLL_STATE_IDLE);
}
resetTouch();
}
break;
public boolean fling(int velocityX, int velocityY) {
if (!dispatchNestedPreFling(velocityX, velocityY)) {
final boolean canScroll = canScrollHorizontal || canScrollVertical;
dispatchNestedFling(velocityX, velocityY, canScroll);
if (canScroll) {
mViewFlinger.fling(velocityX, velocityY);
return true;
}
}
return false;
}
private void resetTouch() {
stopNestedScroll();
}
這里我們發(fā)現(xiàn)先是child
執(zhí)行fling
方法,也就是當(dāng)手松開時仍然有速度,那么會執(zhí)行一段慣性滑動,而在這慣性滑動中, 這里就很奇妙了,先是通過dispatchNestedPreFling()
將滑動速度傳遞給parent
, 如果parent
不消耗的話,再次通過dispatchNestedFling
向parent
傳遞,只是這次的傳遞會帶上child
自己是否有能力消費慣性滑動,最后不管parent
有沒有消費,child
也就是recyclerview
都會執(zhí)行自己的fling
.也就是:
mViewFlinger.fling(velocityX, velocityY);
走完了慣性滑動,就會走到stopNestedScroll()
. 按照上面的邏輯處理,我們應(yīng)該可以猜到接下來的邏輯就是走到NestedScrollingChildHelper
這個類。然后目的地會到達(dá)parent
的onStopNestedScroll
方法。這里,parent
就可以處理當(dāng)小手指離開屏幕時的一些邏輯了。這條路很簡單,沒有返回值,也沒有傳遞什么變量。還是很好理解的。
public void stopNestedScroll() {
if (mNestedScrollingParent != null) {
ViewParentCompat.onStopNestedScroll(mNestedScrollingParent, mView);
mNestedScrollingParent = null;
}
}
這里呢,我們可以總結(jié)如下:收尾
ACTION_UP
--> childHelper.dispatchNestedPreFling
--> parent.onNestedPreFling
--> childHelper.dispatchNestedFling
--> parent.onNestedFling
--> child.fling
--> childHelper.stopNestedScroll
--> parent.onStopNestedScroll
這樣,我們整個nestedScrollingChild
和nestedScrollingParent
之間的絲絲縷縷都講解完了。
3. 實踐
這里我們利用nestedScrollingChild
和nestedScrollingParent
實現(xiàn)的自定義上拉加載,下拉刷新的控件。