在某些App
当中,咱们常常会见到相似于下面的这种设计: android
Google
为这种设计提供了几个类,让咱们只用实现不多的代码就可以实现这种效果,避免了咱们经过去监听列表的滚动状态来去改变头部区域的显示,这篇文章,咱们就一步步来学习如何实现这种效果。 要实现上面这种效果,须要对下面几方面的知识有所了解:
CoordinatorLayout
AppBarLayout
CollapsingToolbarLayout
NestedScrollingChild
接口的滚动布局,例如RecyclerView
、NestedScrollView
等这四者的关系能够用下面这张图来表示: bash
其中CollapsingToolbarLayout
须要依赖于AppBarLayout
,所以我打算把文章的讨论分为两部分:app
AppBarLayout
实现AppBarLayout + CollapsingToolbarLayout
实现AppBarLayout
实现当只采用AppBarLayout
时,咱们的布局层次通常是下面这样: ide
CoordinatorLayout
CoordinatorLayout
是一个重写的ViewGroup
,它负责AppBarLayout
以及可滚动布局之间的关系,所以,它是做为这二者的直接父控件。布局
AppBarLayout
下的列表通常是RecyclerView
或者ViewPager
,为了能让它和AppBarLayout
协同工做,须要给列表控件添加下面这个属性,这样列表就会显示在AppBarLayout
的下方:学习
app:layout_behavior="@string/appbar_scrolling_view_behavior"
复制代码
AppBarLayout
的标志位须要将AppBarLayout
做为CoordinatorLayout
的直接子View
,同时它也是一个LinearLayout
,所以它的全部子View
都是线性排列的,而它对子View
的滚动显示管理是经过子View
的app:layout_scrollFlags
属性,注意,这个标志位是在AppBarLayout
的子View
中声明的,而不是AppBarLayout
中。ui
app:layout_scrollFlags
的值有下面五种可选:spa
scroll
exitUntilCollapsed
enterAlways
enterAlwaysCollapsed
snap
scroll
使得AppBarLayout
的子View
伴随着滚动而收起或者展开,有两点须要注意:设计
scroll
标志位的子View
必须放在AppBarLayout
中的最前面如今咱们给RecyclerView
设置了app:layout_behavior
,而AppBarLayout
中的两个子View
都没有设置scroll
标志位:3d
<?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:id="@+id/al_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:text="layout_scrollFlags=scroll"
android:textColor="@android:color/white"
android:background="@android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="100dp"/>
<TextView
android:gravity="center"
android:text="没有设置layout_scrollFlags"
android:textColor="@android:color/white"
android:background="@android:color/holo_orange_dark"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
复制代码
此时AppBarLayout
中的子View
都一直固定在CoordinatorLayout
的顶部:
AppBarLayout
中的第一个子
View
加上
app:layout_scrollFlags="scroll|exitUntilCollapsed"
标志位:
<android.support.design.widget.AppBarLayout
android:id="@+id/al_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:text="layout_scrollFlags=scroll"
android:textColor="@android:color/white"
android:background="@android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_scrollFlags="scroll"/> //注意看这里!
<TextView
android:gravity="center"
android:text="没有设置layout_scrollFlags"
android:textColor="@android:color/white"
android:background="@android:color/holo_orange_dark"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</android.support.design.widget.AppBarLayout>
复制代码
那么此时滑动状况为:
scroll
的子View
能够在滚动后收起,而没有设置的则不能够。AppBarLayout
中的可收起View
,当它处于收起状态时,下面的列表内容才开始向尾部滚动。AppBarLayout
的可收起View
才展开。
exitUntilCollapsed
对于可收起的子View
来讲有两种状态:Enter
和Collapsed
。手指向上移动的时候,会优先收起AppBarLayout
中的子View
,而收起的最小高度为0
,假如但愿它在收起时仍然保留部分可见,那么就须要使用exitUntilCollapsed + minHeight
属性,minHeight
就决定了收起时的最小高度是多少,也就是Collapsed
状态。
<android.support.design.widget.AppBarLayout
android:id="@+id/al_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:text="layout_scrollFlags=scroll"
android:textColor="@android:color/white"
android:background="@android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="100dp"
android:minHeight="50dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"/>
<TextView
android:gravity="center"
android:text="没有设置layout_scrollFlags"
android:textColor="@android:color/white"
android:background="@android:color/holo_orange_dark"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</android.support.design.widget.AppBarLayout>
复制代码
enterAlways
exitUntilCollapsed
决定了手指向上移动时AppBarLayout
怎么收起它的子View
,而enterAlways
则决定了手指向下移动时的行为。默认状况下,在手指向下移动时,会优先让列表滚动到顶部,而若是设置了enterAlways
,那么会优先让AppBarLayout
中的子View
滚动到展开。
<android.support.design.widget.AppBarLayout
android:id="@+id/al_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 须要设置属性 -->
<TextView
android:gravity="center"
android:text="layout_scrollFlags=scroll|enterAlways"
android:textColor="@android:color/white"
android:background="@android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_scrollFlags="scroll|enterAlways"/>
<TextView
android:gravity="center"
android:text="没有设置layout_scrollFlags"
android:textColor="@android:color/white"
android:background="@android:color/holo_orange_dark"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</android.support.design.widget.AppBarLayout>
复制代码
enterAlwaysCollapsed
enterAlwaysCollapsed
,enterAlways
以及minHeight
属性一块儿配合使用,采用这个标志位,主要是为了使得手指向下移动时出现下面这个效果:
AppBarLayout
中的子View
,可是并非滚动到所有展开,而是只滚动到minHeight
AppBarLayout
中的子View
滚动到minHeight
以后,开始滚动列表,直到列表滚动到头部AppBarLayout
中的子View
,直到它彻底展开注意和exitUntilCollapsed
的minHeight
区分开来;
enterAlways|enterAlwaysCollapsed
的minHeight
,决定的是手指向下移动且列表没有滚动到顶端的最大高度exitUntilCollapsed
的minHeight
,决定的是手指向上移动时的最小高度这两个标志位和minHeight
是相互依赖的关系,若是声明对应的标志位,那么minHeight
是没有任何意义的;而若是只声明了标志位,没有声明minHeight
,那么默认minHeight
为0
。
<android.support.design.widget.AppBarLayout
android:id="@+id/al_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 须要设置属性 -->
<TextView
android:gravity="center"
android:text="layout_scrollFlags=scroll|enterAlways|enterAlwaysCollapsed, minHeight=50dp"
android:textColor="@android:color/white"
android:background="@android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="100dp"
android:minHeight="50dp"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"/>
<TextView
android:gravity="center"
android:text="没有设置layout_scrollFlags"
android:textColor="@android:color/white"
android:background="@android:color/holo_orange_dark"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</android.support.design.widget.AppBarLayout>
复制代码
snap
默认状况下,在手指从屏幕上抬起以后,AppBarLayout
中子View
的状态就不会变化了,而若是咱们设置了snap
标志位,那么在手指抬起以后,会根据子View
当前的偏移量,决定是让它变为收起仍是展开状态。
<android.support.design.widget.AppBarLayout
android:id="@+id/al_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 须要设置属性 -->
<TextView
android:gravity="center"
android:text="layout_scrollFlags=scroll|snap"
android:textColor="@android:color/white"
android:background="@android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_scrollFlags="scroll|snap"/>
<TextView
android:gravity="center"
android:text="没有设置layout_scrollFlags"
android:textColor="@android:color/white"
android:background="@android:color/holo_orange_dark"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</android.support.design.widget.AppBarLayout>
复制代码
AppBarLayout
的滚动状态AppBarLayout
提供了监听滚动状态的接口,咱们能够根据这个偏移值来改变界面的状态:
private void setAppBar() {
final TextView moveView = (TextView) findViewById(R.id.iv_move_title);
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.al_title);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
Log.d("AppBarLayout", "offset=" + verticalOffset);
int height = moveView.getHeight();
int minHeight = moveView.getMinHeight();
float fraction = verticalOffset / (float) (minHeight - height);
moveView.setAlpha(1 - fraction);
}
});
}
复制代码
这里,咱们根据offset
的值来改变alpha
,最终达到下面的效果:
verticalOffset
从
0
变为负值:
AppBarLayout
小结AppBarLayout
主要是掌握两点:
app:layout_scrollFlags
几种标志位以前的区别AppBarLayout
的滚动掌握完这两点以后,咱们就能够自由发挥,作出本身想要的效果了。
AppBarLayout
和CollapsingToolbarLayout
结合CollapsingToolbarLayout
用于对Toolbar
进行包装,所以,它是AppBarLayout
的直接子View
,同时也是Toolbar
的直接父View
,当使用CollapsingToolbarLayout
时咱们的界面布局通常是这样的:
CollapsingToolbarLayout
的标志比较多,并且须要和
Toolbar
相结合,下面,咱们就以一种比较常见的例子,来说解几个比较重要的标志位,先看效果:
AppBarLayout
的布局以下:
<android.support.design.widget.AppBarLayout
android:id="@+id/al_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/ctl_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="@color/colorPrimaryDark"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/iv_title"
android:src="@drawable/ic_bg"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:layout_height="150dp"
app:layout_collapseParallaxMultiplier="0.5"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:title="@string/app_name"
android:layout_width="match_parent"
android:layout_height="50dp"
app:title="Collapse"
app:navigationIcon="@android:drawable/ic_media_play"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
复制代码
下面,咱们就来介绍上面这个布局中比较重要的标志位:
app:contentScrim
设置收起以后CollapsingToolbarLayout
的颜色,正如例子中的那样,在收起以后,ImageView
的背景被咱们设置的contentScrim
所覆盖。app:layout_scrollFlags="scroll|exitUntilCollapsed"
scroll
标志位是必须设置的,exitUntilCollapsed
保证了咱们在手指上移的时候,CollapsingToolbarLayout
最多收起到Toolbar
的高度,使得它始终保持可见。app:layout_collapseMode="parallax"
和app:layout_collapseParallaxMultiplier="0.5"
layout_collapseMode
有两种模式,parallax
表示视差效果,简单地说,就是当CollapsingToolbarLayout
滑动的距离不等于背景滑动的距离,从而产生一种视差效果,而视差效果的大小由app:layout_collapseParallaxMultiplier
决定。app:layout_collapseMode="pin"
layout_collapseMode
的另外一种模式,它使得Toolbar
一直固定在顶端。除了上面这几个重要的标志位以外,相信你们还发现了CollapsingToolbar
在所有展开的时候会把标题设置为比较大,而当上移时,标题慢慢缩小为Toolbar
的标题大小,并移动到Toolbar
的标题所在位置。
以上就是对于使用CoordinatorLayout
、AppBarLayout
、CollapsingToolbarLayout
实现标题跟随列表滚动的简要介绍,最主要的是掌握layout_scrollFlags
几种标志之间的区别。
Material Design之 AppbarLayout 开发实践总结