android - 在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>
解决方案
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
推荐阅读
- c - 同一个C程序的三个版本,为什么第一个这么快?
- java - 当 url 不匹配时,匹配 driver.getcurrenturl () 和预期 url 报告的断言通过
- electron - 防止 Electron App 附加到控制台窗口
- android-studio - Android Studio statvfs
- java - 覆盖函数未更新全局变量
- java - 按 ID 查找元素无法识别 - Selenium
- symfony - 反序列化时自动合并相关项
- javascript - 通过原型继承扩展 ES2015
- python - 打印一个列表并从每个新行中删除 '\n',给出错误
- python - 获取使用 pysftp Connection.walktree 迭代的文件和文件夹的属性?