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

Android AppBarLayout滚动行为不适用于嵌套片段,android,android-fragments,android-recyclerview,android-coordinatorlayout,android-appbarlayout,Android,Android Fragments,Android Recyclerview,Android Coordinatorlayout,Android Appbarlayout,我有一个main活动,带有一个FrameLayout占位符,根据导航视图选择在其中切换入/出片段。我希望当MainActivity工具栏在其包含的任何片段(甚至是嵌套片段)中遇到滚动行为时,能够使其从屏幕上滚动 它适用于HomeFragment(1),它只包含SwipeRefreshLayout和RecyclerView 但是对于ViewPagerFragment(2),它包含一个表格布局和一个带有FragmentStatePagerDatapter的ViewPagerFragment(3)类型

我有一个
main活动
,带有一个
FrameLayout
占位符,根据导航视图选择在其中切换入/出片段。我希望当MainActivity工具栏在其包含的任何片段(甚至是嵌套片段)中遇到滚动行为时,能够使其从屏幕上滚动

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

但是对于
ViewPagerFragment(2)
,它包含一个
表格布局
和一个
带有
FragmentStatePagerDatapter的
ViewPagerFragment(3)
类型的子片段,它无法在包含
RecyclerView
ViewPagerItemFragment上注册滚动事件

完整的布局(去掉主题或不必要的属性)如下所示

布局\u活动\u主要内容:

<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>
<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>
<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>
<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>

布局\u主页\u片段(1):

<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>
<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>
<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>
<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>

布局\u片段\u浏览页面(2):

<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>
<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>
<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>
<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>

fragment\u viewpager\u项目(3):

<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>
<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>
<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>
<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>


没关系,显然我在另一个容器父容器中使用了另一个Coordinator布局,却没有意识到这一点。只要确保你没有多个包含CoordinatorLayout的容器,它会在你的内部片段中破坏你的布局。

没关系,显然我在另一个容器父容器中使用了另一个CoordinatorLayout,但我没有意识到这一点。只需确保您没有多个包含协调器的布局,它会在您的内部片段中破坏您的布局。

问得好,谢谢您的回答。但是如果我们在片段中需要一个协调器布局,我们该怎么办?我有一个片段,里面有协调器布局、appbar和折叠布局。没有在每个片段中使用协调器布局是不可避免的。问得好,谢谢你的回答。但是如果我们在片段中需要一个协调器布局,我们该怎么办?我有一个片段,里面有协调器布局、appbar和折叠布局。不在每个片段中使用协调器布局是不可避免的。