Yes92
Yes92

Reputation: 311

Recyclerview very slow to load

I have a recycler view in an Activity where sometimes I show a lot of elements. 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.

Upvotes: 4

Views: 8850

Answers (2)

Luka Radujevic
Luka Radujevic

Reputation: 61

I had a problem with a very small amount of items. I'm using Room Dao. The reason why RecycleView ran so slow was the fact that photos that l used were large in size. So be careful about that.

Upvotes: 2

GianhTran
GianhTran

Reputation: 3711

I think the best solution is not loading all data, just using lazy-loading

Refer here

Upvotes: 5

Related Questions