material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[CollapsingToolbarLayout] CollapsingToolbarLayout scroll issue with ViewPager

Open WojciechLatala opened this issue 6 years ago • 9 comments

Description: CollapsingToolbarLayout stops reacting to scroll when ViewPager2 with FragmentStateAdapter changes page (occurs with either ViewPager or ViewPager2). Happens if ViewPager's Fragment content has either NestedScrollView or RecyclerView inside.

Steps to reproduce:

  1. Run the app.
  2. Toolbar collapses correctly while dragging AppBar or ViewPager content up and down.
  3. Swipe ViewPager right to get to the next page (do not scroll the ViewPager content!).
  4. Dragging AppBar to collapse/expand Toolbar is not working anymore.
  5. Scrolling by dragging ViewPager2 fragment enables Toolbar collapse/enlargement by dragging AppBar again.

gif error

In the case of ViewPager the error does not occur when swiped out page is still within the ViewPager2.offscreenPageLimit (eg. when offscreenPageLimit = 1, swiping from page 0 -> 1 does not break the scroll, but from 1 -> 2 does). Essentialy, for view pager it happens when fragment that had been scrolled gets out of ViewPager.offscreenPageLimit range.

Expected behavior: Toolbar should respond to scrolls and collapse/enlarge in the same way before and after a ViewPager page change.

Source code: GitHub repo to reproduce the issue. Created by adding ViewPager to AndroidStudio's new "ScrollingActivity" project: https://github.com/WojciechLatala/CoordinatorViewPager

Source code for the layout:

<?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:fitsSystemWindows="true"
        tools:context=".ScrollingActivity">
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/app_bar_height"
            android:fitsSystemWindows="true"
            android:theme="@style/Theme.CoordinatorTabsExample.AppBarOverlay">
    
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:toolbarId="@+id/toolbar">
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/Theme.CoordinatorTabsExample.PopupOverlay" />
    
            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>
    
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/view_pager2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#00FF00"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

Android API version: min: 22, target/compile: 29

Material Library version: 1.2.1, error still occurs on 1.3.0-alpha03

Device: Sony Xperia Android 9.0 (and many others).

WojciechLatala avatar Nov 17 '20 17:11 WojciechLatala

I can confirm this issue.

Android API version: min: 23, target/compile: 30 Material Library version: 1.3.0 Device: Google Pixel 2, Android 11

Seil0 avatar Feb 07 '21 00:02 Seil0

Same issue here :

Possible reason :

It seems that the issue is coming from the method AppBarLayout.canDragView(T view)

This method uses the property lastNestedScrollingChildRef which is the latest target view passed to the method AppBarLayout.onStopNestedScroll

Since the (target) view in the viewPager has been swiped out, lastNestedScrollingChildRef is not visible anymore and the following condition in AppBarLayout.canDragView returns false

 return scrollingView != null
            && scrollingView.isShown() // return false because ViewPager has been swiped
            && !scrollingView.canScrollVertically(-1);

brozot avatar Feb 08 '21 12:02 brozot

@brozot It seems you have dug deeper than I managed to. Did you perhaps manage to find a workaround for this?

WojciechLatala avatar Feb 08 '21 12:02 WojciechLatala

@brozot It seems you have dug deeper than I managed to. Did you perhaps manage to find a workaround for this?

Setting ViewPager2.offscreenPageLimit to a value higher than the added pages works around the issue for me.

Seil0 avatar Feb 08 '21 12:02 Seil0

@brozot It seems you have dug deeper than I managed to. Did you perhaps manage to find a workaround for this?

Setting ViewPager2.offscreenPageLimit to a value higher than the added pages works around the issue for me.

Yeah, I thought about using that, but unfortunately, as I am working with memory-heavy fragments, having more than 1 offscreenPageLimit is a no-go in my case. And the problem also happens then, when I go from page 2 to page 3, and page 1 gets recycled.

WojciechLatala avatar Feb 08 '21 12:02 WojciechLatala

You can create a custom AppBarLayout that sets its own DragCallback like this:

class CustomAppBarLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppBarLayout(context, attrs, defStyleAttr) {

    override fun getBehavior(): CoordinatorLayout.Behavior<AppBarLayout> {
        return Behavior().also {
            it.setDragCallback(object : Behavior.DragCallback() {
                override fun canDrag(appBarLayout: AppBarLayout) = true
            })
        }
    }
}

Nickoludis avatar Apr 07 '21 10:04 Nickoludis

I forgot about this, good thing I got email reminder. Thanks @Nickoludis , after quick test it seems to be working nicely ;) Would be nice if we didn't have to make this workaround and a proper solution would be included in the library though.

WojciechLatala avatar Apr 08 '21 09:04 WojciechLatala

I forgot about this, good thing I got email reminder.

Thanks @Nickoludis , after quick test it seems to be working nicely ;)

Would be nice if we didn't have to make this workaround and a proper solution would be included in the library though.

You're welcome 🙂

I had that bug ticket for ages assigned to me, and tried several times to fix it.

Shame on you Google 😄

Nickoludis avatar Apr 08 '21 10:04 Nickoludis

You can create a custom AppBarLayout that sets its own DragCallback like this:

class CustomAppBarLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppBarLayout(context, attrs, defStyleAttr) {

    override fun getBehavior(): CoordinatorLayout.Behavior<AppBarLayout> {
        return Behavior().also {
            it.setDragCallback(object : Behavior.DragCallback() {
                override fun canDrag(appBarLayout: AppBarLayout) = true
            })
        }
    }
}

Thanks @Nickoludis. This CustomAppBarLayout solved my problem though 😄

Krishnasony avatar Aug 25 '21 08:08 Krishnasony