首页 > 解决方案 > 在nestedscrollview滚动问题内的片段中的Recyclerview

问题描述

在活动中,我有TabLayout&FrameLayout用于加载片段。片段包含RecyclerView. 它只适用于第一次。但是当我更改选项卡并返回上一个选项卡时,RecyclerView没有滚动完整。

主要活动

<android.support.v4.widget.NestedScrollView
   android:fillViewport="true"
   android:layout_height="match_parent"
   android:layout_width="match_parent">

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

   <android.support.design.widget.TabLayout
       android:id="@+id/tabMain"
       android:layout_height="wrap_content"
       android:layout_width="match_parent" />

   <FrameLayout
       android:id="@+id/containerMain"
       android:layout_height="match_parent"
       android:layout_width="match_parent" />
   </LinearLayout>
 </android.support.v4.widget.NestedScrollView>

分段

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

   <android.support.v7.widget.RecyclerView
       android:id="@+id/rvMedia"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:nestedScrollingEnabled="false" />
</LinearLayout>

标签: androidandroid-fragmentsandroid-recyclerviewandroid-nestedscrollviewandroid-framelayout

解决方案


recyclerView 本身具有平滑滚动,但是当我们需要将 recyclerView 放在任何 scrollView 中时,它不会像下面这样工作:

布局 XML

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </ScrollView>
</LinearLayout>

解决方案是我们需要使用 nestedScrollView 而不是如下所示的滚动视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never">


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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
     </android.support.v4.widget.NestedScrollView>
</LinearLayout>

当我们使用 nestedScrollView 并将 recyclerView 放入 nestedScrollView 时会出现问题,它会根据手势以不同的速度滚动。滚动功能将不流畅。

因此,要解决此问题,您在设置适配器后要做的就是添加此行ViewCompat.setNestedScrollingEnabled(recyclerView, false);

这不是一个好的解决方案。将 RecyclerView 放置在 NestedScrollView 中会导致 RecyclerView 适配器的所有元素都被渲染,这会占用大量内存。在大多数内存较少的设备中,这可能会非常慢。

这种方法还可能导致禁用需要滚动,这将禁用视图回收,因此所有项目将立即初始化。例如,在包含 1000 个项目的列表中。这将使应用程序滞后。如果您使用分页来在用户向下滚动列表时加载固定数量的项目,则可以避免这种情况。

阅读有关分页的更多信息。

使用 RecyclerView 进行分页 – Etienne Lawlor – Medium

Android RecyclerView 分页与分页库使用 MVVM ...

分页库概述 | 安卓开发者


推荐阅读