为什么我的水平ViewPager2中的垂直RecyclerView无法滚动?

时间:2020-04-07 20:17:14

标签: android android-recyclerview android-viewpager2

我在ViewPager2中的RecyclerView无法滚动。

The documentation of ViewPager2的一节中介绍了嵌套的可滚​​动元素,但它的内容与ViewPager2具有相同的方向。我在ViewPager2(水平)和RecyclerView(垂直)中有不同的方向。

如何使RecyclerView滚动?我究竟做错了什么? 布局文件中的代码:

RecyclerView片段代码:

<layout 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">

     // ...

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/pieces_list"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</layout>

包含ViewPager2的片段代码:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.google.android.material.tabs.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/tabs"
            style="@style/AppTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed"
            app:tabTextAppearance="@style/AppTabTextAppearance" />

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

如果这很重要,我会将它们与片段代码中的FragmentStateAdapterTabLayoutMediator连接起来。

1 个答案:

答案 0 :(得分:0)

该错误不在我的RecyclerViewViewPager2设置中。我也在使用AppBarLayout。我没有将app:layout_scrollFlags="scroll|enterAlways"放在包含ViewPager2RecyclerView的子布局上。但是AppBarLayout的每个子级必须指定app:layout_scrollFlags才能滚动。

此外,ViewPager2RecyclerView不应是AppBarLayout的子代,而应位于AppBarLayout标记的正下方。我将它们移出了AppBarLayout,现在一切正常。