android - NestedScrollView 内的 RecyclerView 导致加载缓慢和/或崩溃

标签 android android-recyclerview android-view android-nestedscrollview

我正在使用 RecyclerView,它将用于加载可能的许多图像,我希望在 RecyclerView 上方有一个操作栏,如下所示:

enter image description here

但我也希望操作栏在最顶部只有灰色背景。如果用户滚动,它应该像这样完全透明:

enter image description here

通过将其用作我的布局(有一个主要问题),我已经完成了我想要的事情:

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 是让我的整个 fragment 滚动而不仅仅是 RecyclerView 的关键,但它会使我的应用程序在加载大量图像(500+)时变慢或无响应。有没有更好的方法来创建我正在寻找的东西?我已经尝试寻找解决方案,但我能找到的只是“不要在 nestedScrollViews 中使用 recyclerviews”,而没有提供任何替代解决方案。

如果这也很重要,我正在使用 Glide 将图像加载到每个 ImageView(在 recyclerview 内)。

而且我已经在我的 RecyclerView 上使用它了:

mAlbumRecyclerView.setNestedScrollingEnabled(false);

最佳答案

每当您在 RecyclerView 上使用 wrap_content 时,您都会遇到性能问题。使用 wrap_content 完全抵消了您从回收 View 中获得的性能改进。因此,接下来的问题是如何在不使用 wrap_content 的情况下做您想做的事。

您可以做的一件事是使用 FrameLayoutToolbar 覆盖在您的 RecyclerView 之上,然后在 上使用填充code>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>

关于android - NestedScrollView 内的 RecyclerView 导致加载缓慢和/或崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51886382/

相关文章:

Android:我应该在哪里保存临时文件?

java - DrawerLayout onDrawerClosed() 调用两次

android - 过度滚动效果不会出现在我的 swiperefreshlayout 内的 recyclerview 中

android - 必须是 : View. VISIBLE、View.INVISIBLE、View.GONE 之一

android - offsetLeftAndRight() 究竟做了什么?

android - Android 单声道 : Map Balloons for Mono

android - Android 支持线程吗?

android - 在 CardView 中滚动 RecyclerView 非常慢

android - 如何使用约束布局设置recyclerview最小高度wrap_content但最大百分比?

android - 给定 Activity 中的嵌套类,如何初始化自定义 surfaceView?