Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of ConstraintLayout inside RecyclerView ViewHolder

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>

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?

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

like image 723
0xMatthewGroves Avatar asked Jan 27 '23 16:01

0xMatthewGroves


1 Answers

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.

like image 94
0xMatthewGroves Avatar answered Jan 31 '23 19:01

0xMatthewGroves