微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RecyclerView OnLayout花费的时间太长,性能问题

如何解决RecyclerView OnLayout花费的时间太长,性能问题

我有一个带有RecyclerView的片段(jetpack导航图的一部分)(它是2 x 10的垂直网格), 1张卡是ConstraintLayout(1张TextView标题和2张TextView SubTitle + 1张ImageView 64x64dp,每个使用大约10kb的png)

问题是根据android studio systrace(cpu-> Trace System Calls)的帧需要160-250ms。我正在从Android配置文件按钮启动的Android 10小米Note 9 Pro(6G内存,SG 720G)上进行测试。

1张卡的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/id1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:background="@drawable/card_background"
android:layout_margin="8dp"
android:elevation="2dp"
android:foreground="@drawable/btn_background_ripple">

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:fontFamily="@font/roboto"
    android:gravity="center_horizontal"
    android:linespacingExtra="3sp"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    app:layout_constraintBottom_toTopOf="@+id/tvSubTitle"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/iv"
    app:layout_constraintVertical_bias="0.0"
    app:layout_constraintVertical_chainStyle="packed" />

<TextView
    android:id="@+id/tvSubTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="20dp"
    android:fontFamily="@font/roboto"
    android:gravity="center_horizontal"
    android:linespacingExtra="3sp"
    android:textColor="@android:color/black"
    android:textSize="14sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvTitle"
    tools:text="Vocabulario" />

<ImageView
    android:id="@+id/iv"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_marginTop="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />


 </androidx.constraintlayout.widget.ConstraintLayout>

stacktrace的屏幕截图

stacktrace

] 1

解决方法

要找出导致系统跟踪中Choreographer#doFrame长时间调用的原因,可以进一步放大并查看每个膨胀跟踪事件的组成。您应该能够将这些跟踪事件(目前太小,无法在屏幕截图中看到)映射到XML布局中的各个元素。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。