question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[CollapsingToolbarLayout] CollapsingToolbarLayout scroll issue with ViewPager

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

3reactions
Nickoludiscommented, Apr 7, 2021

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
            })
        }
    }
}
1reaction
WojciechLatalacommented, Apr 8, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CollapsingToolbarLayout and ViewPager Scroll not work , not ...
The problem is that if I have a tab in the viewPager with a recycleview list, the toolbar collapses properly, and the scroll...
Read more >
[AppBarLayout] liftOnScroll not working with ViewPager ...
I have "scroll" not set because I don't want the CollapsingToolbarLayout to shrink when my RecyclerView is empty, and most of the time...
Read more >
Using TabLayout and ViewPager with CollapsingToolbarLayout
Using android TabLayout and new viewpager2 you can make your fragment layout inside tab layout to make them have expand collapse.
Read more >
How to Create a Collapsing ToolBar and FAB on Scroll with ...
In this article, we are going to create a collapsing Toolbar and Floating action button (FAB) on scroll with RecyclerView.
Read more >
How to do AppBarLayout with Collapsing ToolbarLayout that ...
CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" ... To solve the problem of i cant scroll the list , you can try the following ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found