Оптимизация фрагмента в Android

У меня существует ScrollView, в котором находится LinearLayout, а вот в нем уже лежат 4 FrameLayout'а. Так вот, эти четыре фрейма заменяются фрагментами с фотографиями, в каждом из которых их по 36 штук. При добавлении фрагмента Scroll очень сильно начинает зависать, и неважно сколько времени пройдет, от "прогрузки" ничего не меняется. Вот в инстаграме в профиле, когда листаешь фотки, ничего не зависает. Я наверное запутал вас, но мне интересно, как там реализован Scroll с большим количеством фотографий или как мне можно оптимизировать свой Scroll ? Вот такой кусок кода на 9 фотографий, в каждом фрагменте их 4. Фрагментов тоже 4. Но скролл лагает даже при 18 фотографиях, меньше не проверял.

    <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:orientation="horizontal"
        android:padding="8dp"
        android:weightSum="3"
        app:layout_constraintDimensionRatio="H,1:1"
        android:baselineAligned="false">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:weightSum="3"
            android:layout_marginEnd="4dp"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/image_1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/colorBlack"
                android:scaleType="centerCrop"
                android:layout_marginBottom="4dp"
                android:src="@drawable/ph_1"
                tools:ignore="ContentDescription,NestedWeights" />
            <ImageView
                android:id="@+id/image_2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginTop="4dp"
                android:background="@color/colorBlack"
                android:scaleType="centerCrop"
                android:src="@drawable/ph_4"
                android:layout_marginBottom="4dp"
                tools:ignore="ContentDescription,NestedWeights" />
            <ImageView
                android:id="@+id/image_3"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:src="@drawable/ph_7"
                android:layout_marginTop="4dp"
                android:background="@color/colorBlack"
                android:scaleType="centerCrop"
                tools:ignore="ContentDescription,NestedWeights" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:weightSum="3"
            android:layout_marginStart="4dp"
            android:layout_marginEnd="4dp"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/image_4"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/colorBlack"
                android:scaleType="centerCrop"
                android:layout_marginBottom="4dp"
                android:src="@drawable/ph_2"
                tools:ignore="ContentDescription,NestedWeights" />
            <ImageView
                android:id="@+id/image_5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/colorBlack"
                android:scaleType="centerCrop"
                android:layout_marginTop="4dp"
                android:src="@drawable/ph_5"
                android:layout_marginBottom="4dp"
                tools:ignore="ContentDescription,NestedWeights" />
            <ImageView
                android:id="@+id/image_6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:src="@drawable/ph_8"
                android:background="@color/colorBlack"
                android:scaleType="centerCrop"
                android:layout_marginTop="4dp"
                tools:ignore="ContentDescription,NestedWeights" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:weightSum="3"
            android:layout_marginStart="4dp"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/image_7"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/colorBlack"
                android:scaleType="centerCrop"
                android:src="@drawable/ph_3"
                android:layout_marginBottom="4dp"
                tools:ignore="ContentDescription,NestedWeights" />
            <ImageView
                android:id="@+id/image_8"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/colorBlack"
                android:scaleType="centerCrop"
                android:layout_marginTop="4dp"
                android:src="@drawable/ph_6"
                android:layout_marginBottom="4dp"
                tools:ignore="ContentDescription,NestedWeights" />
            <ImageView
                android:id="@+id/image_9"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/colorBlack"
                android:scaleType="centerCrop"
                android:src="@drawable/ph_9"
                android:layout_marginTop="4dp"
                tools:ignore="ContentDescription,NestedWeights" />
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Ответы (1 шт):

Автор решения: Sergei Buvaka

А теперь вопрос: А зачем вам 4 фрагмента для отображения фото?

Если вам нужно отобразить какой-либо список есть RecyclerView. Это как раз прекрасное решение от компании Google для отображения любых списков, любого размера и любой наполненности.

Вот здесь и здесь есть примеры его использования. Вот здесь есть простой пример адаптера.

Вообще если вы возьмете приложение и видите в нем скролящийся список в котором больше 10 элементов то с 95% вероятностью это RecyclerView.

→ Ссылка