NestedScrollingChild学习之一

重要的几个接口方法:

boolean startNestedScroll(@ScrollAxis int axes);
boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed,
@Nullable int[] offsetInWindow);
boolean dispatchNestedScroll(int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow);

我们先学习下startNestedScroll这个方法的官方注释, 可以说这个注释说明了一切.
/**
* Begin a nestable scroll operation along the given axes.
在一个方向上开始一次嵌套滑动操作. (竖向或者横向)
ViewCompa.SCROLL_AXIS_HORIZONTAL -- 横向的我没经历过.
ViewCompa.SCROLL_AXIS_VERTICAL
*
*

A view starting a nested scroll promises to abide by the following contract:


一个view开始一次嵌套滑动, 需要遵循如下的约定:
*

The view will call startNestedScroll upon initiating a scroll operation. In the case
* of a touch scroll this corresponds to the initial {@link MotionEvent#ACTION_DOWN}.
* In the case of touch scrolling the nested scroll will be terminated automatically in
* the same manner as {@link ViewParent#requestDisallowInterceptTouchEvent(boolean)}.
* In the event of programmatic scrolling the caller must explicitly call
* {@link #stopNestedScroll()} to indicate the end of the nested scroll.


view必须在开始一次滑动操作之前调用startNestedScroll , 就一次触摸事件而言, 也就是在ACTION_DOWN事件时. 在触摸滑动时, 嵌套滑动被自动终止, 就像requestDisallowInterceptTouchEvent() 那样. (这里没看懂, 请看原文)
再一次典型的滑动过程中, 调用者(也就是实现了NestedScrollingChild接口的view) 必须调用stopNestedScroll() 来标志一次嵌套滑动的结束.

 * 

If startNestedScroll returns true, a cooperative parent was found. * If it returns false the caller may ignore the rest of this contract until the next scroll. * Calling startNestedScroll while a nested scroll is already in progress will return true.

如果startNestedScroll()返回true, 说明找到了一个cooperative parent, 也就是在当前view之上, 有根布局控件, 实现了NestedScrollingParent.
如果返回false, 调用者可以忽略后续的所有约定, 知道下一次嵌套滑动事件被触发.
另外, 如果在一次嵌套滑动过程中, 调用startNestedScroll(), 应该返回true .

 * 

At each incremental step of the scroll the caller should invoke * {@link #dispatchNestedPreScroll(int, int, int[], int[]) dispatchNestedPreScroll} * once it has calculated the requested scrolling delta. If it returns true the nested scrolling * parent at least partially consumed the scroll and the caller should adjust the amount it * scrolls by.

在每一步的增量滑动过程中,一旦caller(比如说RecyclerView) 计算好了这一次的需要滑动的数值delta. caller应该调用dispatchNestedPreScroll(int, int, int[], int[])

如果返回true,( 这个返回值其实是由NestedScrollingParent来计算并决定的.) 说明当前的嵌套滑动parent消耗了一点滑动值, caller需要根据consumed值进行调整自身需要滑动的距离值. 如果parent把所有值都消耗了, 那caller自身可能就不需要滑动了. 但是同时还需要关注返回的offsetInWindow参数, 需要根据这个值调整caller自身的位置.

 * 

After applying the remainder of the scroll delta the caller should invoke * {@link #dispatchNestedScroll(int, int, int, int, int[]) dispatchNestedScroll}, passing * both the delta consumed and the delta unconsumed. A nested scrolling parent may treat * these values differently. See * {@link NestedScrollingParent#onNestedScroll(View, int, int, int, int)}. *

caller进行完自身滑动之后, 需要调用dispatchNestedScroll() 再次通知parent, 把caller自身消耗的值, 剩余没有消耗的值, 再次告诉parent, 也许parent会进行处理(也许会再滑动什么的)

 * @param axes Flags consisting of a combination of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL}
 *             and/or {@link ViewCompat#SCROLL_AXIS_VERTICAL}.
 * @return true if a cooperative parent was found and nested scrolling has been enabled for
 *         the current gesture.
 *
 * @see #stopNestedScroll()
 * @see #dispatchNestedPreScroll(int, int, int[], int[])
 * @see #dispatchNestedScroll(int, int, int, int, int[])
 */

你可能感兴趣的:(NestedScrollingChild学习之一)