在过去的两天里,我一直在尝试分类为什么我的 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) }
}
}
这就是我的布局的样子。尽可能简单:
<?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>
所以我的问题是:我的布局有什么问题导致卡顿?我使用了不正确的 LayoutManager 吗?限制是否导致透支?
如果我将布局 XML 完全重写为基于 LinearLayout,那么它就会如丝绸般光滑。
好吧,我终于找到了罪魁祸首:
android:layout_height="wrap_content"
如果我为此属性指定固定大小,一切都很好。确定每行视图高度的不断计算可能会导致问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)