意外的 CoordinatorLayout 行为

Unexpected CoordinatorLayout behavior

这是我用来创建可折叠工具栏动画的xml:

主要布局:

   <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:fitsSystemWindows="true">
                <include layout="@layout/toolbar"/>
        </android.support.design.widget.AppBarLayout>
            <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.design.widget.CoordinatorLayout>

工具栏布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    style="@style/AppTheme.toolbarStyle"
    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="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"/>

替换 container FrameLayout 的布局:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

我得到的结果:

现在明显的问题是,Recyclerview绘制在工具栏下方,只有在工具栏完全隐藏后列表才开始滚动。预期的动画是在隐藏工具栏的同时滚动回收视图。据我了解,这可能是因为 CoordinatorLayout 是 FrameLayout 的子类。我该如何解决?

将此添加到您的 FrameLayout (@+id/container):

app:layout_behavior="@string/appbar_scrolling_view_behavior"