原文首發於微信公衆號:jzman-blog,歡迎關注交流!php
AppBarLayout 是一個垂直方向的 LinearLayout,它實現了許多符合 Material Design 設計規範的狀態欄應該具備的功能,好比滾動手勢。java
AppBarLayout 通常直接用做 CoordinatorLayout 的直接子對象,若是 AppBarLayout 在別的佈局中使用,其大部分功能會失效,AppBarLayout 須要一個標示纔可以知道滾動視圖何時滾動,這個標示過程是經過綁定 AppBarLayout.ScrollingViewBehavior 類完成的,這意味着應該將滾動視圖的行爲設置爲 AppBarLayout.ScrollingViewBehavior的一個實例,這裏設置包含完整類名的字符串資源,具體以下:android
//設置layout_behavior屬性
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- Your scrolling content -->
</android.support.v4.widget.NestedScrollView>
複製代碼
AppBarLayout 的子 View 應該設置一個可供滾動的 behavior,能夠經過代碼和 xml 屬性設置,具體以下:bash
//代碼
setScrollFlags(int)
//xml attribute
app:layout_scrollFlags
複製代碼
layout_scrollFlags 屬性主要是指定 AppBarLayout 子 View 當滑動手勢變化時,AppBarLayout 的子 View 執行什麼動做,layout_scrollFlags 屬性有 5 個值,分別是:微信
在介紹這幾個屬性值以前,這些屬性值的效果將如下面佈局效果爲例,佈局以下:app
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.manu.materialdesignsamples.samples.SampleCoordinatorAppBarActivity">
<!--AppBarLayout——>子View設置layout_scrollFlags屬性-->
<android.support.design.widget.AppBarLayout android:id="@+id/ablBar" android:layout_width="match_parent" android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="70dp" android:minHeight="?attr/actionBarSize" app:layout_scrollFlags="scroll">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<!--NestedScrollView——>設置layout_behavior屬性-->
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView android:id="@+id/rvAppBarData" android:clipToPadding="true" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
複製代碼
scroll: 當設置 layout_scrollFlags 的屬性爲以下時:佈局
app:layout_scrollFlags="scroll"
複製代碼
此時,上滑時先隱藏 AppBarLayout,而後再開始滾動,下滑時直到滾動視圖的頂部出現 AppBarLayout 纔開始顯示,效果以下:學習
enterAlways: enterAlways 必須配合 scroll 來使用,當設置 layout_scrollFlags 屬性爲以下時:ui
app:layout_scrollFlags="scroll|enterAlways"
複製代碼
此時,上滑時先隱藏AppBarLayout,而後再開始滾動,下滑時先顯示AppBarLayout,而後再開始滾動,效果以下:spa
enterAlwaysCollapsed: 使用 enterAlwaysCollapsed 屬性值時,必須配合 scroll 和 enterAlways 來使用,此外還需設置 AppBarLayout 的子 View (這裏是 Toolbar)一個最小高度 來提供 enterAlwaysCollapsed 的功能支持,當設置 layout_scrollFlags 的屬性爲以下時:
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
複製代碼
此時,上滑時先隱藏AppBarLayout,而後再開始滾動,下滑時 AppBarLayout 先顯示子 View 最小高度,而後直到滾動視圖的頂部出現 AppBarLayout 所有顯示,效果以下:
exitUntilCollapsed: 使用 exitUntilCollapsed 屬性值時,必須配合 scroll 來使用,此外還需設置 AppBarLayout 的子 View (這裏是 Toolbar)一個最小高度 來提供 exitUntilCollapsed 的功能支持,當設置 layout_scrollFlags 的屬性爲以下時:
app:layout_scrollFlags="scroll|exitUntilCollapsed"
複製代碼
此時,上滑時先隱藏 AppBarLayout 至最小高度,而後再開始滾動,下滑時直到滾動視圖的頂部出現 AppBarLayout 纔開始顯示,效果以下:
snap: 使用 snap 屬性值時,必須配合 scroll 來使用,主要做用是在滾動結束時,若是伸縮的視圖只是部分可見,那麼它將自動滾動到最近的邊緣,當設置 layout_scrollFlags 屬性爲以下時:
app:layout_scrollFlags="scroll|snap"
複製代碼
此時,伸縮的視圖(這裏是 Toolbar)若是部分可見,那麼伸縮的視圖將自動滾動到最近的邊緣,即要麼隱藏、要麼顯示,效果以下:
關於 layout_scrollFlags 屬性就介紹完了,固然上面只是爲了說明屬性值的效果,還能夠結合 CollapsingToolbarLayout 等其餘 Material Design 實現酷炫的效果,上面是在佈局文件對 layout_scrollFlags 屬性的設置,順便說一下如何在代碼中設置 layout_scrollFlags 呢,具體以下:
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) appBarLayout
.getChildAt(0).getLayoutParams();
//上滑時先隱藏AppBarLayout,而後再開始滾動,下滑時先顯示AppBarLayout,而後再開始滾動
params.setScrollFlags(
AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL |
AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
//...
複製代碼
AppBarLayout 的使用及其重要屬性已經介紹完了,實際開發中確定要複雜的多,但願上面的內容可以對你有所幫助。能夠選擇關注我的微信公衆號:jzman-blog 獲取最新更新,一塊兒交流學習!