NestedScrolling机制(能够称为嵌套滚动或嵌套滑动)可以让父view和子view在滚动时进行配合,其基本流程以下:spa
要实现这样的交互,父View须要实现NestedScrollingParent接口,而子View须要实现NestedScrollingChild接口。.net
在这套交互机制中,child是动做的发起者,parent只是接受回调并做出响应。code
另外: 父view和子view并不须要是直接的父子关系,即若是“parent1包含parent2,parent2包含child”,则parent1和child仍能经过nestedScrolling机制进行交互。blog
关于动做的方法大概能够分红两类继承
第一类:主要实现通用的的关联动做,各类view的改变均可以使用。接口
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency)
第二类:主要实现滚动的动做ci
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) public void onNestedScrollAccepted(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target) public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) public boolean onNestedFling(CoordinatorLayout coordinatorLayout, View child, View target, float velocityX, float velocityY, boolean consumed) public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, View child, View target, float velocityX, float velocityY)
方法之间的具体对应关系以下:get
子(发起者) | 父(被回调) |
---|---|
startNestedScroll | onStartNestedScroll、onNestedScrollAccepted |
dispatchNestedPreScroll | onNestedPreScroll |
dispatchNestedScroll | onNestedScroll |
stopNestedScroll | onStopNestedScroll |
流程源码
能够大体将嵌套滚动的流程归纳以下(以触摸滚动为例,惯性滚动(fling)的流程与此相似):it
其余
一、要关联滚动的View须要实现NestedScrollingChild接口,并在用NestedScrollingChildHelper辅助类处理相应的回调 二、须要关联滚动的View的父View须要实现NestedScrollingParent接口,可用NestedScrollingParentHelper辅助类处理相应的回调,并在相应的回调方法中处理动做 三、自定义Behavior要继承CoordinatorLayout.Behavior<V extends View> 四、CoordinatorLayout的直接子类设置layout_behavior属性才有效果(CoordinatorLayout会循环调用每一个子view的behavior)
参考:
NestedScrolling机制(一)——概述 http://blog.csdn.net/al4fun/article/details/53888990 源码看CoordinatorLayout.Behavior原理 http://blog.csdn.net/qibin0506/article/details/50377592