简体   繁体   English

Recyclerview 加载速度很慢

[英]Recyclerview very slow to load

I have a recycler view in an Activity where sometimes I show a lot of elements.我在 Activity 中有一个回收器视图,有时我会在其中显示很多元素。 The problem is that when the number of elements is very high the recycler view needs some seconds to render the elements and it's frustrating.问题是,当元素数量非常多时,回收器视图需要几秒钟来渲染元素,这令人沮丧。 I wonder why.我想知道为什么。
My code:我的代码:

<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>

My single item:我的单品:

 <?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>

In order to update the elements I simply change the list of them and then I notify that to the recycler view.为了更新元素,我只需更改它们的列表,然后将其通知给回收站视图。

I think the best solution is not loading all data, just using lazy-loading我认为最好的解决方案不是加载所有数据,而是使用延迟加载

Refer here参考这里

I had a problem with a very small amount of items.我遇到了少量物品的问题。 I'm using Room Dao.我正在使用Room Dao。 The reason why RecycleView ran so slow was the fact that photos that l used were large in size . RecycleView 运行缓慢的原因是我使用的照片尺寸较大 So be careful about that.所以要小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM