Fro*_*ket 4 android android-viewholder android-recyclerview android-constraintlayout
过去 2 天我一直在尝试对为什么我的 RecyclerView 在滚动时如此缓慢地进行分类,并将其缩小到我用于行的 ConstraintLayout。在 android 上使用 GPU 分析器显示绿色/蓝绿色条一直到屏幕顶部,表明严重卡顿。很明显有什么地方不对劲。
这是我的观察者的样子:
class MyViewHolder(
override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer {
fun bindTo(item: Item?, clickListener: (Item?) -> Unit) {
text_item_name.text = item?.name
Glide.with(containerView.context).load(item?.url).into(image_item)
containerView.setOnClickListener { clickListener(item) }
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我的布局的样子。尽可能简单:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:paddingBottom="@dimen/padding"
android:paddingEnd="@dimen/padding_2x"
android:paddingStart="@dimen/padding_2x"
android:paddingTop="@dimen/padding">
<ImageView
android:id="@+id/image_item"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="@dimen/margin_2x"
android:layout_marginStart="@dimen/margin_2x"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_coin_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_2x"
android:layout_marginStart="@dimen/margin_2x"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/image_item"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:我的布局有什么问题导致了卡顿?我是否使用了不正确的 LayoutManager?约束是否会导致透支?
如果我将布局 XML 完全重写为基于 LinearLayout,它会像丝绸一样光滑。
好吧,我终于找到了罪魁祸首:
android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)
如果我为此属性指定固定大小,一切都很好。确定每一行的视图高度的不断计算可能会导致问题。
| 归档时间: |
|
| 查看次数: |
4976 次 |
| 最近记录: |