前言
本章节主要从 嵌套滑动、滑动冲突方案解决来入手,这里不是基于协调者布局;
嵌套滑动
我们先来看一下淘宝和京东的首页滑动效果:
可以看到首页是有一个嵌套滑动的效果,上推的时候,频道先跟着移动,然后置顶不动,这种效果基于协调者布局可以实现,但是如果没有协调者布局的情况下,如何实现呢?
界面如何布局
在不使用 协调者 布局的情况下,界面最终的布局效果应该是类似下面这样的一层结构;
最外层 ScrollVIew/NestedScrollView
嵌套:不能滑动的RecyclerView、TabLayout、ViewPager
经过分析,我们来搭建一个这样的布局来看下
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 这里要稍微改造下,改成不能滑动的 RecyclerView -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/combo_top_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
然后我们编译执行下,看下最终的滑动效果,是否能达到我们的预期:
可以看到,并没有达到我们期望的联动效果,那么原因是什么呢?
View 和 ViewGroup 的关系
- 代码层面:ViewGroup 继承 View&#