Material-components-android: [CollapsingToolbarLayout] "scroll|exitUntilCollapsed" makes RecyclerView not scrollable to bottom after view recreation

Created on 6 Aug 2020  路  4Comments  路  Source: material-components/material-components-android

Description:
In 1st fragment I have a CollapsingToolbarLayout with pinned Toolbar. For CollapsingToolbarLayout I have set layout_scrollFlags to be "scroll|exitUntilCollapsed" since I wanted Toolbar to stay pinned.

When I scroll down so that CollapsingToolbarLayout gets collapsed, then click on any item inside RecyclerView to navigate to next screen and then come back to 1st screen after clicking on back button I can't see nor scroll to last item. I can scroll to the very bottom if I first scroll to top and make CollapsingToolbarLayout expanded.

_It looks like CollapsingToolbarLayout eats from bottom of RecyclerView a vertical space equal to height of Toolbar._

But if I remove exitUntilCollapsed from layout_scrollFlags and use others flags, the problem is gone but toolbar gets collapsed.

20200805_153417

Expected behavior: RecyclerView should be able to scroll until very bottom.

Source code: I created a test project where you can see the problem. https://github.com/MaratTursynbek/RecyclerView-test

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    tools:context=".first.FirstFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay.Light"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:statusBarScrim="@color/colorTransparent"
            app:titleEnabled="false"
            app:toolbarId="@+id/toolbar">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/first_fragment_tv_first_row"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="?attr/actionBarSize"
                    android:layout_marginEnd="16dp"
                    android:gravity="start"
                    android:text="First row"
                    android:textColor="@color/colorWhite"
                    android:textSize="30sp"
                    android:textStyle="bold"
                    app:layout_constrainedWidth="true"
                    app:layout_constraintBottom_toTopOf="@+id/first_fragment_tv_second_row"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_goneMarginBottom="16dp" />

                <TextView
                    android:id="@+id/first_fragment_tv_second_row"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="8dp"
                    android:fontFeatureSettings="smcp,c2sc"
                    android:gravity="start"
                    android:text="Second row"
                    android:textColor="@color/colorWhite"
                    android:textSize="16sp"
                    app:layout_constrainedWidth="true"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/first_fragment_tv_first_row" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="First Fragment"
                    android:textColor="@color/colorWhite"
                    android:textSize="16sp"
                    android:textStyle="bold" />

            </androidx.appcompat.widget.Toolbar>

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

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

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone" />

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Android API version: I tested on Android API 29

Material Library version: I have tried material library versions 1.1.0 and 1.2.0 and latest 1.3.0-alpha02. The problem looks to be present on all of these versions.

Device: Samsung Galaxy A10, OnePlus 5T, Pixel 3 emulator.

bug

Most helpful comment

I'm also having the exact same issue. 馃憢

As I am using a CollapsingToolbarLayout inside a NavHostFragment and a BottomNavigationView (on the activity) I was thinking about some issue on my side but removing exitUntilCollapsed solved it.

Using the following code in onCreateView makes it usable for me, scroll is not restored correctly but at least the user can reach the item.

    view.postDelayed({ view.requestLayout() }, 0)

Seems linked to #1019 Video seems to point out the same issue.

All 4 comments

I also encounter the same issue. Really looking forward to the solution

I'm also having the exact same issue. 馃憢

As I am using a CollapsingToolbarLayout inside a NavHostFragment and a BottomNavigationView (on the activity) I was thinking about some issue on my side but removing exitUntilCollapsed solved it.

Using the following code in onCreateView makes it usable for me, scroll is not restored correctly but at least the user can reach the item.

    view.postDelayed({ view.requestLayout() }, 0)

Seems linked to #1019 Video seems to point out the same issue.

This is a duplicate of #1558 and it has been fixed today in a21a300

Thanks @NitroG42!

Was this page helpful?
0 / 5 - 0 ratings