NestedScrollingChild和NestedScrollingParent

来源:互联网 发布:阿里云dns解析 编辑:程序博客网 时间:2024/05/18 03:02

NestedScrollingChild和NestedScrollingParent

记录在学习嵌套滑动中遇到的接口和使用方法。


滑动动作是 Child 主动发起,Parent 就收滑动回调并作出响应。
NestedScrollingChild接口的实现,基本上就是调用 Helper(NestedScrollingChildHelper) 类的对应的函数即可,因为 Helper 类中已经实现好了 Child 和 Parent 交互的逻辑。

准备工作:
当Child要滑动的时候,调用startNestedScroll()方法,通过Helper去通知Parent,此时Parent调用了onStartNestedScroll(),并返回true同意Child一同滑动的请求。随后,Parent调用onNestedScrollAccepted(),来做接受滑动且滑动之前的准备工作,可以直接使用Helper(NestedScrollingParentHelper)类帮我们做处理。

滑动:
每次滑动前,Child首先调用dispatchNestedPreScroll()方法来询问Parent是否需要先于Child滑动,与之对应的是Parent的onNestedPreScroll()方法,在这个方法中,Parent可以做处理,拦截Child自身的滑动,并先于Child滑动。
如果Parent不需要先于Child滑动,Child继续调用dispatchNestedScroll(),对应到Parent的onNestedScroll()方法,这时Parent是在Child滑动并处理完事情后再做处理。
上面两种处理方法都是通过Parent消费滑动距离来处理滑动事件,即给[]consumed赋值,该值是Parent滑动的距离,consumed[0]是x轴,consumed[1]是y轴。
滑动结束后,Parent调用onStopNestedScroll()结束本次滑动。

滑行:
原理和滑动差不多,在滑行之前,Child调用dispatchNestedPreFling(),并通过Helper通知Parent要滑行了,询问Parent要不要在onNestedPreFling()中先行处理Fling事件。

**补充笔记
RecyclerView获取滑动距离
只限于使用LinearLayoutManager的时候

public int getScollYDistance() {      LinearLayoutManager layoutManager = (LinearLayoutManager) this.getLayoutManager();      int position = layoutManager.findFirstVisibleItemPosition();      View firstVisiableChildView = layoutManager.findViewByPosition(position);      int itemHeight = firstVisiableChildView.getHeight();      return (position) * itemHeight - firstVisiableChildView.getTop();  } 
0 0
原创粉丝点击