Android NestedScrolling嵌套滾動的示例代碼
一、什么是NestedScrolling?
Android在Lollipop版本中引入了NestedScrolling——嵌套滾動機制。在Android的事件處理機制中,事件序列只能由父View和子View中的一個處理。在嵌套滾動機制中,子View處理事件前會將事件傳給父View處理,兩者協(xié)作配合處理事件。
在嵌套滾動機制中,父View需實現(xiàn)NestedScrollingParent接口,子View需要實現(xiàn)NestedScrollingChild接口。從Lollipop起View都已經(jīng)實現(xiàn)了NestedScrollingChild的方法。嵌套滾動過程如下:
- 開始滾動前,子View調用startNestedScroll方法。該方法會調用父View的onStartNestedScroll方法并返回onStartNestedScroll的值。如果返回true,則表示父View愿意接收后續(xù)的滾動事件,此時父View的onNestedScrollAccepted會被調用。該方法一般是在子View處理DOWN事件時調用。
- 子View滾動某個距離前,調用dispatchNestedPreScroll方法,把滾動距離傳給父View。該方法回調父View的onNestedPreScroll方法,如果父View需要消耗滾動距離,只需要把需要消耗的距離賦給onNestedPreScroll方法的參數(shù)consumed。該參數(shù)是一個數(shù)組,consumed[0]表示消耗的水平滾動距離,consumed[1]表示消耗的垂直滾動距離。dispatchNestedPreScroll返回true則表示父View消耗了部分或者全部滾動距離。
- 子View滾動某個距離后,調用dispatchNestedScroll方法。如果該方法返回true則表示,子View會調用父View的onNestedScroll方法,把已消耗和未消耗的滾動距離傳給父View。
- 子View處理Fling事件前,調用dispatchNestedPreFling方法。該方法會調用父View的onNestedPreFling并返回onNestedPreFling的值。如果true,則表示父View處理消耗了該Fling事件,則子View不應該處理該Fling事件。
- 如果dispatchNestedPreFling方法返回false,子View在處理Fling事件后會調用dispatchNestedFling方法,該方法會調用父View的onNestedFling方法。onNestedFling方法返回true表示父View消耗或處理了Fling事件。
- 當子View停止?jié)L動時,調用stopNestedScroll方法。該方法會調用父View的onStopNestedScroll方法。
上面提及的各個方法的具體用法請參考官方文檔。
二、怎么實現(xiàn)NestedScrollingChild?
Android為NestedScrollingChild提供了一個代理類NestedScrollingChildHelper。所以,NestedScrollingChild的最簡單的實現(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);
}
}
然后,在適當?shù)臅r機調用如下方法:
1.startNestedScroll
2.dispatchNestedPreScroll
3.dispatchNestedScroll
4.dispatchNestedPreFling
5.dispatchNestedFling
6.stopNestedScroll
三、怎么實現(xiàn)NestedScrollingParent?
Android為NestedScrollingParent提供了一個代理類NestedScrollingParentHelper。NestedScrollingParent的最簡單實現(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向上查找愿意接收嵌套滾動事件的父View,如果找到了則調用父View的onNestedScrollAccepted方法。ViewParentCompat是父View的兼容類,該類會判斷版本,如果在Lollipop及以上則調用View自帶的方法。否則,調用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;
}
調用父View的onNestedPreScroll方法并記錄滾動偏移量。參數(shù)offsetInWindow是一個長度為2的一位數(shù)組,記錄滾動的偏移量,用來修改Touch事件的坐標,保證下次滾動的準確性。dispatchNestedScroll方法也同理。
五、舉個例子
實現(xiàn)一個簡單的NestedScrollingParent。該View包含一個頭部View和RecyclerView。RecyclerView已經(jīng)實現(xiàn)了NestedScrollingChild接口方法。向上滾動時,如果頭部沒有完全收起,則向上滾動頭部。如果頭部收起才滾動RecyclerView。向下滾動時,如果頭部收起,則向下滾動頭部,否則滾動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();
}
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
android 實現(xiàn)APP中改變頭像圖片的實例代碼
這篇文章主要介紹了android 實現(xiàn)APP中改變頭像圖片的實例代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
Android ViewPager實現(xiàn)智能無限循環(huán)滾動回繞效果
這篇文章主要為大家詳細介紹了Android ViewPager實現(xiàn)智能無限循環(huán)滾動回繞效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

