我在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>
如果这很重要,我会将它们与片段代码中的FragmentStateAdapter
和TabLayoutMediator
连接起来。
答案 0 :(得分:0)
该错误不在我的RecyclerView
或ViewPager2
设置中。我也在使用AppBarLayout
。我没有将app:layout_scrollFlags="scroll|enterAlways"
放在包含ViewPager2
和RecyclerView
的子布局上。但是AppBarLayout
的每个子级必须指定app:layout_scrollFlags
才能滚动。
此外,ViewPager2
和RecyclerView
不应是AppBarLayout
的子代,而应位于AppBarLayout
标记的正下方。我将它们移出了AppBarLayout
,现在一切正常。