android - NestedScrollView 内的 RecyclerView 导致加载缓慢和/或崩溃
问题描述
我正在使用一个 RecyclerView ,它将用于加载潜在的许多图像,我希望在 RecyclerView 上方有一个操作栏,如下所示:
但我也希望操作栏的顶部只有灰色背景。如果用户滚动,它应该是完全透明的,如下所示:
通过使用它作为我的布局,我已经完成了我想要的(有一个主要问题):
fragment_recycler_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Gray bar at top -->
<View
android:id="@+id/gray_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@null" />
<android.support.v7.widget.RecyclerView 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"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
NestedScrollView 是让我的整个片段滚动而不是仅滚动 RecyclerView 的关键,但它使我的应用程序在加载大量图像(500+)时变慢或无响应。有没有更好的方法来创建我正在寻找的东西?我试过寻找解决方案,但我能找到的只是“不要在nestedScrollViews 中使用recyclerviews”,而没有提供任何替代解决方案。
如果这也很重要,我正在使用 Glide 将图像加载到每个 ImageView(在 recyclerview 中)。
而且我已经在我的 RecyclerView 上使用了它:
mAlbumRecyclerView.setNestedScrollingEnabled(false);
解决方案
任何时候你wrap_content
在 RecyclerView 上使用,你都在为性能问题做准备。使用wrap_content
完全破坏了从回收视图中获得的性能改进。因此,问题是如何在不使用wrap_content
.
您可以做的一件事是使用 aFrameLayout
将 a 覆盖Toolbar
在您的 顶部RecyclerView
,然后在RecyclerView
( 与 结合使用android:clipToPadding="false"
) 上使用填充以使事情看起来从操作栏下方开始。只要你的工具栏有一个半透明的背景色,当你滚动时,你的 recyclerview 内容就会出现在它的下方。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
android:clipToPadding="false"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="@layout/itemview"/>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#1111"
app:title="Hello world"/>
</FrameLayout>
推荐阅读
- php - CakePHP 2 会话突然/过早结束
- javascript - 如何在 React 的函数中获取 useEffect?
- java - ./gradlew 在 docker 容器中找不到
- python - 用于查找矩阵和的顶部特征值的 Lanczos 算法
- node.js - npm audit fix 命令无法解决漏洞
- sql-server - 寻找 Linq 查询
- node.js - sveltekit 中未定义的 process.env 环境变量
- deep-learning - 梯度累积问题
- java - 字符串未显示在输出中
- python - 如何在 Python 中的密度颜色图上绘制密度线图