1. 程式人生 > >AppBarLayout+TabLayout+RecyclerView實現滑動隱藏導航欄。

AppBarLayout+TabLayout+RecyclerView實現滑動隱藏導航欄。

先看效果圖。

其實現思路是這樣的,整體佈局用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.csdn.net/detail/lhp15575865420/9920378

如對其中知識點有不理解,參考以下部落格。