介紹
AppBarLayout實際是一個垂直方向的LinearLayout,在它的內部做了很多滾動
事件的封裝,并應用了一些Material Design的設計理念。關鍵
(1)CoordinatorLayout作為AppBarLayout的直接父布局;
(2)CoordinatorLayout里面包含有兩個子控件,一個是AppBarLayout,一個是可以
滑動的控件(CoordinatorLayout是一個加強版FrameLayout,目前支持的滑動
控件有RecyclerView 和 NestedScrollView)
(3)外面直接子view要:
app:layout_behavior="@string/appbar_scrolling_view_behavior"
這樣當滑動控件滑動時候,就會將滾動事件通知給AppBarLayout,通過關鍵屬性:behavior
(4)AppActionBar要去處理接受到的滾動事件,它內部的子控件可以通過
app:layout_scrollFlags:
第一種:scroll:值設為scroll的View會跟隨滾動事件一起發生移動。相當于這個子view和下面滑動一體
最終實現向上滾動并且實現隱藏。
第二種:enterAlways:值設為enterAlways的View,當ScrollView往下滾動時,該View會直接往下滾動。最終效果是向下滾動并且顯示。
而不用考慮ScrollView是否在滾動。
第三種:exitUntilCollapsed:值設為exitUntilCollapsed的View,當這個View
要往上逐漸“消逝”時,會一直往上滑動,直到剩下的的高度達到它的最
小高度后,再響應ScrollView的內部滑動事件。要手動設置其最小高度
第四種:enterAlwaysCollapsed:是enterAlways的附加選項,一般跟enterAlways
一起使用,它是指,View在往下“出現”的時候,首先是enterAlways效果,
當View的高度達到最小高度時
View就暫時不去往下滾動,直到ScrollView滑動到頂部不再滑動時
View再繼續往下滑動,直到滑到View的頂部結束。
也需要設置最小的高度
第五種:snap:表示子view還沒有完全隱藏或顯示時候,會根據當前滾動的距離,
自動選擇還是隱藏還是顯示。
- 提醒
(1)由于常常是tablayout+viewpager來使用,這里也做一下介紹,前面提到需要用CoordinatorLayout包裹兩個
子控件,將tablayout放在appbarlayout中,將ViewPager放在下面,但是ViewPager不具有滑動屬性,因此,需要viewpager里面的內容是可滑動的比如RecyclerView才可以,下面是關鍵代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:fitsSystemWindows="true" //關鍵代碼,沒有這句話,中間的tablayout就會只顯示一般,最后滑動結束時候,也可以設置margin_top=25dp但是效果不如fitsSystemWindows好
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout //關鍵代碼,直接包裹兩個控件
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
app:layout_scrollFlags="scroll" //告訴他是可以滑動的
android:background="#000"
android:layout_width="match_parent"
android:layout_height="200dp">
</RelativeLayout>
<android.support.design.widget.TabLayout
app:layout_scrollFlags="exitUntilCollapsed" //告訴他是滑動,并且是停在頂部
app:tabIndicatorColor="@android:color/holo_blue_light"
app:tabSelectedTextColor="@android:color/holo_blue_light"
app:tabTextAppearance="@style/TabLayoutTextStyle"
android:background="#ffff"
android:id="@+id/myTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
app:layout_behavior="@string/appbar_scrolling_view_behavior" //關鍵屬性,如果沒有這個,雖然會滑動,
//但是呢viewpager里面內容區域會和tablaout區域重疊。
android:id="@+id/myViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
//還有一點需要提醒的是,這里的viewpager沒有上下滑動的屬性,這里之所以可以用,使其的內容區域是可 滑動的RecyclerView,這里RecyclerView最好要配置了Adapter,但是可以不顯示內容,也是可以的
device-2017-03-17-124036_20170317124158.gif