title: AppBar not scrolling with nested ViewPager2
body: <p>I have a view hierarchy as shown in the image below.</p>
  <p><a href="https://i.stack.imgur.com/tdUty.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tdUty.png" alt="View hierarchy" /></a></p>
  <p>I'm getting strange scroll behaviors like,</p>
  <ol>
  <li>If I scroll (drag slowly or fling) from Area 1 the <code>AppBar</code> collapses along with it. This is fine.</li>
  <li>But if I drag slowly from Area 2 the <code>AppBar</code> does not collapse. It stays there and <code>RecyclerView</code> goes beneath it. However, it works fine with a fling.</li>
  </ol>
  <p><code>activity_challenge_detail.xml</code></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;androidx.swiperefreshlayout.widget.SwipeRefreshLayout
          android:id=&quot;@+id/swipeRefresh&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          tools:context=&quot;.challengedetail.ChallengeDetailActivity&quot;&gt;
  
          &lt;androidx.coordinatorlayout.widget.CoordinatorLayout
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;&gt;
  
              &lt;com.google.android.material.appbar.AppBarLayout
                  android:id=&quot;@+id/app_bar&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:background=&quot;@color/black&quot;&gt;
  
                  &lt;com.google.android.material.appbar.CollapsingToolbarLayout
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      app:contentScrim=&quot;@color/black&quot;
                      app:layout_constraintStart_toStartOf=&quot;parent&quot;
                      app:layout_constraintTop_toTopOf=&quot;parent&quot;
                      app:layout_scrollFlags=&quot;scroll|exitUntilCollapsed&quot;&gt;
  
                      &lt;androidx.constraintlayout.widget.ConstraintLayout
                          android:id=&quot;@+id/header&quot;
                          android:layout_width=&quot;match_parent&quot;
                          android:layout_height=&quot;wrap_content&quot;
                          app:layout_collapseMode=&quot;parallax&quot;
                          app:layout_collapseParallaxMultiplier=&quot;0.2&quot;&gt;
  
                          &lt;FrameLayout
                              android:id=&quot;@+id/challengeBannerFrame&quot;
                              android:layout_width=&quot;match_parent&quot;
                              android:layout_height=&quot;0dp&quot;
                              android:foreground=&quot;@drawable/banner_gradient&quot;
                              app:layout_constraintDimensionRatio=&quot;H,1:1&quot;
                              app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                              app:layout_constraintStart_toStartOf=&quot;parent&quot;
                              app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
  
                              &lt;ImageView
                                  android:id=&quot;@+id/challengeBanner&quot;
                                  android:layout_width=&quot;match_parent&quot;
                                  android:layout_height=&quot;match_parent&quot;
                                  android:contentDescription=&quot;@string/challenge_banner&quot;
                                  android:scaleType=&quot;centerCrop&quot;
                                  tools:src=&quot;@tools:sample/avatars&quot; /&gt;
                          &lt;/FrameLayout&gt;
                      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
                      &lt;androidx.appcompat.widget.Toolbar
                          android:layout_width=&quot;wrap_content&quot;
                          android:layout_height=&quot;wrap_content&quot;
                          android:minHeight=&quot;@dimen/dp16&quot;
                          app:layout_collapseMode=&quot;pin&quot;&gt;
  
                          &lt;com.company.widget.StatusBarSpacer
                              android:layout_width=&quot;match_parent&quot;
                              android:layout_height=&quot;wrap_content&quot; /&gt;
                      &lt;/androidx.appcompat.widget.Toolbar&gt;
                  &lt;/com.google.android.material.appbar.CollapsingToolbarLayout&gt;
  
                  &lt;androidx.appcompat.widget.Toolbar
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:background=&quot;@android:color/transparent&quot;
                      app:contentInsetEnd=&quot;0dp&quot;
                      app:contentInsetStart=&quot;0dp&quot;
                      app:layout_collapseMode=&quot;pin&quot;&gt;
  
                      &lt;androidx.constraintlayout.widget.ConstraintLayout
                          android:layout_width=&quot;match_parent&quot;
                          android:layout_height=&quot;match_parent&quot;
                          android:background=&quot;@android:color/transparent&quot;&gt;
  
                          &lt;com.google.android.material.tabs.TabLayout
                              android:id=&quot;@+id/switchingTabsBar&quot;
                              android:layout_width=&quot;match_parent&quot;
                              android:layout_height=&quot;@dimen/dp0&quot;
                              android:background=&quot;@drawable/switching_tab_bg&quot;
                              app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
                              app:layout_constraintDimensionRatio=&quot;4&quot;
                              app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                              app:layout_constraintStart_toStartOf=&quot;parent&quot;
                              app:layout_constraintTop_toTopOf=&quot;parent&quot;
                              app:tabBackground=&quot;@drawable/active_tab_selector&quot;
                              app:tabIconTint=&quot;@color/black&quot;
                              app:tabIndicator=&quot;@drawable/active_tab_indicator&quot;
                              app:tabIndicatorColor=&quot;@color/yellow_500&quot;
                              app:tabMode=&quot;fixed&quot;
                              app:tabRippleColor=&quot;@null&quot; /&gt;
  
                      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
                  &lt;/androidx.appcompat.widget.Toolbar&gt;
  
              &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  
              &lt;androidx.viewpager2.widget.ViewPager2
                  android:id=&quot;@+id/challengeDetailsViewPager&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot; /&gt;
          &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
  
      &lt;/androidx.swiperefreshlayout.widget.SwipeRefreshLayout&gt;
  </code></pre>
  <p><code>fragment_challenge_post.xml</code></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:background=&quot;@drawable/gradient_challenge_post&quot;
          app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;
          tools:context=&quot;.challengedetail.fragment.ChallengePostFragment&quot;&gt;
      &lt;androidx.recyclerview.widget.RecyclerView
              android:id=&quot;@+id/challengePostRecyclerView&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
              app:layout_constraintEnd_toEndOf=&quot;parent&quot;
              app:layout_constraintStart_toStartOf=&quot;parent&quot;
              app:layout_constraintTop_toTopOf=&quot;parent&quot;
              app:layout_constraintVertical_bias=&quot;0.0&quot;
              tools:itemCount=&quot;1&quot;
              tools:listitem=&quot;@layout/list_item_post&quot; /&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p><code>list_item_post.xml</code></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;com.google.android.material.card.MaterialCardView
          android:id=&quot;@+id/cardView&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          app:cardBackgroundColor=&quot;@color/white&quot;
          app:cardCornerRadius=&quot;@dimen/dp16&quot;
          app:cardElevation=&quot;@dimen/dp0&quot;
          app:strokeColor=&quot;@color/gray_f5&quot;
          app:strokeWidth=&quot;@dimen/dp1&quot;&gt;
  
          &lt;androidx.constraintlayout.widget.ConstraintLayout
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:paddingBottom=&quot;@dimen/dp16&quot;&gt;
  
              &lt;com.google.android.material.imageview.ShapeableImageView
                  android:id=&quot;@+id/userImageView&quot;
                  android:layout_width=&quot;@dimen/dp48&quot;
                  android:layout_height=&quot;@dimen/dp48&quot;
                  android:layout_marginStart=&quot;16dp&quot;
                  android:layout_marginTop=&quot;16dp&quot;
                  android:scaleType=&quot;centerCrop&quot;
                  app:layout_constraintStart_toStartOf=&quot;parent&quot;
                  app:layout_constraintTop_toTopOf=&quot;parent&quot;
                  app:shapeAppearanceOverlay=&quot;@style/ShapeAppearance.userProfileImage&quot;
                  tools:src=&quot;@tools:sample/avatars&quot; /&gt;
  
              &lt;TextView
                  android:id=&quot;@+id/userNameText&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_marginStart=&quot;16dp&quot;
                  android:layout_marginTop=&quot;16dp&quot;
                  android:lineSpacingExtra=&quot;5sp&quot;
                  android:textAppearance=&quot;@style/Inter.Semi.16&quot;
                  app:layout_constraintStart_toEndOf=&quot;@+id/userImageView&quot;
                  app:layout_constraintTop_toTopOf=&quot;parent&quot;
                  tools:text=&quot;@tools:sample/full_names&quot; /&gt;
  
              &lt;TextView
                  android:id=&quot;@+id/timestampText&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_marginStart=&quot;16dp&quot;
                  android:lineSpacingExtra=&quot;7sp&quot;
                  android:textAppearance=&quot;@style/Inter.Regular.14&quot;
                  app:layout_constraintStart_toEndOf=&quot;@+id/userImageView&quot;
                  app:layout_constraintTop_toBottomOf=&quot;@+id/userNameText&quot;
                  tools:text=&quot;2 hrs ago&quot; /&gt;
  
              &lt;com.company.widget.NestedScrollableHost
                  android:id=&quot;@+id/viewPagerHost&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_marginTop=&quot;8dp&quot;
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                  app:layout_constraintStart_toStartOf=&quot;parent&quot;
                  app:layout_constraintTop_toBottomOf=&quot;@+id/userImageView&quot;
                  tools:layout_constraintDimensionRatio=&quot;1:1&quot;&gt;
  
                  &lt;androidx.viewpager2.widget.ViewPager2
                      android:id=&quot;@+id/postImagesViewPager&quot;
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;wrap_content&quot; /&gt;
              &lt;/com.company.widget.NestedScrollableHost&gt;
          &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
      &lt;/com.google.android.material.card.MaterialCardView&gt;
  </code></pre>
  <p>I have tried solutions to other questions as well like wrapping the nested <code>ViewPager2</code> by <a href="https://github.com/android/views-widgets-samples/blob/master/ViewPager2/app/src/main/java/androidx/viewpager2/integration/testapp/NestedScrollableHost.kt" rel="nofollow noreferrer">NestedScrollableHost</a> class. But it did not seem to work. Any ideas?</p>