AppBarLayout的五種ScrollFlags

ScrollFlags共有五種常量值供AppBarLayoutChild View使用零蓉,在xml布局文件中通過app:layout_scrollFlags設(shè)置茄茁,對應(yīng)的值為:
scroll西土,
enterAlways陆错,
enterAlwaysCollapsed
exitUntilCollapsed剪侮,
snap
也可以在代碼中使用setScrollFlags(int)進行設(shè)置洛退,具體使用及效果請看下面內(nèi)容

scroll:

屏幕向上滑動時AppBarLayout中的Toolbar先被隱藏然后RecycleViewitem才會開始滾動瓣俯,向下滑動時當(dāng)RecycleViewitem到達頂部時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>

如圖:


1578541857903.gif

注意上文中的第二點:如果在這個Toolbar前面的任何其他child View沒有設(shè)置這個值驼仪,那么這個Toolbar設(shè)置的任何屬性都將會失去作用

比如下面的代碼TextViewToolbar的前一個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>

如圖:


1578542300006.gif

enterAlways

屏幕向上滑動時AppBarLayout中的Toolbar先被隱藏然后RecycleViewitem才會開始滾動绪爸,向下滑動時先將Toolbar展示,然后RecycleViewitem才會開始滾動宙攻。這里要注意的是一定要設(shè)置scroll這樣才能讓enterAlways產(chǎn)生效果

app:layout_scrollFlags="scroll|enterAlways"

如圖:


1578542503353.gif

enterAlwaysCollapsed

enterAlwaysCollapsedenterAlways的附加值奠货,因此要同時設(shè)置enterAlwaysenterAlwaysCollapsed,并且要設(shè)置一個新的參數(shù)android:minHeight=""座掘,屏幕向上滑動時AppBarLayout中的Toolbar
先被隱藏然后RecycleViewitem才會開始滾動递惋,向下滑動時先展示android:minHeight=""中設(shè)置的高度然后將RecycleViewitem滾動至頂部,最后將Toolbar剩余高度展示溢陪。

 app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"

如圖:


1578542359254.gif

exitUntilCollapsed

exitUntilCollapsed類似enterAlwaysCollapsed但是效果正好相反萍虽,滑動時始終保留android:minHeight=""設(shè)置的最小高度,向下滑動時到達RecycleViewitem頂部時才會將Toolbar剩余部分展示出來

app:layout_scrollFlags="scroll|exitUntilCollapsed"

如圖:


1578542325287.gif

snap

snap是一個吸附效果類似于 ViewPager

app:layout_scrollFlags="scroll|snap"

如圖:


1578542382670.gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末形真,一起剝皮案震驚了整個濱河市杉编,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖王财,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件卵迂,死亡現(xiàn)場離奇詭異,居然都是意外死亡绒净,警方通過查閱死者的電腦和手機见咒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來挂疆,“玉大人改览,你說我怎么就攤上這事$脱裕” “怎么了宝当?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長胆萧。 經(jīng)常有香客問我庆揩,道長,這世上最難降的妖魔是什么跌穗? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任订晌,我火速辦了婚禮,結(jié)果婚禮上蚌吸,老公的妹妹穿的比我還像新娘锈拨。我一直安慰自己,他們只是感情好羹唠,可當(dāng)我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布奕枢。 她就那樣靜靜地躺著,像睡著了一般佩微。 火紅的嫁衣襯著肌膚如雪缝彬。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天喊衫,我揣著相機與錄音跌造,去河邊找鬼。 笑死族购,一個胖子當(dāng)著我的面吹牛壳贪,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播寝杖,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼违施,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了瑟幕?” 一聲冷哼從身側(cè)響起磕蒲,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤留潦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后辣往,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體兔院,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年站削,在試婚紗的時候發(fā)現(xiàn)自己被綠了坊萝。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡许起,死狀恐怖十偶,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情园细,我是刑警寧澤惦积,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站猛频,受9級特大地震影響狮崩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鹿寻,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一厉亏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧烈和,春花似錦、人聲如沸皿淋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽窝趣。三九已至疯暑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間哑舒,已是汗流浹背妇拯。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留洗鸵,地道東北人越锈。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像膘滨,于是被迫代替她去往敵國和親甘凭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容