问题描述
使用带有 RecyclerView 的 CollapsingToolBarLayout 时,我发现工具栏在滚动时折叠不一致。在任何东西崩溃之前,我可以在 recyclerview 中滚动大约一半,然后它会断断续续地崩溃。这种行为并不总是一致的——有时它会比其他人崩溃得更多。然而,当我滚动到底部时,它总是完全折叠。我的 XML 如下所示:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|enteralways|enteralwaysCollapsed" >
<com.google.android.material.appbar.MaterialToolbar
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_collapseMode="pin" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
解决方法
事实证明,最终的问题是在第一个的一些视图持有者中拥有第二个 RecyclerView。根据 this answer,在内部 RecyclerView 上设置 setNestedScrollingEnabled(false)
会导致事情按预期运行。