Android 嵌套布局
简介:
1. NestedScrollingParent接口
① onStartNestedScroll(View child, View target, int nestedScrollAxes):当nestedChild想要进行嵌套滚动时,会调用nestedParent的这个方法。这个芳法用于指示是否支持嵌套滚动,比如我们只想支持垂直方向上的嵌套滚动,可以在nestedParent中这样实现这个方法:
@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
if (nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL) {
return true;
}
return false;
}
② onNestedPreScroll(View target, int dx, int dy, int[] consumed)
当我们滚动nestedChild时,nestedChild进行实际的滚动前,会先调用nestParent的这个方法。nestedParent在这个方法中可以把子View想要滚动的距离消耗掉一部分或是全部消耗,比如我们的例子中,当我们向上滚动nestedChild时,nestParent会抢在它前头先滚动,直到ImageView完全隐藏,才让nestedChild开始滚动
在onNestedPreScroll()方法中,参数dy代表了本次NestedScrollView想要滑动的距离。若我们向上滑动NestedScrollView,dy就是正的,向下就是负的。getScorllY()会返回ParentView的mScrollY参数,为正则表示当前ParentView的内容已经向上滚动了一段距离,否则表示向下滚动过一段距离。
- NestedScrollingChild接口
① startNestedScroll(int axes)
开始沿着参数中指定的方向(水平 or 垂直)进行嵌套滚动
② dispatchNestedPreScroll(…)
这个方法会调用nestedParent的onNestedPreScroll()方法。这样就使得nestedParent有机会抢在NestedScroll之前消耗滚动事件
文章推荐:
http://blog.youkuaiyun.com/lmj623565791/article/details/52204039
https://segmentfault.com/a/1190000006657044
https://segmentfault.com/a/1190000006665225
https://segmentfault.com/a/1190000006666005
http://blog.youkuaiyun.com/yanzhenjie1003/article/details/52205665