[CollapsingToolbarLayout] CollapsingToolbarLayout scroll issue with ViewPager
See original GitHub issueDescription: 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).
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:9
Top GitHub Comments
You can create a custom
AppBarLayout
that sets its ownDragCallback
like this: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.