在新的Android Support Library裏面,新增了CoordinatorLayout, AppBarLayout等.android
實現的效果: 向下滾動RecylerView,Tab會被隱藏,向上滾動RecylerView,Tab恢復出現.這麼作的好處在於,用戶能有更多的空間位置去看列表裏面的內容.app
實現步驟:ui
<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
<android.support.v7.widget.Toolbar android:id="@+id/third_activity_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"/>
<android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|enterAlways" app:tabIndicatorColor="@color/medium_blue" app:tabSelectedTextColor="@color/medium_blue" app:tabTextAppearance="@style/TabText" app:tabTextColor="@color/gray_text"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
1) 首先須要用CoordinatorLayout包住AppBarLayout;this
2) 頂部區域的View都放在AppBarLayout裏面;spa
3) AppBarLayout外面,CoordinatorLayout裏面,放一個帶有可滾動的View.如上的例子,放的是ViewPager,而ViewPager裏面是放了RecylerView的,便是能夠滾動的View.code
4) 在AppBarLayout裏面的View,經過app:layout_scrollFlags屬性來控制,滾動時候的表現.其中有4種Flag的類型.xml
scroll
: this flag should be set for all views that want to scroll off the screen - for views that do not use this flag, they’ll remain pinned to the top of the screenenterAlways
: this flag ensures that any downward scroll will cause this view to become visible, enabling the ‘quick return’ patternenterAlwaysCollapsed
: When your view has declared a minHeight and you use this flag, your View will only enter at its minimum height (i.e., ‘collapsed’), only re-expanding to its full height when the scrolling view has reached it’s top.exitUntilCollapsed
: this flag causes the view to scroll off until it is ‘collapsed’ (its minHeight) before exiting上面的例子種用的是 scroll 和 enterAlways. blog
Scroll 表示向下滾動時,這個View會被滾出屏幕範圍直到隱藏.繼承
enterAlways 表示向上滾動時,這個View會隨着滾動手勢出現,直到恢復原來的位置.utf-8
5) 在能夠滾動的View上設置屬性 app:layout_behavior.
該屬性的值其實是一個完整的class名字,而上面例子中的 @string/appbar_scrolling_view_behavior 是Android Support Library 定義後的值,能夠被直接使用.
這個Behavior的class是真正控制滾動時候View的滾動行爲.咱們也能夠繼承Behavior這個class去實現特有的滾動行爲.
6) 代碼部分,只須要實現RecylerView的邏輯就能夠了.
Demo 代碼地址: http://pan.baidu.com/s/1pKbRWzL