CoordinatorLayout中的ViewPager(用于粘性标题)会导致滚动中断



在此处测试演示问题的应用程序:https://github.com/jstubing/ViewPagerTest

我使用CoordinatorLayout/AppBarLayout组合来实现可滚动页面中的粘性标题。页面的最底部是ViewPage2。当我滑动到一个新的ViewPager页面时,内容会异步加载,并在ViewPager中填充一个RecyclerView。这一切都很好。

当我滑动到一个新的ViewPager页面时,会出现问题。此时,内容加载和渲染都很好,但我再也无法在ViewPager之外启动滚动。换句话说,在";"上页内容";部分(请参阅XML(不执行任何操作。

但是,在ViewPager中向上和向下滚动确实有效。如果我在ViewPager中向上或向下滚动,甚至只点击ViewPager一次,它会修复所有问题,我可以再次从ViewPager外部启动滚动。

我试着切换到最初的ViewPager而不是ViewPager2,它肯定更可靠,但问题仍然偶尔发生。

你知道我该怎么解决这个问题吗?最终,我只想能够在ViewPager页面之间滑动,并使整个活动保持可滚动。我很困惑,为什么我一开始就失去了滚动能力,为什么点击ViewPager可以修复它

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:elevation="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background"
app:elevation="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
app:layout_scrollFlags="scroll">
<!-- UPPER PAGE CONTENT -->
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- Sticky header -->
<include
android:id="@+id/sticky_header"
layout="@layout/sticky_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/events_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

滑动到新的ViewPager页面时出现问题。此时点,内容加载并渲染良好,但我再也无法在ViewPager之外启动滚动。

我再次能够从ViewPager外部启动滚动。

因此,现在问题仅限于ViewPager。很可能是因为ViewPager2支持在其内部RecyclerView中嵌套滚动(我不是指页面的RecyclerView,而是使ViewPager在内部发挥作用的那个

所以,我们首先需要禁用ViewPager2:的嵌套滚动

Kotlin:

viewPager.children.find { it is RecyclerView }?.let {
(it as RecyclerView).isNestedScrollingEnabled = false
}

Java:

for (int i = 0; i < viewPager.getChildCount(); i++) {
View child = viewPager.getChildAt(i);
if (child instanceof RecyclerView) 
child.setNestedScrollingEnabled(false);
}

我们不能完全禁用嵌套滚动,因为这会严重影响外部滚动,所以,我们可以通过包装ViewPager:的NestedScrollView来操作它

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/events_pager"
android:layout_width="match_parent"
android:layout_height="match_parent />
</androidx.core.widget.NestedScrollView>

最新更新