Search code examples
androidandroid-recyclerviewandroid-collapsingtoolbarlayoutandroidxandroid-jetpack-navigation

Achieving proper scrolling with RecyclerView, android jetpack navigation and a collapsing Toolbar


I try to implement proper scroll behaviour with the following setup in my android application.

For navigation I use the jetpack navigation in combination with a Toolbar layout and a bottom navigation. I also use the principle of 'one activity, many fragments'. Each item of the bottom navigation launches a corresponding Fragment based on my navigation graph. This requires me to use a NavHostFragment in my layout.

My Toolbar layout is part of the activity layout and gets populated based on the current fragment. Some fragments require a collapsing Toolbar, which also gets added when needed. But when having a collapsing toolbar I face the following problem:

In the particular case I have a collapsing Toolbar, the NavHostFragment is populated with a RecyclerView. In other cases it appears I can add app:layout_behavior="@string/appbar_scrolling_view_behavior" to the RecyclerView and get the expected result, because the RecyclerView is a direct child of the CoordinatorLayout. In my case the RecyclerView is a child of a Fragment, which is basically a FrameLayout (according to the Layout Inspector). This leads to the problem, that the layout_behaviour on the RecyclerView has no effect as the RecyclerView is not a direct child of the CoordinatorLayout.

I couldn't come up with a working solution for this problem. Does anybody have an idea?

layout/activity_overview.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OverviewActivity">

<androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="wrap_content"
                                                     android:layout_height="wrap_content"
                                                     android:id="@+id/toolbarCoordiantor"
                                                     android:layout_marginTop="?attr/actionBarSize"
                                                     app:layout_constraintTop_toTopOf="parent"
                                                     app:layout_constraintBottom_toTopOf="@id/overview_bottom_navigation"
                                                     app:layout_constraintStart_toStartOf="parent"
                                                     app:layout_constraintEnd_toEndOf="parent">

    <fragment android:layout_width="match_parent" android:layout_height="wrap_content"
              android:id="@+id/overview_fragmentholder"
              android:name="androidx.navigation.fragment.NavHostFragment"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/toolbar"
              app:layout_constraintBottom_toBottomOf="parent"
              app:defaultNavHost="true"
              android:layout_marginBottom="?attr/actionBarSize"
              android:layout_marginTop="?attr/actionBarSize"
              app:navGraph="@navigation/nav_graph"
              app:layout_constraintVertical_bias="1.0"/>

    <include layout="@layout/toolbar"
             android:id="@+id/toolbar"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/overview_bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foregroundGravity="bottom"
        app:menu="@menu/navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:itemBackground="@color/white"
        app:itemIconTint="@color/bottom_navigation_color_states"
        app:itemTextColor="@color/bottom_navigation_color_states"/>

layout/toolbar.xml

<com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap|snapMargins"
        android:minHeight="?attr/actionBarSize">

    <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
                                                       android:layout_height="wrap_content"
                                                       android:id="@+id/expandedToolbarContentContainer"
                                                       android:layout_marginTop="?attr/actionBarSize"
                                                       app:layout_collapseMode="parallax"/>

    <androidx.appcompat.widget.Toolbar android:layout_width="match_parent"
                                       android:layout_height="?attr/actionBarSize"
                                       app:layout_collapseMode="pin"
                                       style="@style/AppTheme.DarkToolbar"
                                       android:id="@+id/toolbarView">

        <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
                                                           android:layout_height="match_parent"
                                                           android:background="@drawable/round_outline"
                                                           style="@style/AppTheme.DarkToolbar.Container"
                                                           android:id="@+id/toolbarContentContainer"/>

    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.CollapsingToolbarLayout>

layout/list.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dish_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layoutAnimation="@anim/layout_animation_fall_down"
    />


Solution

  • Try to wrap up host_fragment with NestedScrollView with needed behaviour, like this:

     <androidx.core.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:fitsSystemWindows="true"
            android:transitionGroup="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <fragment
                android:id="@+id/nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/my_nav_host" />
        </androidx.core.widget.NestedScrollView>