原文地址声明: https://blog.youkuaiyun.com/qq_23179075/article/details/52792046
CoordinatorLayout+AppBarLayout+ToolBar
实现标题栏伸缩
项目中加入Design包。
dependencies {
compile 'com.android.support:design:24.1.1'
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinator_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="zhengliang.com.coordinatorlayout.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:titleTextColor="@color/icons"
app:layout_scrollFlags="scroll|enterAlways|snap">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="48dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/icons"
app:tabTextColor="@color/secondary_text"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
AppBarLayout
是一个实现了很多单独特性的垂直的 LinearLayout
,它能响应滑动事件。但是必须在它的子view上设置 app:layout_scrollFlags
属性或者是在代码中调用 setScrollFlags()
设置这个属性。
layout_scrollFlags的五个基本属性
- scroll
Child View 伴随着滚动事件而滚出或滚进屏幕。
如果使用了其他值,必定要使用这个值才能起作用;
如果在这个child View前面的任何其他Child View没有设置这个值,那么这个Child View的设置将失去作用。
app:layout_scrollFlags="scroll"
- enterAlways
快速返回模式。其实就是向下滚动时Scrolling View和Child View之间的滚动优先级问题。对比scroll和scroll丨enterAlways
设置,发生向下滚动事件时,前者优先滚动Scrolling View,后者优先滚动Child View,当优先滚动的一方已经全部滚进屏幕之后,另一方才开始滚动。
app:layout_scrollFlags="scroll|enterAlways"
- enterAlwaysCollapsed
enterAlways的附加值。这里涉及到Child View的高度和最小高度,向下滚动时,Child View先向下滚动最小高度值,然后Scrolling View开始滚动,到达边界时,Child View再向下滚动,直至显示完全。
android:layout_height="@dimen/dp_200"
android:minHeight="@dimen/dp_56"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
- exitUntilCollapsed
这里也涉及到最小高度。发生向上滚动事件时,Child View向上滚动退出直至最小高度,然后Scrolling View开始滚动。也就是,Child View不会完全退出屏幕
android:layout_height="@dimen/dp_200"
android:minHeight="@dimen/dp_56"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
- snap
简单理解,就是Child View滚动比例的一个吸附效果。也就是说,Child View不会存在局部显示的情况,滚动Child View的部分高度,当我们松开手指时,Child View要么向上全部滚出屏幕,要么向下全部滚进屏幕,有点类似ViewPager的左右滑动。
android:layout_height="@dimen/dp_200"
app:layout_scrollFlags="scroll|snap"
layout_scrollFlags
的五个属性的都是一些细微的差别,仔细观察效果图就会看出来的!
这个类的特性强烈依赖于它是否是一个CoordinatorLayout
的直接子view,如果不是,那么它的很多特性不能够使用。AppBarLayout
需要一个具有滑动属性的兄弟节点view (比如:ScrollView,ListView,RecyclerView
之类),并且在这个兄弟节点View中指定behavior属性为 AppBarLayout.ScrollingViewBehavior
的类实例,可以使用一个内置的string表示这个默认的实例 @string/appbar_scrolling_view_behavior.