Performance of ConstraintLayout inside RecyclerView ViewHolder

守給你的承諾、 提交于 2019-12-23 19:49:53

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/51604562/performance-of-constraintlayout-inside-recyclerview-viewholder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!