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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With