为何要进行嵌套滑动设计

● 从 Android 5.0 Lollipop 开始提供一套来支持嵌入的滑动效果,同样在最新的 Support V4 包中也提供了前向的兼容,有了嵌入滑动机制,就能实现很多很复杂的滑动效果,在 Android Design Support 库中非常重要的CoordinatorLayout 组件就是使用了这套机制,实现了 Toolbar 的收起和展开功能
● 看起来像带有 header 的 RecyclerView 在滑动,但其实是嵌套滑动
● layout_scrollFlags 和 layout_behavior 有很多可选值,配合起来可以实现多种效果,不只限于嵌套滑动。具体可以参考 API 文档。
● 使用 CoordinatorLayout 实现嵌套滑动比手动实现要好得多,既可以实现连贯的吸顶嵌套滑动,又支持 fling。而且是官方提供的布局,可以放心使用,出 bug 的几率很小,性能也不会有问题。不过也正是因为官方将其封装得很好,使用 CoordinatorLayout 很难实现比较复杂的嵌套滑动布局,比如多级嵌套滑动
● NestedScrolling提供了一套父 View 和子 View 滑动交互机制。要完成这样的交互,父 View 需要实现 NestedScrollingParent 接口,而子 View 需要实现 NestedScrollingChild 接口
嵌套滑动与事件分发机制
● 事件分发机制:子View首先得到事件处理权,处理过程中父View可以对其拦截,但是拦截了以后就无法再还给子View(本次手势内)
● NestedScrolling 滑动机制:内部View在滚动的时候,首先将dx,dy交给NestedScrollingParent,NestedScrollingParent可对其进行部分消耗,剩余的部分还给内部View
● 总结:嵌套布局要注意的有几个方面
● ACTION_DOWN 时子view调用父布局的onStartNestedScroll,根据滑动方向判断父布局是否要收到子view的滑动参数
● ACTION_MOVE时子view调用父布局的onNestedPreScroll函数,父布局是否要滑动已经消费掉自身需要的距离
● ACTION_UP时,手指抬起可能还有加速度,调用父布局的onPreFling判断是否需要消费以及消费剩下的再传给子布局
嵌套滑动冲突
● 2个同向滑动的RecyclerView相互嵌套,进行滑动时发生滑动冲突。例如,Viewpager2内部包含了另一个ViewPager2或一个横向滑动的recyclerView。这时,在横向滑动时,滑动的可能是外部的ViewPager2,也可能是内部的RecyclerView
● 滑动冲突的原因:外部ViewPager2和内部RecyclerView的滑动阈值不同造成滑动冲突。ViewPager2的滑动阈值较高,内部RecyclerView的滑动阈值较低。具体来说,在触摸事件分发时,ViewPager2会先尝试对事件进行拦截,若滑动的距离大于了ViewPager2的滑动阈值,则ViewPager2会对此事件进行拦截,即给之前处理事件的子视图发一个Cancel通知,然后自己去处理后继的所有事件
● 比如,对于包含有RecyclerView的ViewPager2来说,由于滑动距离的增长快慢不同,会导致不同的视图处理滑动事件;1、滑动距离增长缓慢,当滑动距离超过了RecyclerView的滑动阈值且低于ViewPager2的滑动阈值时,RecyclerView会处理事件,VewPager
● 2、不会处理事件,且RecclerView在发生滑动时,会通知父视图不要拦截之后的事件;2、当滑动距离突增,超过了ViewPager2的滑动阈值时,ViewPager2会处理事件,并对事件进行拦截,这样就造成内部的RecyclerView无法处理事件。这就是滑动冲突产生的原因
滑动冲突解决方案
方案一
布局这样写, 用RelativeLayout包裹住RecyclerView, 添加android:descendantFocusability="blocksDescendants"属性
<RelativeLayout
android:id="@+id/rl_recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_gravity="center_horizontal"
android:overScrollFooter="@android:color/transparent"
android:overScrollHeader="@android:color/transparent"
android:overScrollMode="never"
android:scrollbars="vertical"
android:clipToPadding="false"/>
</RelativeLayout>
java代码中,加上这句话
recycle_view.setNestedScrollingEnabled(false);//禁止rcyc嵌套滑动
方案二
那有一个很直接的办法就是不用ViewPager,用FragmentManager,这样就能实现解决滑动冲突。
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
android:id="@+id/nsv"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:minHeight="10dp"
android:padding="10dp"
android:id="@+id/ll_header">
.........................................
</LinearLayout>
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:id="@+id/c_nsv"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl_content"
>
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
这里的FrameLayout就是给FragmentManager来显示FrameLayout。
这样做就能解决一个activity多个fragment的情况下的滑动冲突
结语
以上就是我今天向大家分享的内容
更多Android进阶资料,学习笔记,底层源码解析
资料获取方式:现在点击免费获取:更多Android进阶资料,学习笔记,面试真题·
Android架构师之路还很漫长,与君共勉
PS:有问题欢迎指正,在评论区留下你的建议和感受