coordinatorlayout介绍:
coordinatorlayout是5.0之后推出一个功能。使用布局完成特效比较简单。它像一个协调者,来协助一些属性完成一些特效。appbarlayout,是一个可以滑动的控件,但是必须在coordinatorlayout里面才可以有滑动效果的。是一个“加强版”的 FrameLayout。
布局:
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="us.mifeng.administrator.coordinator.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!--可以设置控件的显示与隐藏,设置滑动的方式 app:layout_scrollFlags:设置ToolBar与RecyclerView的滑动效果 ascroll:两个视图一起滑动,当完全显示就不再滑动 ascroll|enterAlways:两个视图向上滑动的时候一起滑动, 当RecyclerView开始向下滑动的时候, ToolBar开始显示,不管RecyclerView有没有滑多少, scroll|enterAlways|exitUntilCollapsed:上拉的时候合并,下拉的时候分开--> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="240dp" android:minHeight="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways"/> </android.support.design.widget.AppBarLayout> <!--layout_behavior:两个子空间之间的协调关系 appbar_scrolling_view_behavior:AppBarLayout在RecyclerView的前面面 bottom_sheet_behavior:AppBarLayout在RecyclerView的后面--> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v7.widget.RecyclerView> </android.support.design.widget.CoordinatorLayout>
注意:
为了使得Toolbar有滑动效果,必须做到如下三点
1. CoordinatorLayout作为布局的父布局容器。
2.给需要滑动的组件设置app:layout_scrollFlags=”scroll|enterAlways”属性。
3.给滑动的组件设置app:layout_behavior属性