ScrollFlags
共有五種常量值供AppBarLayout
的Child View
使用零蓉,在xml
布局文件中通過app:layout_scrollFlags
設(shè)置茄茁,對應(yīng)的值為:
scroll
西土,
enterAlways
陆错,
enterAlwaysCollapsed
,
exitUntilCollapsed
剪侮,
snap
,
也可以在代碼中使用setScrollFlags(int)
進行設(shè)置洛退,具體使用及效果請看下面內(nèi)容
scroll:
屏幕向上滑動時AppBarLayout
中的Toolbar
先被隱藏然后RecycleView
的item
才會開始滾動瓣俯,向下滑動時當(dāng)RecycleView
的item
到達頂部時AppBarLayout
中的Toolbar
才會開始展示。注意兩點:第一點兵怯,如果使用了其他值彩匕,必定要使用這個值才能起作用;第二點:如果在這個Toolbar
前面的任何其他child View
沒有設(shè)置這個值媒区,那么這個Toolbar
設(shè)置的任何屬性都將會失去作用
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="100dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:layout_scrollFlags="scroll"
app:title="scroll"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvToDoList"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
如圖:
注意上文中的第二點:如果在這個Toolbar
前面的任何其他child View
沒有設(shè)置這個值驼仪,那么這個Toolbar
設(shè)置的任何屬性都將會失去作用
比如下面的代碼TextView
是Toolbar
的前一個child view
,并且TextView
中并沒有設(shè)置app:layout_scrollFlags="scroll"
袜漩,這就導(dǎo)致Toolbar
中設(shè)置的scroll
是無效的
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:text="child view 2"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:visibility="visible"
app:layout_scrollFlags="scroll"
app:title="scroll"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvToDoList"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
如圖:
enterAlways
屏幕向上滑動時AppBarLayout
中的Toolbar
先被隱藏然后RecycleView
的item
才會開始滾動绪爸,向下滑動時先將Toolbar
展示,然后RecycleView
的item
才會開始滾動宙攻。這里要注意的是一定要設(shè)置scroll
這樣才能讓enterAlways
產(chǎn)生效果
app:layout_scrollFlags="scroll|enterAlways"
如圖:
enterAlwaysCollapsed
enterAlwaysCollapsed
是enterAlways
的附加值奠货,因此要同時設(shè)置enterAlways
和enterAlwaysCollapsed
,并且要設(shè)置一個新的參數(shù)android:minHeight=""
座掘,屏幕向上滑動時AppBarLayout
中的Toolbar
先被隱藏然后RecycleView
的item
才會開始滾動递惋,向下滑動時先展示android:minHeight=""
中設(shè)置的高度然后將RecycleView
的item
滾動至頂部,最后將Toolbar
剩余高度展示溢陪。
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
如圖:
exitUntilCollapsed
exitUntilCollapsed
類似enterAlwaysCollapsed
但是效果正好相反萍虽,滑動時始終保留android:minHeight=""
設(shè)置的最小高度,向下滑動時到達RecycleView
的item
頂部時才會將Toolbar
剩余部分展示出來
app:layout_scrollFlags="scroll|exitUntilCollapsed"
如圖:
snap
snap
是一個吸附效果類似于 ViewPager
app:layout_scrollFlags="scroll|snap"
如圖: