我在recyclerView中使用了非常复杂的recyclerView项目布局。我已经开始有一段时间的性能问题了。每次我运行应用程序时,在为recyclerView填充视图布局时都会收到警告。例如:
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
平均每项 800ms确实是长时间的充气。我测量了onBindViewHolder()
的绑定时间,每个项目的绑定时间最长为10ms。
有什么方法可以减少这种测量时间吗?我只使用了ConstraintLayouts
,因为我发现由于recyclerView的性能好,因此建议将其用于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>
答案 0 :(得分:0)
因此,正如@RamiJemli所建议的那样,我将所有ConstraintLayouts替换为RelativeLayouts,并且性能很棒。零跳帧-清除60fps应用运行。显然,那些关于RecyclerViews中的ConstraintLayout> RelativeLayout的文章并不是那么真实(至少在我的应用程序中如此)。