这个机制是帮助我们处理滚动一些事件的,
但是从源码上我们可以思考,其实更好的说法应该是处理触摸事件,不仅仅滚动
因为其实里面是 子View 在回调给了 父View
- NestedScrollingParent
- NestedScrollingChild
- NestedScrollingParentHelper
- NestedScrollingChildHelper
NestedScrollingParent
子View一定要 setNestedScrollingEnabled 为true
按首次调用排序
方法:
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes);`
1 2 3
比如返回`(nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0` 说明可以接收垂直方向的事件,才会允许接下来的`NestedScrollingParent`的其余动作
onNestedScrollAccepted(View child, View target, int axes)这个方法一般是记录 axes
如果使用
NestedScrollingParentHelper可以用:下面方法1 2 3 4 5 6 7
/** * 一般记录 axes * */ @Override public void onNestedScrollAccepted(View child, View target, int axes) { nestedScrollingParentHelper.onNestedScrollAccepted(child , target ,axes); }getNestedScrollAxes1 2 3 4 5 6 7
这个方法一般用不到,但是自己重写制定View的时候 可以用到 @Override public int getNestedScrollAxes() { // return super.getNestedScrollAxes(); return nestedScrollingParentHelper.getNestedScrollAxes(); }onNestedPreScroll(target , dx , dy , consumed)1 2 3 4 5 6 7 8 9 10 11
1.target 目标 2.dx 滑动的x坐标 3.dy 滑动的y坐标 4.comsumed 需要消费的多少 通过名字就可以找到 **pre** 准备 就知道是准备滑动的时候 , 滚动前 通常是子Viwe `dispatchNestedPreScroll`调用
onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed)1 2 3 4 5
一般子view开始滚动的时候会触发该方法 , 滚动后 同理: 子View中 dispatchNestedScroll 参数:也不多做介绍
boolean super.onNestedPreFling(target , velocityX , velocityY)1 2 3
也是同理 返回子View中 `dispatchNestedPreFling` 这个方法是fling前 允不允许 返回true 运行在fling 否则不允许
super.onNestedPreFling(target , velocityX , velocityY)1 2
对应子View中 `dispatchNestedPreFling` 可以处理fling 事件
NestedScrollingChild
dispatchNestedFling(float velocityX, float velocityY, boolean consumed)
1向嵌套滚动父级派发一个投掷
dispatchNestedPreFling(float velocityX, float velocityY)
1在此视图处理它之前,将嵌套分派给嵌套的滚动父级。
dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow)
在该视图消耗其任何部分之前,调度正在进行的嵌套滚动的一个步骤。
dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow)
1分派正在进行的嵌套滚动的一个步骤。
hasNestedScrollingParent()
1如果此视图具有嵌套滚动父级,则返回true。
isNestedScrollingEnabled()
1如果对此视图启用嵌套滚动,则返回true。
setNestedScrollingEnabled(boolean enabled)
1启用或禁用此视图的嵌套滚动。
startNestedScroll(int axes)
1沿给定轴开始可嵌套滚动操作。
stopNestedScroll()
1停止正在进行的嵌套滚动。
NestedScrollingParentHelper
getNestedScrollAxes()
1返回此ViewGroup的嵌套滚动的当前轴。
onNestedScrollAccepted(View child, View target, int axes)
1当由ViewGroup接受由子视图启动的嵌套滚动操作时调用。
onNestedScrollAccepted(View child, View target, int axes, int type)
1当由ViewGroup接受由子视图启动的嵌套滚动操作时调用。
onStopNestedScroll(View target, int type)
1反应到嵌套滚动操作结束。
onStopNestedScroll(View target)
1反应到嵌套滚动操作结束。