MaterialDesign(三),CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout使用

什么是 CoordinatorLayout

CoordinatorLayout is a super-powered FrameLayout.
CoordinatorLayout is intended for two primary use cases:
1.As a top-level application decor or chrome layout
2.As a container for a specific interaction with one or more child views

根据官方的介绍,CoordinatorLayout 继承自 ViewGroup,主要有以下两个用途

  1. 作为一个应用顶层的装饰布局,也就是一个Activity Layout 的最外一层布局
  2. 作为一个或多个有特定响应动作的容器,协调(Coordinate)其他组件, 实现联动

什么是 AppBarLayout

AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures.

Children should provide their desired scrolling behavior through setScrollFlags(int) and the associated layout xml attribute: app:layout_scrollFlags.

This view depends heavily on being used as a direct child within a CoordinatorLayout. If you use AppBarLayout within a different ViewGroup, most of it's functionality will not work.

根据官方的介绍

  • AppBarLayout 继承自 LinearLayout,子控件默认为竖直方向,可以用它实现 Material Design 的 Toolbar,支持滑动手势
  • 它的子控件可以通过在代码里调用 setScrollFlags(int) 或者在XML里 app:layout_scrollFlags 来设置它的滑动手势
  • 根布局必须使用 CoordinatorLayout,否则很多功能无法正常使用

什么是 CollapsingToolbarLayout

CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout.

根据官方的介绍 CollapsingToolbarLayout 是一个折叠的 Toolbar 布局,作为 AppBarLayout 的直接子项

下面结合 CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout 的例子来学习

简单使用

按照惯例,我们先看一下效果图:


CoordinatorLayout.gif

从效果图来看,就是一个可根据手势滑动进行折叠的 Toolbar,很普通但很实用,下面看看具体实现。对 Toolbar 还不熟悉的可以参考这篇介绍 MaterialDesign(一),ToolBar使用

  1. 引入 com.android.support:design
    由于我们这个例子使用到 CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout,所以需要引入 com.android.support:design
compile 'com.android.support:design:26.1.0'
  1. xml 文件创建
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <!--添加 android:minHeight="?actionBarSize" 解决 RecyclerView 最后一项显示不全-->
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            app:collapsedTitleTextAppearance="@style/CollapsedTitleTextStyle"
            app:expandedTitleTextAppearance="@style/ExpandedTitleTextStyle"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/android_bg"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="标题"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <!--以下这部分我们下篇文章再介绍,不是本篇的重点,暂时知道是一个列表就可以了-->
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>

==================================================================
<!--CollapsingToolbarLayout展开时标题文字样式-->
<style name="ExpandedTitleTextStyle" parent="TextAppearance.AppCompat.Title">
    <item name="android:textSize">30sp</item>
    <item name="android:textColor">#333333</item>
</style>

<!--CollapsingToolbarLayout折叠时标题文字样式-->
<style name="CollapsedTitleTextStyle" parent="TextAppearance.AppCompat.Title">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">#ffffff</item>
</style>

从 xml 布局文件看,根布局是 CoordinatorLayout,里面包含了 AppBarLayout 和 SwipeRefreshLayout(下篇介绍),AppBarLayout 包含了 CollapsingToolbarLayout 作为可折叠的 Toolbar 的父布局。 AppBarLayout 的子项并没有硬性规定,可以包含任意视图。

这里我们重点关注 CollapsingToolbarLayout 的几个属性:

  • app:layout_scrollFlags
Scroll Flag 作用
scroll 子 View 伴随着滚动事件而滚出或滚进屏幕。如果要使用以下其他值,必须设置该值;如果在设置该值的子 View 之前的View 没有设置这个值,那么该 View 的设置将无效
enterAlways 快速返回模式。当 Scrolling View 向下滑动时,子 View 将直接向下滑动,而不管 Scrolling View 是否在滑动。使用该值必须与 scroll 一起使用才有效果
enterAlwaysCollapsed 该值是对 enterAlways 的补充,涉及到子 View 的高度和最小高度,向下滚动时,子 View 先向下滚动最小高度值,然后 Scrolling View 开始滚动,到达边界时,子 View 再向下滚动,至显示完全
exitUntilCollapsed 发生向上滚动事件时,子 View 向上滚动退出直至设置的最小高度,然后 Scrolling View 才开始滚动
snap 在滚动结束后,如果设置该属性的 view 只是部分可见,它将滑动到最近的边界,不会存在部分显示的情况

在本篇的例子中我们设置的是 app:layout_scrollFlags="scroll|exitUntilCollapsed|snap",呈现的效果是往上滑时,直至 Toolbar 设置的高度时,才轮到下面列表的滑动;往下滑时,直至滑动到列表顶部时,才轮到 CollapsingToolbarLayout 的滑动,由于设置了 snap,所以不会存在 CollapsingToolbarLayout 显示部分的时候,要么显示最小高度,要么完全显示。其他效果这里就不一一展示了,感兴趣的可以自己实现,跑一下效果

  • app:collapsedTitleTextAppearance 用于设置折叠时标题文字样式,这里只是简单的设置了大小和颜色

  • app:expandedTitleTextAppearance 用于设置展开时标题文字样式,这里也只是简单的设置了大小和颜色

到此就完成了一开始展示的效果,是不是很简单,甚至不用在 Activity 中编写任何代码。

结语

本文主要介绍了 CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout 的使用,这种折叠式的布局在实际开发中还是用到的比较多。本文 demo 已上传到 github。下面是几个控件的官方API地址(自备梯子)

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

推荐阅读更多精彩内容