android - 如何在 NestedScrollView 中放置 RecyclerView?
问题描述
我在 NestedScrollView 中有一个 RecyclerView。当 recyclerView 适配器列表大小较小时,它工作正常。但是当列表大小很大时,它会使我的应用程序崩溃(或造成更多延迟)。我该如何解决。代码如下。
XML:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.textview.MaterialTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:text="0 Products"
android:textSize="16sp"
android:theme="@style/radio_font_textview" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/products_recycler"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
爪哇:
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ProductView.this);
productRecyclerAdapter = new ProductViewRecyclerAdapter(mData,mData2,ProductView.this);
products_recycler.setAdapter(productRecyclerAdapter);
ViewCompat.setNestedScrollingEnabled(products_recycler, false);
products_recycler.setLayoutManager(linearLayoutManager);
解决方案
回收器视图旨在解决与内存相关的问题并提高性能,因为它回收了屏幕外的视图持有者并轻松处理大量项目。
但是,当您将 aRecyclerView
放入其中时,NestedScrollView
它会失去回收ViewHolder
s 的能力,因此在将大量数据加载到其适配器中时会导致问题。
因此,要么减少要加载到回收器中的项目的大小/长度,要么提供适当的用例和崩溃堆栈跟踪,以便我们可以提出适当的解决方案
推荐阅读
- python - 计算平均python
- javascript - PHP & JavaScript - 变量
- java - 如何将 MySQL 命令的输出存储到 java 变量(JDBC)中
- azure-ad-b2c - 如何使用 B2C 自定义策略防止密码重复使用
- algorithm - 如何生成以第一个元素为轴的快速排序的最佳数组?
- javascript - 如何将嵌套对象转换为字符串索引对象
- xaml - XAML / UWP 中主题样式的快捷方式
- python - 使用傅里叶变换的时间序列特征提取
- python - 什么时候应该使用 for 循环或 while 循环?
- swift - 保存 Bool/tableView 复选标记 - 第三次幸运