依賴
implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0" //協(xié)調(diào)布局
//最外層使用 CoordinatorLayout
<androidx.coordinatorlayout.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=".MainActivity3">
<com.google.android.material.appbar.AppBarLayout //頭部部分需要使用 AppBarLayout 容器
android:id="@+id/appbar"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout //折疊 Toolbar 控件
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="250dp"
app:collapsedTitleGravity="left" //titlebar 對(duì)齊方式
app:contentScrim="#ECB2B2" //titlebar 背景顏色
app:expandedTitleMarginStart="60dp" //展開的時(shí)候title的位置
app:expandedTitleMarginBottom="60dp"
app:title="tttt" //title
app:layout_scrollFlags="scroll|exitUntilCollapsed" //詳見下面
>
<ImageView
android:id="@+id/main.backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@mipmap/icon"
app:layout_collapseMode="parallax" //詳見下面
/>
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rec"
app:layout_behavior="@string/appbar_scrolling_view_behavior" //設(shè)置在appbar的下面
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:layout_anchor="@id/appbar" //設(shè)置錨點(diǎn)
android:layout_margin="10dp"
android:backgroundTint="#D10F0F"
android:src="@mipmap/ic_launcher"
app:layout_anchorGravity="right|bottom|end" //根據(jù)錨點(diǎn)定位位置
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
layout_scrollFlags 屬性
1圣絮、scroll:影響向下滾動(dòng)(默認(rèn)不設(shè)置的值)
Child View 伴隨著滾動(dòng)事件而滾出或滾進(jìn)屏幕关贵。
注意兩點(diǎn):
第一點(diǎn)扯饶,如果使用了其他值喳魏,必定要使用這個(gè)值才能起作用棉浸;
第二點(diǎn):如果在這個(gè)child View前面的任何其他Child View沒有設(shè)置這個(gè)值,那么這個(gè)Child View的設(shè)置將失去作用
發(fā)生向下滾動(dòng)是刺彩,優(yōu)先滾動(dòng)Scrolling View迷郑,當(dāng)優(yōu)先滾動(dòng)的一方已經(jīng)全部滾進(jìn)屏幕之后,另一方才開始滾動(dòng)
2创倔、enterAlways : 影響向下滾動(dòng)
對(duì)比scroll和scroll | enterAlways設(shè)置嗡害,發(fā)生向下滾動(dòng)事件時(shí),前者優(yōu)先滾動(dòng)Scrolling View畦攘,后者優(yōu)先滾動(dòng)Child View霸妹,
效果:當(dāng)優(yōu)先滾動(dòng)的一方已經(jīng)全部滾進(jìn)屏幕之后,另一方才開始滾動(dòng)
3知押、enterAlwaysCollapsed:影響向下滾動(dòng)
一般:scroll|enterAlways|enterAlwaysCollapsed
child View需設(shè)定最小值叹螟,即minHeight。
效果:向下滾動(dòng)時(shí)台盯,Child View先向下滾動(dòng)最小高度值罢绽,然后Scrolling View開始滾動(dòng),到達(dá)邊界時(shí)爷恳,Child View再向下滾動(dòng)有缆,直至顯示完全
4、exitUntilCollapsed:影響向上滾動(dòng)
這里也有最小高度温亲,即有沒有設(shè)置minHeight棚壁,一般不設(shè)置
效果:發(fā)生向上滾動(dòng)時(shí),Child View向上滾動(dòng)退出直到最小高度栈虚,然后Scrolling View開始滾動(dòng)袖外。一般minHeight不設(shè)置,所以它就會(huì)完全滾出屏幕
5魂务、snap
Child View滾動(dòng)比例的一個(gè)吸附效果曼验。也就是說,
效果:Child View不會(huì)存在局部顯示的情況粘姜,滾動(dòng)Child View的部分高度鬓照,當(dāng)我們松開手指時(shí),Child View要么向上全部滾出屏幕孤紧,要么向下全部滾進(jìn)屏幕豺裆,有點(diǎn)類似ViewPager的左右滑動(dòng)
layout_collapseMode 屬性
1.parallax:視差模式,在折疊的時(shí)候會(huì)有折疊視差效果号显。一般搭配layout_collapseParallaxMultiplier=“0.5”視差的明顯程度be between 0.0 and 1.0.
2.none:沒有任何效果臭猜,往上滑動(dòng)的時(shí)候toolbar會(huì)首先被固定并推出去躺酒。
3.pin:固定模式,在折疊的時(shí)候最后固定在頂端蔑歌。