为什么我的水平 ViewPager2 中的垂直 RecyclerView 不滚动?
Why does my vertical RecyclerView in my horizontal ViewPager2 not scroll?
我在 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
连接起来。
错误不在我的 RecyclerView
或 ViewPager2
设置中。我也在用AppBarLayout
。我没有将 app:layout_scrollFlags="scroll|enterAlways"
放在包含我的 ViewPager2
和 RecyclerView
的子布局上。但是 AppBarLayout
的每个子项都必须指定 app:layout_scrollFlags
才能使其可滚动。
此外,ViewPager2
和 RecyclerView
不应是 AppBarLayout
的子标签,而应位于 AppBarLayout
标签的正下方。我将它们移出 AppBarLayout
,现在一切正常。
我在 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
连接起来。
错误不在我的 RecyclerView
或 ViewPager2
设置中。我也在用AppBarLayout
。我没有将 app:layout_scrollFlags="scroll|enterAlways"
放在包含我的 ViewPager2
和 RecyclerView
的子布局上。但是 AppBarLayout
的每个子项都必须指定 app:layout_scrollFlags
才能使其可滚动。
此外,ViewPager2
和 RecyclerView
不应是 AppBarLayout
的子标签,而应位于 AppBarLayout
标签的正下方。我将它们移出 AppBarLayout
,现在一切正常。