先看效果图。
其实现思路是这样的,整体布局用CoordinatorLayout,然后里面加入AppBarLayout,这个bar容器里面放入toolbar和TabBar,下面再加入一viewpager,类里面让它与TabLayout联动,RecycleView则放在viewpager里面。下面是布局。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.design.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/barlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toobar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:gravity="bottom"
app:layout_scrollFlags="snap|scroll|enterAlways"
app:title="这是toolbar"
/>
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#68f506c1"
app:tabTextColor="#ffffff"
app:tabSelectedTextColor="#fcd7132d"
app:tabIndicatorColor="#fcd7132d"
app:layout_scrollFlags="snap|scroll|enterAlways"
/>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v4.view.ViewPager
android:id="@+id/page"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
这个布局中,不仅toolbar设置了app:layout_scrollFlags,TabLayout也设置了,这意味着TabLayout和toolbar都会随着Recyclerview一起滑动,然后,app:layout_behavior是放在一个布局里面,其实这里也可以放在viewpager里面,因为这个属性必须放在一个指定的可滑动组件或者容器里面。这里的指定的可滑动组件是RecyclerView和NestedScrollview。而不能说listView和Scrollview。类里面实现了其他的功能,比较简单,不在贴代码,源码已上传,http://download.youkuaiyun.com/detail/lhp15575865420/9920378
如对其中知识点有不理解,参考以下博客。