CoordinatorLayout的基本使用

本文參考文章

CoordinatorLayout 學(xué)習(xí)(一) - CoordinatorLayout的基本使用

AppBarLayout中的五種ScrollFlags使用方式匯總

1. 概述

本文主要介紹CoordinatorLayout的基本使用,主要是介紹CoordinatorLayoutAppBarLayout的搭配使用。 和CollapsingToolbarLayout 的屬性

2. AppBarLayout

AppBarLayout繼承自LinearLayout
默認(rèn)有CoordinatorLayoutlayout_behavior屬性,所以能實(shí)現(xiàn)各種效果
其直接子控件可以設(shè)置layout_scrollFlags屬性揽涮,不同的效果就自己去試吧,實(shí)踐才是王道

在正式介紹AppBarLayout的使用時(shí),我們先來(lái)看看幾個(gè)Flag坯认,這幾個(gè)FlagAppBarLayout里面非常的重要翻擒。

屬性值 效果(app:layout_scrollFlags="")
scroll 設(shè)置這個(gè)Flag,表示該View參與聯(lián)動(dòng)牛哺。
snap 必須scroll組合使用陋气,該Flag表示View擁有吸附功能。當(dāng)我們松開(kāi)手指時(shí)引润,Child View要么向上全部滾出屏幕巩趁,要么向下全部滾進(jìn)屏幕
enterAlways 必須scroll組合使用,滑出屏幕還是滑進(jìn)屏幕淳附,該View都能立即響應(yīng)滑動(dòng)事件议慰,跟隨滑動(dòng)。比如說(shuō)奴曙,如果該View是折疊的别凹,當(dāng)RecyclerView向下滑動(dòng)時(shí),該View隨時(shí)都能跟隨展開(kāi)洽糟;
enterAlwaysCollapsed 必須scroll組合使用炉菲,向下滾動(dòng)的時(shí)候堕战,優(yōu)先將AppBarLayout中的childView滾動(dòng)到它的最小高度,滾動(dòng)完成之后scrollview才開(kāi)始自身的滾動(dòng)拍霜,當(dāng)scrollview滾動(dòng)完成時(shí)嘱丢,這個(gè)時(shí)候childView才會(huì)將自己高度完全滾動(dòng)進(jìn)入屏幕;
exitUntilCollapsed 必須scroll組合使用祠饺,這里也涉及到最小高度越驻。發(fā)生向上滾動(dòng)事件時(shí),Child View向上滾動(dòng)退出直至最小高度吠裆,
snapMargins 必須scroll組合使用伐谈,這個(gè) View 將會(huì)被 snap 到它的頂部外邊距和它的底部外邊距的位置,而不是這個(gè) View 自身的上下邊緣试疙。

如果使用了其他值诵棵,必定要使用scroll值才能起作用 ,否則這個(gè)直接子view將失去作用;
使用方式

  • scroll 正匙?酰滑動(dòng)
  • scroll|snap (snap 使用) snap 可以和上面任意一個(gè)組合使用履澳,使用它可以確保childView不會(huì)滑動(dòng)停止在中間的狀態(tài);
  • scroll|enterAlways(enterAlways 使用) 往上滾動(dòng)時(shí),Toolbar逐漸顯示怀跛,但是會(huì)跟隨視圖滾出屏幕距贷;往下滾動(dòng)時(shí),列表滾到頂部吻谋,Toolbar才會(huì)顯示忠蝗。
  • scroll|enterAlways|enterAlwaysCollapsed (enterAlwaysCollapsed 使用) 它是enterAlways的附加值。往上滾動(dòng)時(shí)漓拾,Toolbar逐漸顯示阁最,但是會(huì)跟隨視圖滾出屏幕;往下滾動(dòng)時(shí)骇两,Toolbar立刻顯示速种,不用等列表滾到頂部。
  • scroll|exitUntilCollapsed (exitUntilCollapsed 使用) 在enterAlways使用的基礎(chǔ)上低千,Toolbar會(huì)停留在屏幕頂部配阵。
  • scroll|snap|snapMargins (snapMargins使用) 這個(gè) View 將會(huì)被 snap 到它的頂部外邊距和它的底部外邊距的位置,而不是這個(gè) View 自身的上下邊緣示血,layout_marginTop,和 layout_marginBottom 的邊距位置棋傍。

3.CollapsingToolbarLayout

app:contentScrim Toolbar顯示時(shí)的背景色
app:scrimVisibleHeightTrigger 滾動(dòng)到多高開(kāi)始變色
app:collapsedTitleTextAppearance 折疊式顯示的文本樣式
app:expandedTitleTextAppearance 展開(kāi)時(shí)顯示的文本樣式
app:toolbarId 關(guān)聯(lián)的Toolbar的ID

android.support.v7.widget.Toolbar

layout_collapseMode 折疊模式,pin是隨著往上滾動(dòng)难审,parallax時(shí)要結(jié)合layout_collapseParallaxMultiplier=“0.5”舍沙,上下都往中間收縮。
app:title Toolbar顯示文本
app:navigationIcon 導(dǎo)航圖標(biāo)
————————————————

接下來(lái)剔宪,我們?cè)賮?lái)看一下CollapsingToolbarLayout拂铡。CollapsingToolbarLayout主要是實(shí)現(xiàn)折疊布局的,配合Toolbar使用壹无。
首先,我們來(lái)看看CollapsingToolbarLayout的幾個(gè)Flag:

名稱 作用 (app:layout_collapseMode="")
parallax 設(shè)置該FlagView會(huì)跟內(nèi)容滾動(dòng)感帅,可以通過(guò)setParallaxMultiplier方法來(lái)設(shè)置視圖差比率斗锭,其中0表示毫無(wú)視圖差,完全跟內(nèi)容滾動(dòng)同步失球;1表示View完全不動(dòng)岖是。默認(rèn)的視圖差為0.5。
pin 當(dāng)CollapsingToolbarLayout完全收縮之后实苞,設(shè)置該FlagView會(huì)保留在屏幕當(dāng)中豺撑。
none 默認(rèn)值,表示View不會(huì)有任何屬性

CollapsingToolbarLayout折疊到最頂端時(shí),會(huì)處于最上層,包括toolbar在內(nèi),所有的布局都會(huì)被他蓋住,顯示不出來(lái)黔牵,或者可以設(shè)置一個(gè)透明的背景顯示下面的內(nèi)容

AppBarLayout中的ScrollFlags使用方式匯總
scroll

scroll

scroll|snap
scroll|snap

scroll|enterAlways
scroll|enterAlways

scroll | enterAlways | enterAlwaysCollapsed
scroll | enterAlways | enterAlwaysCollapsed

scroll | exitUntilCollapsed

scroll | exitUntilCollapsed

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false"
    tools:context=".tanDian.merchant.tmine.MerchantMineFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_behavior=".view.AppBarLayoutBehavior"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:fitsSystemWindows="false"
            android:minHeight="@dimen/toolBarSize"
            app:expandedTitleGravity="center"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleTextAppearance="@style/TitleTheme"
            app:titleEnabled="true">
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/tool_bar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/toolBarSize"
                app:titleTextAppearance="@style/TitleTheme"
                />
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                app:layout_collapseMode="parallax"
                >

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="180dp"
                    android:scaleType="fitXY"
                    android:src="@mipmap/mine_head"
                    app:layout_scrollFlags="scroll" />
                <ImageView
                    android:id="@+id/user_avatar"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    tools:src="@mipmap/logo"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="20dp"/>
                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="82dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="150dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginBottom="5dp"
                    app:cardCornerRadius="7dp"
                    app:cardElevation="3dp"
                    app:layout_scrollFlags="scroll">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@color/white">

                        <LinearLayout
                            android:id="@+id/tab_dingdan_view"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center"
                            android:orientation="vertical">

                            <ImageView
                                android:layout_width="40dp"
                                android:layout_height="40dp"
                                android:src="@mipmap/mine_page_content_dingdan" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="6dp"
                                android:text="訂單"
                                android:textColor="@color/merchantBlack"
                                android:textSize="11sp" />
                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/tab_collect_view"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center"
                            android:orientation="vertical">

                            <ImageView
                                android:layout_width="40dp"
                                android:layout_height="40dp"
                                android:src="@mipmap/mine_page_content_shoucang" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="6dp"
                                android:text="收藏"
                                android:textColor="@color/merchantBlack"
                                android:textSize="11sp" />
                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/tab_wallet_view"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center"
                            android:orientation="vertical">

                            <ImageView
                                android:layout_width="40dp"
                                android:layout_height="40dp"
                                android:src="@mipmap/mine_page_content_qianbao" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="6dp"
                                android:text="錢包"
                                android:textColor="@color/merchantBlack"
                                android:textSize="11sp" />
                        </LinearLayout>
                    </LinearLayout>
                </androidx.cardview.widget.CardView>
            </RelativeLayout>


        </com.google.android.material.appbar.CollapsingToolbarLayout>


    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/item_1"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ImageView

                    android:layout_width="16dp"
                    android:layout_height="16dp"
                    android:layout_marginStart="28dp"
                    android:src="@mipmap/mine_comment" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="7dp"
                    android:layout_marginEnd="28dp"
                    android:drawableEnd="@mipmap/item_next"
                    android:text="我的評(píng)論"
                    android:textColor="@color/merchantBlack"
                    android:textSize="@dimen/sp12" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/item_2"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="16dp"
                    android:layout_height="16dp"
                    android:layout_marginStart="28dp"
                    android:src="@mipmap/mine_info" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="7dp"
                    android:layout_marginEnd="28dp"
                    android:drawableEnd="@mipmap/item_next"
                    android:text="個(gè)人中心"
                    android:textColor="@color/merchantBlack"
                    android:textSize="@dimen/sp12" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/item_3"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="16dp"
                    android:layout_height="16dp"
                    android:layout_marginStart="28dp"
                    android:src="@mipmap/mine_service" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="7dp"
                    android:layout_marginEnd="28dp"
                    android:drawableEnd="@mipmap/item_next"
                    android:text="客服中心"
                    android:textColor="@color/merchantBlack"
                    android:textSize="@dimen/sp12" />
            </LinearLayout>
            <TextView
                android:id="@+id/merchant_mobile_tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="@dimen/sp10"
                android:textColor="@color/merchantGray"
                tools:text="客服電話:18205999999"
                android:layout_marginTop="28dp"/>
        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末聪轿,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子猾浦,更是在濱河造成了極大的恐慌陆错,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件金赦,死亡現(xiàn)場(chǎng)離奇詭異音瓷,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)夹抗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門绳慎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人漠烧,你說(shuō)我怎么就攤上這事杏愤。” “怎么了沽甥?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)乏奥。 經(jīng)常有香客問(wèn)我摆舟,道長(zhǎng),這世上最難降的妖魔是什么邓了? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任恨诱,我火速辦了婚禮,結(jié)果婚禮上骗炉,老公的妹妹穿的比我還像新娘照宝。我一直安慰自己,他們只是感情好句葵,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布厕鹃。 她就那樣靜靜地躺著兢仰,像睡著了一般。 火紅的嫁衣襯著肌膚如雪剂碴。 梳的紋絲不亂的頭發(fā)上把将,一...
    開(kāi)封第一講書(shū)人閱讀 51,182評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音忆矛,去河邊找鬼察蹲。 笑死,一個(gè)胖子當(dāng)著我的面吹牛催训,可吹牛的內(nèi)容都是我干的洽议。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼漫拭,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼亚兄!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起嫂侍,我...
    開(kāi)封第一講書(shū)人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤儿捧,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后挑宠,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體菲盾,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年各淀,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了懒鉴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡碎浇,死狀恐怖临谱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情奴璃,我是刑警寧澤悉默,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站苟穆,受9級(jí)特大地震影響抄课,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜雳旅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一跟磨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧攒盈,春花似錦抵拘、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)尚蝌。三九已至,卻和暖如春墩瞳,著一層夾襖步出監(jiān)牢的瞬間驼壶,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工喉酌, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留热凹,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓泪电,卻偏偏與公主長(zhǎng)得像般妙,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子相速,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

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