[CollapsingToolbarLayout] CollapsingToolbarLayout scroll issue with ViewPager
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:
- Run the app.
- Toolbar collapses correctly while dragging AppBar or ViewPager content up and down.
- Swipe ViewPager right to get to the next page (do not scroll the ViewPager content!).
- Dragging AppBar to collapse/expand Toolbar is not working anymore.
- Scrolling by dragging ViewPager2 fragment enables Toolbar collapse/enlargement by dragging AppBar again.

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).
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
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 It seems you have dug deeper than I managed to. Did you perhaps manage to find a workaround for this?
@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.
@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.
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
})
}
}
}
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.
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 😄
You can create a custom
AppBarLayoutthat sets its ownDragCallbacklike 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 😄