目前已实现改接口的类包括: CoordinatorLayout, NestedScrollView, SwipeRefreshLayout。它通常是配合 NestedScrollingChild 进行嵌套滑动的。
- boolean onStartNestedScroll(View child, View target, int nestedScrollAxes)
在 Scrolling Child 开始滑动的时候会调用这个方法
当 Scrolling Child 调用 onStartNestedScroll 方法的时候,通过 NestedScrollingChildHelper 会回调 Scrolling parent 的 onStartNestedScroll 方法,如果返回 true, Scrolling parent 的 onNestedScrollAccepted(View child, View target, int nestedScrollAxes) 方法会被回调。
target 表示发起滑动事件的 View,Child 是 ViewParent 的直接子View,包含 target,nestedScrollAxes 表示滑动方向。
- void onNestedScrollAccepted(View child, View target, int nestedScrollAxes)
如果 Scrolling Parent 的onStartNestedScroll 返回 true, Scrolling parent 的 onNestedScrollAccepted(View child, View target, int nestedScrollAxes) 方法会被回调。
- boolean onNestedPreScroll(View target, int dx, int dy, int[] consumed)
在 Scrolling Child 进行滑动之前,Scrolling Parent 可以先于Scrolling Child 进行相应的处理
如果 Scrolling Child 调用 dispatchNestedPreFling(float velocityX, float velocityY) ,通过 NestedScrollingChildHelper 会回调 Scrolling parent 的 onNestedPreScroll 方法
接下来的几个方法,我们不一一介绍了。与 Scrolling Child 方法几乎是一一对应的。
[](
)NetsedScrollingChildHelper相关方法
RecyclerView实现了NestedScrollingChild接口,因此我们以RecyclerView为例,详细探究NetsedScrollingChildHelper的具体应用
public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
...
mScrollingChildHelper = new NestedScrollingChildHelper(this);
setNestedScrollingEnabled(true);
}
...
@Override
public void setNestedScrollingEnabled(boolean enabled) {
mScrollingChildHelper.setNestedScrollingEnabled(enabled);
}
@Override
public boolean isNestedScrollingEnabled() {
return mScrollingChildHelper.isNestedScrollingEnabled();
}
@Override
public boolean startNestedScroll(int axes) {