首页 > 解决方案 > 如何在具有平滑滚动的同一布局中拥有两个 recyclerview(先水平然后垂直)

问题描述

我在同一个布局中有两个 recyclerview(首先是水平的,然后是垂直的),我希望两者一起向上滚动,所以我使用了 NestedScrollView,但它带来了一些严重的滚动问题。它滞后并导致崩溃一段时间。

我已经看到一些人建议nestedScrollingEnabled = "false"在 RecyclerView 中使用,但我已经尝试过,但仍然没有结果。

关于如何实现这一目标的任何想法?

这是xml顺便说一句

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/appbar"
            android:clipToPadding="false"
            android:orientation="vertical">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/stories_recycler_view"
                storiesData="@{viewModel.stories}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:orientation="horizontal"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                tools:listitem="@layout/item_story" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/posts_recycler_view"
                feedListData="@{viewModel.entries}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:orientation="vertical"
                app:stackFromEnd="true"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                app:paddingBottomSystemWindowInsets="@{true}"
                app:paddingTopSystemWindowInsets="@{true}"
                app:reverseLayout="true"
                tools:listitem="@layout/item_text_post" />

        </LinearLayout>

标签: androidandroid-recyclerviewnestedscrollview

解决方案


 <androidx.core.widget.NestedScrollView
android:id="@+id/nested_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout>
...
Add `android:nestedScrollingEnabled="true"` in both Recyclerview Tag...<br>
...
</LinearLayout>

<androidx.core.widget.NestedScrollView/>

推荐阅读