Android NestedScrolling嵌套滾動(dòng)的示例代碼
一、什么是NestedScrolling?
Android在Lollipop版本中引入了NestedScrolling——嵌套滾動(dòng)機(jī)制。在Android的事件處理機(jī)制中,事件序列只能由父View和子View中的一個(gè)處理。在嵌套滾動(dòng)機(jī)制中,子View處理事件前會(huì)將事件傳給父View處理,兩者協(xié)作配合處理事件。
在嵌套滾動(dòng)機(jī)制中,父View需實(shí)現(xiàn)NestedScrollingParent接口,子View需要實(shí)現(xiàn)NestedScrollingChild接口。從Lollipop起View都已經(jīng)實(shí)現(xiàn)了NestedScrollingChild的方法。嵌套滾動(dòng)過程如下:
- 開始滾動(dòng)前,子View調(diào)用startNestedScroll方法。該方法會(huì)調(diào)用父View的onStartNestedScroll方法并返回onStartNestedScroll的值。如果返回true,則表示父View愿意接收后續(xù)的滾動(dòng)事件,此時(shí)父View的onNestedScrollAccepted會(huì)被調(diào)用。該方法一般是在子View處理DOWN事件時(shí)調(diào)用。
- 子View滾動(dòng)某個(gè)距離前,調(diào)用dispatchNestedPreScroll方法,把滾動(dòng)距離傳給父View。該方法回調(diào)父View的onNestedPreScroll方法,如果父View需要消耗滾動(dòng)距離,只需要把需要消耗的距離賦給onNestedPreScroll方法的參數(shù)consumed。該參數(shù)是一個(gè)數(shù)組,consumed[0]表示消耗的水平滾動(dòng)距離,consumed[1]表示消耗的垂直滾動(dòng)距離。dispatchNestedPreScroll返回true則表示父View消耗了部分或者全部滾動(dòng)距離。
- 子View滾動(dòng)某個(gè)距離后,調(diào)用dispatchNestedScroll方法。如果該方法返回true則表示,子View會(huì)調(diào)用父View的onNestedScroll方法,把已消耗和未消耗的滾動(dòng)距離傳給父View。
- 子View處理Fling事件前,調(diào)用dispatchNestedPreFling方法。該方法會(huì)調(diào)用父View的onNestedPreFling并返回onNestedPreFling的值。如果true,則表示父View處理消耗了該Fling事件,則子View不應(yīng)該處理該Fling事件。
- 如果dispatchNestedPreFling方法返回false,子View在處理Fling事件后會(huì)調(diào)用dispatchNestedFling方法,該方法會(huì)調(diào)用父View的onNestedFling方法。onNestedFling方法返回true表示父View消耗或處理了Fling事件。
- 當(dāng)子View停止?jié)L動(dòng)時(shí),調(diào)用stopNestedScroll方法。該方法會(huì)調(diào)用父View的onStopNestedScroll方法。
上面提及的各個(gè)方法的具體用法請參考官方文檔。
二、怎么實(shí)現(xiàn)NestedScrollingChild?
Android為NestedScrollingChild提供了一個(gè)代理類NestedScrollingChildHelper。所以,NestedScrollingChild的最簡單的實(shí)現(xiàn)如下。
public class NestedScrollingChildView extends FrameLayout implements NestedScrollingChild { private final NestedScrollingChildHelper mChildHelper; public NestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mChildHelper = new NestedScrollingChildHelper(this); } @Override public void setNestedScrollingEnabled(boolean enabled) { mChildHelper.setNestedScrollingEnabled(enabled); } @Override public boolean isNestedScrollingEnabled() { return mChildHelper.isNestedScrollingEnabled(); } @Override public boolean startNestedScroll(int axes) { return mChildHelper.startNestedScroll(axes); } @Override public void stopNestedScroll() { mChildHelper.stopNestedScroll(); } @Override public boolean hasNestedScrollingParent() { return mChildHelper.hasNestedScrollingParent(); } @Override public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) { return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow); } @Override public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) { return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow); } @Override public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) { return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed); } @Override public boolean dispatchNestedPreFling(float velocityX, float velocityY) { return mChildHelper.dispatchNestedPreFling(velocityX, velocityY); } }
然后,在適當(dāng)?shù)臅r(shí)機(jī)調(diào)用如下方法:
1.startNestedScroll
2.dispatchNestedPreScroll
3.dispatchNestedScroll
4.dispatchNestedPreFling
5.dispatchNestedFling
6.stopNestedScroll
三、怎么實(shí)現(xiàn)NestedScrollingParent?
Android為NestedScrollingParent提供了一個(gè)代理類NestedScrollingParentHelper。NestedScrollingParent的最簡單實(shí)現(xiàn)如下。
public class NestedScrollView extends FrameLayout implements NestedScrollingParent { private final NestedScrollingParentHelper mParentHelper; public NestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mParentHelper = new NestedScrollingParentHelper(this); } @Override public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { ... return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0; } @Override public void onNestedScrollAccepted(View child, View target, int axes) { mParentHelper.onNestedScrollAccepted(child, target, axes); ... } @Override public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { ... } @Override public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { ... } @Override public boolean onNestedPreFling(View target, float velocityX, float velocityY) { ... return false; } @Override public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { ... return false; } @Override public void onStopNestedScroll(View child) { mParentHelper.onStopNestedScroll(child); } @Override public int getNestedScrollAxes() { return mParentHelper.getNestedScrollAxes(); } }
四、NestedScrollingChildHelper的代碼分析
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; }
startNestedScroll方法從NestedScrollingChild向上查找愿意接收嵌套滾動(dòng)事件的父View,如果找到了則調(diào)用父View的onNestedScrollAccepted方法。ViewParentCompat是父View的兼容類,該類會(huì)判斷版本,如果在Lollipop及以上則調(diào)用View自帶的方法。否則,調(diào)用NestedScrollingParent的接口方法。
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) { if (isNestedScrollingEnabled() && mNestedScrollingParent != null) { if (dx != 0 || dy != 0) { int startX = 0; int startY = 0; if (offsetInWindow != null) { mView.getLocationInWindow(offsetInWindow); startX = offsetInWindow[0]; startY = offsetInWindow[1]; } if (consumed == null) { if (mTempNestedScrollConsumed == null) { mTempNestedScrollConsumed = new int[2]; } consumed = mTempNestedScrollConsumed; } consumed[0] = 0; consumed[1] = 0; ViewParentCompat.onNestedPreScroll(mNestedScrollingParent, mView, dx, dy, consumed); if (offsetInWindow != null) { mView.getLocationInWindow(offsetInWindow); offsetInWindow[0] -= startX; offsetInWindow[1] -= startY; } return consumed[0] != 0 || consumed[1] != 0; } else if (offsetInWindow != null) { offsetInWindow[0] = 0; offsetInWindow[1] = 0; } } return false; }
調(diào)用父View的onNestedPreScroll方法并記錄滾動(dòng)偏移量。參數(shù)offsetInWindow是一個(gè)長度為2的一位數(shù)組,記錄滾動(dòng)的偏移量,用來修改Touch事件的坐標(biāo),保證下次滾動(dòng)的準(zhǔn)確性。dispatchNestedScroll方法也同理。
五、舉個(gè)例子
實(shí)現(xiàn)一個(gè)簡單的NestedScrollingParent。該View包含一個(gè)頭部View和RecyclerView。RecyclerView已經(jīng)實(shí)現(xiàn)了NestedScrollingChild接口方法。向上滾動(dòng)時(shí),如果頭部沒有完全收起,則向上滾動(dòng)頭部。如果頭部收起才滾動(dòng)RecyclerView。向下滾動(dòng)時(shí),如果頭部收起,則向下滾動(dòng)頭部,否則滾動(dòng)RecyclerView。
public class HeaderLayout extends LinearLayout implements NestedScrollingParent { private NestedScrollingParentHelper mParentHelper; private int headerH; private ScrollerCompat mScroller; private boolean resetH = false; public HeaderLayout(Context context) { this(context, null); } public HeaderLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public HeaderLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mParentHelper = new NestedScrollingParentHelper(this); mScroller = ScrollerCompat.create(this.getContext()); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); headerH = getChildAt(0).getMeasuredHeight(); } @Override public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0; } @Override public void onNestedScrollAccepted(View child, View target, int axes) { mParentHelper.onNestedScrollAccepted(child, target, axes); } @Override public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { int scrollY = getScrollY(); if (dy > 0 && scrollY < headerH && scrollY >= 0) { int consumedY = Math.min(dy, headerH - scrollY); consumed[1] = consumedY; scrollBy(0, consumedY); if (!resetH) { resetH = true; int w = getWidth(); int h = getHeight() + headerH; setLayoutParams(new FrameLayout.LayoutParams(w, h)); } } else if (dy < 0 && scrollY == headerH) { consumed[1] = dy; scrollBy(0, dy); } else if (dy < 0 && scrollY < headerH && scrollY > 0) { int consumedY = Math.max(dy, -scrollY); consumed[1] = consumedY; scrollBy(0, consumedY); } } @Override public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { } @Override public boolean onNestedPreFling(View target, float velocityX, float velocityY) { int scrollY = getScrollY(); if (velocityY > 0 && scrollY < headerH && scrollY > 0) { if (!mScroller.isFinished()) { mScroller.abortAnimation(); } mScroller.fling(0, scrollY, (int)velocityX, (int)velocityY, 0, 0, 0, headerH); ViewCompat.postInvalidateOnAnimation(this); return true; } return false; } @Override public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { return false; } @Override public void onStopNestedScroll(View child) { mParentHelper.onStopNestedScroll(child); } @Override public int getNestedScrollAxes() { return mParentHelper.getNestedScrollAxes(); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android入門:廣播發(fā)送者與廣播接收者詳細(xì)介紹
本篇文章主要介紹了Android入門:廣播發(fā)送者與廣播接收者,詳細(xì)介紹了廣播收發(fā)的原理和代碼,有需要的可以了解一下。2016-11-11Android重寫View實(shí)現(xiàn)全新的控件
這篇文章主要介紹了Android重寫View來實(shí)現(xiàn)全新的控件,最難的一種自定義控件形式,感興趣的小伙伴們可以參考一下2016-05-05Android實(shí)現(xiàn)WebView刪除緩存的方法
這篇文章主要介紹了Android實(shí)現(xiàn)WebView刪除緩存的方法,實(shí)例分析了Android針對WebView操作緩存的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07android通過servlet服務(wù)器保存文件到手機(jī)
這篇文章主要為大家詳細(xì)介紹了android通過servlet服務(wù)器保存文件到手機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06android 實(shí)現(xiàn)APP中改變頭像圖片的實(shí)例代碼
這篇文章主要介紹了android 實(shí)現(xiàn)APP中改變頭像圖片的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07Android ViewPager實(shí)現(xiàn)智能無限循環(huán)滾動(dòng)回繞效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)智能無限循環(huán)滾動(dòng)回繞效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07