Search code examples
androidandroid-fragmentsandroid-recyclerviewandroid-coordinatorlayoutandroid-appbarlayout

AppBarLayout scrolling behavior not working for nested fragments


I have a MainActivity with a FrameLayout placeholder where I switch in/out fragments based on the NavigationView selection. I want to be able to have the MainActivity Toolbar scroll off screen when it encounters a scroll behavior in any fragment it contains (even nested fragments).

It works well for HomeFragment (1), which only contains a SwipeRefreshLayout and a RecyclerView.

However, for the ViewPagerFragment (2), which contains a TabLayout and a ViewPager with a FragmentStatePagerAdapter and child fragments of type ViewPagerItemFragment (3), it fails to register scroll events on the ViewPagerItemFragment's containing RecyclerView.

Complete layouts (stripped of theming or unnecessary attributes) are below.

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>

Solution

  • Nevermind, apparently I was using another CoordinatorLayout inside another container parent without realizing it. Just make sure you don't have multiple containing CoordinatorLayouts, it will screw up your layouts inside your inner Fragments.