gpt4 book ai didi

android - RecyclerView onMeasure 性能问题

转载 作者:行者123 更新时间:2023-12-04 02:57:33 28 4
gpt4 key购买 nike

我在 recyclerView 中使用了相当复杂的 recyclerView 项目布局。我已经开始有一段时间的性能问题了。每次我运行应用程序时,我都会在为 recyclerView 中的 View 扩充布局时收到警告。例如:

D/View: [ANR Warning]onMeasure time too long, this =android.support.constraint.ConstraintLayout{91a57db V.E...... ......I. 0,0-0,0 #7f0801d4 app:id/parentLayout}time =807 ms
D/View: [ANR Warning]onMeasure time too long, this =android.support.constraint.ConstraintLayout{20858c2 V.E...... ......I. 0,0-0,0 #7f0801d4 app:id/parentLayout}time =745 ms
D/View: [ANR Warning]onMeasure time too long, this =android.support.constraint.ConstraintLayout{880b0bc V.E...... ......I. 0,0-0,0 #7f0801d4 app:id/parentLayout}time =705 ms

平均每个项目 800 毫秒对于充气来说确实是很长的时间。我已经测量了 onBindViewHolder() 绑定(bind)时间,每个项目最多 10 毫秒,这很好。

有什么办法可以减少这个测量时间吗?我只使用了 ConstraintLayouts,因为我发现 recyclerView 项目推荐使用约束,因为它的性能很快。

此项目包含可展开的布局,将在单击项目时展开。

项目布局:

<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<android.support.constraint.ConstraintLayout
android:id="@+id/itemInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentStart="true"
android:layout_marginBottom="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginBottom="16dp"
app:srcCompat="@mipmap/ic_launcher" />

<android.support.constraint.ConstraintLayout
android:id="@+id/itemTextLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/itemName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:singleLine="false"
android:text="itemName"
android:textColor="@color/colorItemMajor"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.ConstraintLayout
android:id="@+id/BottomLineLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="4dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/itemRate_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/itemName">

<ImageView
android:id="@+id/itemStatus"
android:layout_width="wrap_content"
android:layout_height="12dp"
android:layout_marginEnd="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/itemSupplier"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/itemSupplier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginEnd="4dp"
android:text="suppName"
android:textColor="@color/colorItemMinor"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/itemDel_icon"
app:layout_constraintStart_toEndOf="@id/itemStatus"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/itemDel_icon"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginEnd="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/itemDel"
app:layout_constraintStart_toEndOf="@id/itemSupplier"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />

<TextView
android:id="@+id/itemDel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginEnd="4dp"
android:text="itemDel"
android:textColor="@color/colorItemMinor"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/itemWT_icon"
app:layout_constraintStart_toEndOf="@+id/itemDel_icon"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/itemWT_icon"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginStart="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/itemDel"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />

<TextView
android:id="@+id/itemWT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/itemWT_icon"
android:layout_alignBottom="@+id/itemWT_icon"
android:layout_gravity="fill"
android:layout_marginStart="4dp"
android:layout_toEndOf="@+id/itemWT_icon"
android:text="itemWT"
android:textColor="@color/colorItemMinor"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/itemWT_icon"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

<ImageView
android:id="@+id/itemRate_icon"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginEnd="4dp"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toStartOf="@id/itemRate"
app:layout_constraintTop_toBottomOf="@+id/itemName"
app:srcCompat="@mipmap/ic_launcher" />

<TextView
android:id="@+id/itemRate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/itemRate_icon"
android:layout_alignBottom="@+id/itemRate_icon"
android:layout_gravity="fill"
android:layout_marginTop="4dp"
android:text="itemRate"
android:textColor="@color/colorRating"
android:textSize="10sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/itemName" />

</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
android:id="@+id/expandable_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="@color/colorBackground"
android:orientation="horizontal"
android:visibility="visible"
app:layout_constraintTop_toBottomOf="@+id/itemInfo">

<ImageView
android:id="@+id/arrowPointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/itemDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginEnd="32dp"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:singleLine="false"
android:text="This is some item desc."
android:textColor="@color/colorItemMinor"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/itemWeight_icon"
android:layout_width="12sp"
android:layout_height="12sp"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/itemDesc"
app:srcCompat="@mipmap/ic_launcher" />

<TextView
android:id="@+id/itemWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/itemWeight_icon"
android:layout_alignBottom="@+id/itemWeight_icon"
android:layout_gravity="fill"
android:layout_marginStart="4dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="@+id/itemWeight_icon"
android:text="itemWeight"
android:textColor="@color/colorItemMinor"
android:textSize="10sp"
app:layout_constraintStart_toEndOf="@+id/itemWeight_icon"
app:layout_constraintTop_toBottomOf="@+id/itemDesc" />

<ImageView
android:id="@+id/itemType_icon"
android:layout_width="12sp"
android:layout_height="12sp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
app:layout_constraintStart_toEndOf="@id/itemWeight"
app:layout_constraintTop_toBottomOf="@id/itemDesc"
app:srcCompat="@mipmap/ic_launcher" />

<TextView
android:id="@+id/itemType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/itemType_icon"
android:layout_alignBottom="@+id/itemType_icon"
android:layout_gravity="fill"
android:layout_marginStart="4dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="@+id/itemType_icon"
android:text="itemType"
android:textColor="@color/colorItemMinor"
android:textSize="10sp"
app:layout_constraintStart_toEndOf="@+id/itemType_icon"
app:layout_constraintTop_toBottomOf="@+id/itemDesc" />

<android.support.constraint.ConstraintLayout
android:id="@+id/propertyRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/itemType_icon">

<TextView
android:id="@+id/propertyChangeClickableText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginStart="16dp"
android:clickable="true"
android:focusable="true"
android:text="Change Properties"
android:textColor="@color/colorPrice"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageButton
android:id="@+id/confirmItemButton"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginEnd="16dp"
android:background="@drawable/image_button_border_drawable"
android:scaleType="fitCenter"
app:srcCompat="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="16dp"
android:background="@color/colorDivider"
app:layout_constraintTop_toBottomOf="@id/propertyRow" />

<android.support.constraint.ConstraintLayout
android:id="@+id/topRowLayoutPopup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/separator">

<TextView
android:id="@+id/itemSupplierName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginStart="16dp"
android:text="supName"
android:textColor="@color/colorItemMajor"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/property1_ico"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignBaseline="@+id/itemSupplierName"
android:layout_marginEnd="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/property2_ico"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />

<ImageView
android:id="@+id/property2_ico"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignBaseline="@+id/itemSupplierName"
android:layout_marginEnd="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/property3_ico"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />

<ImageView
android:id="@+id/property3_ico"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignBaseline="@+id/itemSupplierName"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
android:id="@+id/bottomRowLayoutPopup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/topRowLayoutPopup">

<TextView
android:id="@+id/editSupplierButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/supplierRatingBar"
android:layout_marginStart="16dp"
android:clickable="true"
android:focusable="true"
android:text="Edit Supplier"
android:textColor="@color/colorPrice"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/supplierRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/supplierRatingBar"
android:layout_gravity="fill"
android:layout_marginEnd="8dp"
android:text="supplierRating"
android:textColor="@color/colorRating"
android:textSize="@dimen/food_list_bottom_row_font"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/supplierRatingBar"
app:layout_constraintTop_toTopOf="parent" />

<RatingBar
android:id="@+id/supplierRatingBar"
style="@style/RatingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/supplierRatingRateNumber"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/supplierRatingRateNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/supplierRatingBar"
android:layout_gravity="fill"
android:layout_marginEnd="16dp"
android:text="RateNum"
android:textColor="@color/colorItemMinor"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

最佳答案

正如@RamiJemli 建议的那样,我用 RelativeLayouts 替换了我所有的 ConstraintLayouts,性能非常棒。零跳帧 - 清晰的 60fps 应用程序运行。显然,那些关于 RecyclerViews 中的 ConstraintLayout > RelativeLayout 的文章并非如此(至少在我的应用程序中)。

关于android - RecyclerView onMeasure 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52256595/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com