需要添加依賴
compile 'com.android.support:design:25.3.1'
首先先來兩張圖
正如圖中所示:coordinatorlayout包括兩大部分,一部分是appbarlayout(這一部分還可以包含CollapsingToolbarLayout,CollapsingToolbarLayout用于對Toolbar進行包裝,因此,它是AppBarLayout的直接子View,同時也是Toolbar的直接父View),另一部分是實現了nestedscrollingchild接口的滾動列表(比如recycleview,nestedscrollview...)
appbarlayout下方的那個列表(比如recycleview,nestedscrollview...)中必須設置app:layout_behavior="@string/appbar_scrolling_view_behavior"
或者是app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
需要將AppBarLayout作為CoordinatorLayout的直接子View,同時它也是一個LinearLayout,因此它的所有子View都是線性排列的,而它對子View的滾動顯示管理是通過子View的app:layout_scrollFlags屬性,注意,這個標志位是在AppBarLayout的子View中聲明的,而不是AppBarLayout中。
app:layout_scrollFlags的值有下面五種可選:
- scroll
注意事項
- 這個標志位是其它四個標志位生效的前提
- 帶有scroll標志位的子View必須放在AppBarLayout中的最前面
- exitUntilCollapsed
- enterAlways
- enterAlwaysCollapsed
- snap
這五個標志位在子view中的組合及效果有:
- app:layout_scrollFlags="scroll"
效果:
設置了scroll的子View可以在滾動后收起,而沒有設置的則不可以。
在手指向上移動的時候,優先收起AppBarLayout中的可收起View,當它處于收起狀態時,下面的列表內容才開始向尾部滾動。
在手指往下移動的時候,優先讓下面的列表內容向頂部滾動,當列表滾動到頂端時,AppBarLayout的可收起View才展開。
- app:layout_scrollFlags="scroll|exitUntilCollapsed"
另外設置一下android:minHeight="50dp"
效果:
手指向上移動的時候,會優先收起AppBarLayout中的子View,而收起的最小高度為0,如果想收起時仍然保留部分可見,那么就需要使用exitUntilCollapsed + minHeight屬性,minHeight就決定了收起時的最小高度是多少
- app:layout_scrollFlags="scroll|enterAlways"
效果:
enterAlways則決定了手指向下移動時的行為。默認情況下,在手指向下移動時,會優先讓列表滾動到頂部,而如果設置了enterAlways,那么會優先讓AppBarLayout中的子View滾動到展開。
- app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
效果:
優先滾動AppBarLayout中的子View,但是并不是滾動到全部展開,而是只滾動到minHeight
AppBarLayout中的子View滾動到minHeight之后,開始滾動列表,直到列表滾動到頭部
列表滾動到頭部之后,開始滾動AppBarLayout中的子View,直到它完全展開
- app:layout_scrollFlags="scroll|snap"
效果:
默認情況下,在手指從屏幕上抬起之后,AppBarLayout中子View的狀態就不會變化了,而如果我們設置了snap標志位,那么在手指抬起之后,會根據子View當前的偏移量,決定是讓它變為收起還是展開狀態
監聽AppBarLayout的滾動狀態
AppBarLayout提供了監聽滾動狀態的接口,我們可以根據這個偏移值來改變界面的狀態:
private void setAppBar() {
final TextView moveView = (TextView) findViewById(R.id.iv_move_title);
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.al_title);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
Log.d("AppBarLayout", "offset=" + verticalOffset);
int height = moveView.getHeight();
int minHeight = moveView.getMinHeight();
float fraction = verticalOffset / (float) (minHeight - height);
moveView.setAlpha(1 - fraction);
}
});
}
AppBarLayout和CollapsingToolbarLayout結合
直接上代碼
<android.support.design.widget.AppBarLayout
android:id="@+id/al_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/ctl_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="@color/colorPrimaryDark"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/iv_title"
android:src="@drawable/ic_bg"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:layout_height="150dp"
app:layout_collapseParallaxMultiplier="0.5"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:title="@string/app_name"
android:layout_width="match_parent"
android:layout_height="50dp"
app:title="Collapse"
app:navigationIcon="@android:drawable/ic_media_play"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
下面,我們就來介紹上面這個布局中比較重要的標志位:
app:contentScrim
設置收起之后CollapsingToolbarLayout的顏色,正如例子中的那樣,在收起之后,ImageView的背景被我們設置的contentScrim所覆蓋。
app:layout_scrollFlags="scroll|exitUntilCollapsed"
scroll標志位是必須設置的,exitUntilCollapsed保證了我們在手指上移的時候,CollapsingToolbarLayout最多收起到Toolbar的高度,使得它始終保持可見。
app:layout_collapseMode="parallax"和app:layout_collapseParallaxMultiplier="0.5"
layout_collapseMode有兩種模式,parallax表示視差效果,簡單地說,就是當CollapsingToolbarLayout滑動的距離不等于背景滑動的距離,從而產生一種視差效果,而視差效果的大小由app:layout_collapseParallaxMultiplier決定。
app:layout_collapseMode="pin"
layout_collapseMode的另一種模式,它使得Toolbar一直固定在頂端。
參考文獻
Material Design 控件知識梳理(2) - AppBarLayout & CollapsingToolbarLayout
Material Design之 AppbarLayout 開發實踐總結