Проблема прокрутки с FastAdapter и ConstraintLayout: пропущено 211 кадров - PullRequest
0 голосов
/ 17 января 2019

Для моей задачи я подготовил очень простое тестовое приложение AndroidX на Github:

screenshot

Я использую оченьхорошая библиотека FastAdapter в моей MainActivity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(mFastAdapter);

    mFastAdapter.withSelectable(true);
    mFastAdapter.withOnClickListener((v, adapter, item, position) -> {
        item.details = !item.details;
        // pass item.details as payload to bindView to prevent flickering
        adapter.getFastAdapter().notifyAdapterItemChanged(position, item.details);
        return true;
    });

    List<MyItem> newList = new ArrayList<>();
    for (int i = 1; i < 10; i++) {
        MyItem item = new MyItem();
        item.gid = i;
        item.stamp = i;
        item.state1 = (i % 2 == 0 ? WON : LOST);
        item.score1 = (int) (Math.random() * 50);
        item.score2 = (int) (Math.random() * 50);
        item.finished = new Date().toString().substring(0, 10);
        newList.add(item);
    }

    DiffUtil.DiffResult diffResult = FastAdapterDiffUtil.calculateDiff(mItemAdapter, newList);
    FastAdapterDiffUtil.set(mItemAdapter, diffResult);
}

Для макет элементов Я использую ConstraintLayout с 8 TextView и 4 ImageView:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/details"
        android:layout_margin="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guidelineTop"
        app:layout_constraintEnd_toStartOf="@+id/gid"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/plus_circle_gray" />

    <TextView
        android:id="@+id/gid"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guidelineTop"
        app:layout_constraintEnd_toStartOf="@+id/info"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/details"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guidelineTop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/gid"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/photo"
        android:layout_width="80dp"
        android:layout_height="60dp"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toTopOf="@+id/score1"
        app:layout_constraintEnd_toStartOf="@+id/guidelineMiddle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guidelineTop" />

    <ImageView
        android:id="@+id/photo2"
        android:layout_width="80dp"
        android:layout_height="60dp"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toTopOf="@+id/score"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guidelineMiddle"
        app:layout_constraintTop_toTopOf="@+id/guidelineTop" />

    <TextView
        android:id="@+id/score1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/given1"
        app:layout_constraintEnd_toStartOf="@+id/guidelineMiddle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/photo" />

    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/given"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guidelineMiddle"
        app:layout_constraintTop_toBottomOf="@+id/photo2" />

    <TextView
        android:id="@+id/given1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/elo1"
        app:layout_constraintEnd_toStartOf="@+id/guidelineMiddle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/score1" />

    <TextView
        android:id="@+id/given"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/elo2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guidelineMiddle"
        app:layout_constraintTop_toBottomOf="@+id/score" />

    <TextView
        android:id="@+id/elo1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/guidelineBottom"
        app:layout_constraintEnd_toStartOf="@+id/guidelineMiddle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/given1" />

    <TextView
        android:id="@+id/elo2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/guidelineBottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guidelineMiddle"
        app:layout_constraintTop_toBottomOf="@+id/given" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineMiddle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/board"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guidelineBottom" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineBottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" />

</androidx.constraintlayout.widget.ConstraintLayout>

При отладке приложения на мобильном телефоне (я использую Moto G2 и Moto G4 Play) и прокручивании списка только из 10 элементов, я получаю предупреждения:

01-17 15:16:27.279 13947-13947/de.afarber.fasi I/Choreographer: Skipped 173 frames!  The application may be doing too much work on its main thread.
01-17 15:38:17.700 13947-13947/de.afarber.fasi I/Choreographer: Skipped 35 frames!  The application may be doing too much work on its main thread.
01-17 15:38:21.237 13947-13947/de.afarber.fasi I/Choreographer: Skipped 211 frames!  The application may be doing too much work on its main thread.
01-17 15:38:34.569 13947-13947/de.afarber.fasi I/Choreographer: Skipped 106 frames!  The application may be doing too much work on its main thread.
01-17 15:38:38.696 13947-13947/de.afarber.fasi I/Choreographer: Skipped 107 frames!  The application may be doing too much work on its main thread.

В моем реальном приложении у меня есть фрагменты сдругие макеты, которые не приводят к таким ошибкам, поэтому я не думаю, что причиной являются старые мобильные телефоны.Также я не делаю прокрутку особенно быстро или слишком медленно.

1 Ответ

0 голосов
/ 27 января 2019

Я решил проблемы с производительностью прокрутки моего RecyclerView, переключившись с ConstraintLayout на LinearLayout плюс несколько элементов Space для лучшего выравнивания -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/details"
            android:layout_weight="10"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:contentDescription="@string/details"
            app:srcCompat="@drawable/plus_circle_gray"
            />

        <TextView
            android:id="@+id/gid"
            android:textAlignment="center"
            android:textStyle="bold"
            android:layout_weight="40"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/info"
            android:textAlignment="center"
            android:layout_weight="40"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Space
            android:layout_weight="10"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <ImageView
            android:id="@+id/photo1"
            android:scaleType="centerInside"
            android:layout_weight="40"
            android:layout_width="0dp"
            android:layout_height="60dp"
            />

        <ImageView
            android:id="@+id/photo2"
            android:scaleType="centerInside"
            android:layout_weight="40"
            android:layout_width="0dp"
            android:layout_height="60dp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Space
            android:layout_weight="10"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/score1"
            android:textAlignment="center"
            android:layout_weight="40"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/score2"
            android:textAlignment="center"
            android:layout_weight="40"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Space
            android:layout_weight="10"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/given1"
            android:textAlignment="center"
            android:layout_weight="40"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/given2"
            android:textAlignment="center"
            android:layout_weight="40"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Space
            android:layout_weight="10"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/elo1"
            android:textAlignment="center"
            android:layout_weight="40"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/elo2"
            android:textAlignment="center"
            android:layout_weight="40"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <ImageView
        android:id="@+id/board"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        />

</LinearLayout>

screenshot

...