android - RecyclerView ViewHolder内部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) }
}
}
这就是我的布局。尽可能简单:
<?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"
如果我为这个属性指定一个固定大小,一切都很好。为确定每一行的视图高度而进行的不断计算可能会导致问题。
推荐阅读
- python - 如何更新包中的文件?
- java - 类文件 javax/validation/ConstraintViolationException 中非本机或抽象方法中的缺失代码属性
- python - matplotlib 中未排序但规则间隔数据的轮廓图
- javascript - 如何覆盖文件并在同一个 gulp 任务中使用该文件?
- python-3.x - 如何在字符串中搜索子字符串,然后在 python 中找到子字符串之前的字符
- css - css 中 max-width:100% 时图像分辨率很差 - 缩小
- android - 在具有指定端口的 android 上提供 ionic 应用程序
- html - 为什么我的列垂直对齐而不是 2 行?
- python - 曲面计算器
- python - 用python控制autocad:pyautocad什么都不做