RecyclerView ViewHolder内部ConstraintLayout的性能 [英] Performance of ConstraintLayout inside RecyclerView ViewHolder

查看:399
本文介绍了RecyclerView ViewHolder内部ConstraintLayout的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了最后两天的时间来尝试弄清为什么RecyclerView如此之慢,而在滚动时却是如此缓慢,我将其范围缩小到了我用于行的ConstraintLayout.在android上使用GPU探查器时,一直到屏幕顶部都会显示绿色/蓝绿色条,表明有明显的锯齿.很明显,出了问题.

I've spent the last 2 days attempting to triage why my RecyclerView is so is so unbearably slow while scrolling and I've narrowed it down to the ConstraintLayout I'm using for the rows. Using the GPU profiler on android shows green/blueish green bars all the way up to the top of the screen, indicating substantial jank. It's super obvious something is wrong.

这是我的观察者的样子:

Here's what my viewholder looks like:

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) }
        }
    }

这是我的布局.可以很简单:

And here's what my layout looks like. Simple as can be:

<?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吗?约束是否导致透支?

So my question is this: what is wrong with my layout that's causing the jank? Am I using an incorrect LayoutManager? Are the constraints causing overdraws?

如果我将布局XML完全重写为基于LinearLayout,则它像丝绸一样平滑.

If I completely rewrite the layout XML to be LinearLayout based, it's smooth as silk.

推荐答案

好吧,我终于找到了罪魁祸首:

Well, I finally found the culprit:

android:layout_height="wrap_content"

如果我为此属性指定固定大小,则一切正常.确定每一行视图高度的常量计算很可能会引起问题.

If I specify a fixed size for this attribute all is well. The constant calculations to determine view height for each row were likely causing the issues.

这篇关于RecyclerView ViewHolder内部ConstraintLayout的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆