AppBarLayout 滚动行为不适用于嵌套片段

AppBarLayout scrolling behavior not working for nested fragments

我有一个 MainActivity 和一个 FrameLayout 占位符,我根据 NavigationView 选择切换 in/out 片段。当 MainActivity 工具栏在它包含的 任何 片段(甚至嵌套片段)中遇到滚动行为时,我希望能够让 MainActivity 工具栏滚出屏幕。

它适用于 HomeFragment (1),它只包含一个 SwipeRefreshLayout 和一个 RecyclerView

However,对于ViewPagerFragment (2),其中包含一个TabLayout和一个ViewPager以及一个FragmentStatePagerAdapter和子片段ViewPagerItemFragment (3) 类型,无法在包含 RecyclerView.

ViewPagerItemFragment's 上注册滚动事件

下面是完整的布局(去除了主题或不必要的属性)。

layout_activity_main:

<android.support.v4.widget.DrawerLayout
        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:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                    android:layout_height="?attr/actionBarSize"
                    android:layout_width="match_parent"
                    app:layout_scrollFlags="scroll|enterAlways">

                <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center">

                    <Spinner
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"/>
                </LinearLayout>
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
            android:layout_width="@dimen/drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"/>
</android.support.v4.widget.DrawerLayout>

layout_home_fragment (1):

<android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>

layout_fragment_viewpager (2):

<LinearLayout 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:orientation="vertical">

    <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/tab_strip_height"/>

    <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</LinearLayout>

fragment_viewpager_item (3):

<android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>

没关系,显然我在另一个父容器中使用了另一个 CoordinatorLayout 而没有意识到。只需确保您没有包含多个 CoordinatorLayouts,它会搞砸您内部 Fragments 中的布局。