Android Material Design之CoordinatorLayout效果实现

Material Design 相关技术博文,推荐:

ToolBar 基础使用——进阶封装(附源码)


在2015年的Google I/O大会上推出Design Support库,这个库将 Material Design 中最具代表性的一些控件和效果进行了封装。这篇博文就说讲解其中一个 CoordinatorLayout 的控件。Coordinator在英文中是“协调者”的意思,CoordinatorLayout也叫做“协调者布局“。


添加依赖

'com.android.support:recyclerview-v7:26.1.0'
'com.android.support:design:26.1.0'

(提示:根据自己 android.support 版本去调整design、recyclerview依赖版本)


一、CoordinatorLayout+AppBarLayout+Toolbar


<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="wrap_content">
  
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:title="Beauty"
            app:titleTextColor="@color/colorAccent"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />//系统自带值

</android.support.design.widget.CoordinatorLayout>

1、AppBarLayout。

扫描二维码关注公众号,回复: 2017836 查看本文章

    继承自LinearLayout,默认垂直方向,作用是把包裹的内容都作为AppBar,和CoordinatorLayout搭配使用。


2、app:layout_scrollFlags属性。
 
 
  • scroll|exitUntilCollapsed:向上滚动会折叠到顶端,向下滚动到最顶端才能显示
  • scroll|enterAlways: 只要向下滚动布局就会显示,只要向上滑动布局就会向上收缩
  • scroll|enterAlwaysCollapsed: 向下滚动到最底端时该布局才会显示出来
3、app:layout_behavior属性。
app:layout_behavior="@string/appbar_scrolling_view_behavior"
@string/appbar_scrolling_view_behavior 系统值为:android.support.design.widget.AppBarLayout$ScrollingViewBehavior。 

作用是把布局放到AppBarLayout的下面。如果不设置值,会被AppBarLayout重叠并覆盖。


二、AppBarLayout+AppBarLayout+CollapsingToolbarLayout


<?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:fitsSystemWindows="true">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/g"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:title="Beauty"
                app:titleTextColor="@color/colorAccent"/>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

CollapsingToolbarLayout (可折叠的Toolbar):

它确实是起到折叠作用的,可以把自己的布局折叠 。继承自framLayout,子类可以设置layout_gravity的位置。

关键属性:app:layout_collapseMode(折叠模式)

  • 默认是跟随NestedScrollView的滑动一起滑动。
  • pin:(如上图)随着界面滑动CollapsingToolbarLayout布局折叠或展开,Toolbar其实一直固定在所在位置不动。
  • parallax:可用在滑动过程会有视察效果(layout_collapseParallaxMultiplier视差因子 0~1之间取值)。


三、自定义Behavior


Behavior是Design库里新增的布局概念,只有在CoordinatorLayout布局下使用才有意义。Behavior是一系列回调,可以非侵入的为View添加动态的依赖布局,和处理 CoordinatorLayout 滑动手势。

可暂时参考资料:关于CoordinatorLayout与Behavior的一点分析



猜你喜欢

转载自blog.csdn.net/csdn_aiyang/article/details/80196951