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>
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordinator);

        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
}

1、AppBarLayout。

    继承自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"

其系统值为: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:id="@+id/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之间取值)。

 

三、自定义AppBarLayout.OnOffsetChangedListener


public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {

    public enum State {
        EXPANDED,
        COLLAPSED,
        IDLE
    }

    private State mCurrentState = State.IDLE;

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        if (i == 0) {
            if (mCurrentState != State.EXPANDED) {
                onStateChanged(appBarLayout, State.EXPANDED);
            }
            mCurrentState = State.EXPANDED;
        } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {//appbar的高
            if (mCurrentState != State.COLLAPSED) {
                onStateChanged(appBarLayout, State.COLLAPSED);
            }
            mCurrentState = State.COLLAPSED;
        } else {
            if (mCurrentState != State.IDLE) {
                onStateChanged(appBarLayout, State.IDLE);
            }
            mCurrentState = State.IDLE;
        }
    }

    public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}
 appbarlayout =findViewById(R.id.appbarlayout);

        appbarlayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if( state == State.EXPANDED ) {
                    //展开状态
                    toolbar.setNavigationIcon(null);

                }else if(state == State.COLLAPSED){
                    //折叠状态
                  toolbar.setNavigationIcon(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_material);
                }else {
                    //中间状态
                    toolbar.setNavigationIcon(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_material);

                }
            }
        });

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾阳Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值