前言

前几天学习嵌套布局,为了加深理解,找了一些例子,正好5.0 MD 中有,拿来对比理解。
配合使用AppBarLayout,CoordinatorLayout,NestedScrollView

先看布局嵌套层次

Android Design Support Library_xml

重点介绍

AppBarLayout

appBarLayout 继承自LinearLayout,子控件默认为竖直方向显示,可以用它实现Material Design 的Toolbar;它支持滑动手势;它的子控件可以通过在代码里调用setScrollFlags(int)或者在XML里app:layout_scrollFlags来设置它的滑动手势。当然实现这些的前提是它的根布局必须是CoordinatorLayout。

必须是CoordinatorLayout吗?– ​​查看嵌套布局分析​​​ (​​http://ppisland.top​​)

AppBarLayout既然为LinearLayout 子类,嵌套子view当然不仅仅只有Toolbar
下面我们具体来看看它的使用方法,我们先看以下的布局:

<?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/appbar_layout"
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="wrap_content"
android:background="?attr/colorPrimary"
app:title="标题"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/text"/>
</<android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

增加 ImageView 效果更明显

<?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/appbar_layout"
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="wrap_content"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="标题" />

<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@mipmap/demo"
android:minHeight="?android:attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed" />
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/text" />


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

Flag 设置 会呈现不同的效果

Android Design Support Library_sed_02

下面我们就看一下Scroll Flag 都包含哪些:

scroll:设置这个flag之后 Toolbar会滚出屏幕外部,如果不设置这个Toolbar 会固定在顶部不动。

enterAlways:这个flag跟scroll一块使用时,向上滑动时ToolBar移出屏幕,我们向下滑动时Toolbar进入屏幕。
enterAlwaysCollapsed:enterAlways的附加值。这里涉及到Child View的高度和最小高度,向下滚动时,Child View先向下滚动最小高度值,然后Scrolling View开始滚动,到达边界时,Child View再向下滚动,直至显示完全。
exitUntilCollapsed:这个跟上面的enterAlwaysCollapsed相反;它也涉及到minHeight,当发生向上滚动事件时,AppLayout向上滚动,直到我们设置的minHeight。
Snap:与scroll一起使用时,当滑动距离超过阀值,AppBarLayout就会随着滑动的View方向滑动,知道展示/隐藏 子view。

引用效果图:

DEMO 下载地址: ​​http://pan.baidu.com/s/1slGw2p3​