【问题标题】:Recyclerview very slow to loadRecyclerview 加载速度很慢
【发布时间】:2018-06-24 18:40:38
【问题描述】:

我在 Activity 中有一个回收站视图,有时我会显示很多元素。问题是当元素数量非常多时,回收器视图需要几秒钟来渲染元素,这很令人沮丧。我想知道为什么。
我的代码:

<android.support.v4.widget.NestedScrollView 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/_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cct.appload.fragments.FileManagerFragment">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="true">

            <Spinner
                android:id="@+id/_order_spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:contentDescription="@string/back"
                android:src="@drawable/ic_arrow_back_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>


        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:elevation="1dp"
            card_view:cardCornerRadius="0dp"
            card_view:cardElevation="2dp">

            <TextView
                android:id="@+id/_msg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:gravity="center"
                android:visibility="invisible" />

            <ProgressBar
                android:id="@+id/manager_progress_bar"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:progressTint="@color/colorAccent"
                android:visibility="invisible" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/_recycler_view"
                android:layout_width="match_parent"
                android:nestedScrollingEnabled="false"
                android:layout_height="wrap_content"
                android:paddingStart="8dp"
                android:paddingEnd="8dp"
                android:scrollbars="vertical" />
        </android.support.v7.widget.CardView>
    </LinearLayout>

我的单品:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/_icon"
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle_background"
        android:padding="10dp"
        android:tint="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/_separator"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_folder_black_24dp" />

    <TextView
        android:id="@+id/_titolo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:ellipsize="end"
        android:lines="1"
        android:maxLines="1"
        android:text="Titolo"
        android:textAppearance="@android:style/TextAppearance.Material.Body2"
        app:layout_constraintEnd_toStartOf="@+id/_menu"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/_icon"
        app:layout_constraintTop_toTopOf="@+id/_icon" />

    <TextView
        android:id="@+id/_dimensione"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:padding="0dp"
        android:text="dimensione"
        android:ellipsize="end"
        android:lines="1"
        app:layout_constraintBottom_toBottomOf="@+id/_icon"
        app:layout_constraintEnd_toStartOf="@+id/_menu"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/_icon"
        app:layout_constraintTop_toBottomOf="@+id/_titolo" />

    <ImageButton
        android:id="@+id/_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_more_vert_black_24dp" />

    <View
        android:id="@+id/_separator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="0dp"
        android:background="@color/lightGrey"
        app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>

为了更新元素,我只需更改它们的列表,然后通知回收站视图。

【问题讨论】:

  • “非常高”是什么意思? 300 或 3000 还是...?你显示什么样的数据?只是文字还是一些图像?您在哪种设备/模拟器上运行您的代码?最后同样重要的是:请分享足够多的代码(以及列表行的布局),以便我们重现问题。
  • @0X0nosugar 我编辑了这个问题。我不知道我要展示多少个项目,我想有几百个。是文本和图像(如您所见)。我正在使用我的 moto g5 plus。
  • 您是否正在使用 DiffUtil(或新的 ListAdapter,实现 DiffUtil)?在这种情况下,您应该知道将许多项目添加到回收器适配器可能需要一些时间,因为 DiffUtil 必须遍历所有项目并比较差异。

标签: java android performance android-recyclerview


【解决方案1】:

我认为最好的解决方案是不加载所有数据,只使用延迟加载

参考here

【讨论】:

    【解决方案2】:

    我的商品数量非常少。我正在使用 Room Dao。 RecycleView 运行如此缓慢的原因是我使用的照片尺寸很大。所以要小心。

    【讨论】:

      猜你喜欢
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多