CoordinatorLayout的基本使用

本文参考文章

CoordinatorLayout 学习(一) - CoordinatorLayout的基本使用

AppBarLayout中的五种ScrollFlags使用方式汇总

1. 概述

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

2. AppBarLayout

AppBarLayout继承自LinearLayout
默认有CoordinatorLayoutlayout_behavior属性,所以能实现各种效果
其直接子控件可以设置layout_scrollFlags属性,不同的效果就自己去试吧,实践才是王道

在正式介绍AppBarLayout的使用时,我们先来看看几个Flag,这几个FlagAppBarLayout里面非常的重要。

属性值 效果(app:layout_scrollFlags="")
scroll 设置这个Flag,表示该View参与联动。
snap 必须scroll组合使用,该Flag表示View拥有吸附功能。当我们松开手指时,Child View要么向上全部滚出屏幕,要么向下全部滚进屏幕
enterAlways 必须scroll组合使用,滑出屏幕还是滑进屏幕,该View都能立即响应滑动事件,跟随滑动。比如说,如果该View是折叠的,当RecyclerView向下滑动时,该View随时都能跟随展开;
enterAlwaysCollapsed 必须scroll组合使用,向下滚动的时候,优先将AppBarLayout中的childView滚动到它的最小高度,滚动完成之后scrollview才开始自身的滚动,当scrollview滚动完成时,这个时候childView才会将自己高度完全滚动进入屏幕;
exitUntilCollapsed 必须scroll组合使用,这里也涉及到最小高度。发生向上滚动事件时,Child View向上滚动退出直至最小高度,
snapMargins 必须scroll组合使用,这个 View 将会被 snap 到它的顶部外边距和它的底部外边距的位置,而不是这个 View 自身的上下边缘。

如果使用了其他值,必定要使用scroll值才能起作用 ,否则这个直接子view将失去作用;
使用方式

  • scroll 正常滑动
  • scroll|snap (snap 使用) snap 可以和上面任意一个组合使用,使用它可以确保childView不会滑动停止在中间的状态;
  • scroll|enterAlways(enterAlways 使用) 往上滚动时,Toolbar逐渐显示,但是会跟随视图滚出屏幕;往下滚动时,列表滚到顶部,Toolbar才会显示。
  • scroll|enterAlways|enterAlwaysCollapsed (enterAlwaysCollapsed 使用) 它是enterAlways的附加值。往上滚动时,Toolbar逐渐显示,但是会跟随视图滚出屏幕;往下滚动时,Toolbar立刻显示,不用等列表滚到顶部。
  • scroll|exitUntilCollapsed (exitUntilCollapsed 使用) 在enterAlways使用的基础上,Toolbar会停留在屏幕顶部。
  • scroll|snap|snapMargins (snapMargins使用) 这个 View 将会被 snap 到它的顶部外边距和它的底部外边距的位置,而不是这个 View 自身的上下边缘,layout_marginTop,和 layout_marginBottom 的边距位置。

3.CollapsingToolbarLayout

app:contentScrim Toolbar显示时的背景色
app:scrimVisibleHeightTrigger 滚动到多高开始变色
app:collapsedTitleTextAppearance 折叠式显示的文本样式
app:expandedTitleTextAppearance 展开时显示的文本样式
app:toolbarId 关联的Toolbar的ID

android.support.v7.widget.Toolbar

layout_collapseMode 折叠模式,pin是随着往上滚动,parallax时要结合layout_collapseParallaxMultiplier=“0.5”,上下都往中间收缩。
app:title Toolbar显示文本
app:navigationIcon 导航图标
————————————————

接下来,我们再来看一下CollapsingToolbarLayoutCollapsingToolbarLayout主要是实现折叠布局的,配合Toolbar使用。
首先,我们来看看CollapsingToolbarLayout的几个Flag:

名称 作用 (app:layout_collapseMode="")
parallax 设置该FlagView会跟内容滚动,可以通过setParallaxMultiplier方法来设置视图差比率,其中0表示毫无视图差,完全跟内容滚动同步;1表示View完全不动。默认的视图差为0.5。
pin CollapsingToolbarLayout完全收缩之后,设置该FlagView会保留在屏幕当中。
none 默认值,表示View不会有任何属性

CollapsingToolbarLayout折叠到最顶端时,会处于最上层,包括toolbar在内,所有的布局都会被他盖住,显示不出来,或者可以设置一个透明的背景显示下面的内容

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="我的评论"
                    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="个人中心"
                    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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,738评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,377评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,774评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,032评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,015评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,239评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,724评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,374评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,410评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,457评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,132评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,733评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,804评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,022评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,515评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,116评论 2 341

推荐阅读更多精彩内容