【问题标题】:Why ConstraintLayout slower than LinearLayout in RecyclerView item?为什么 RecyclerView 项中的 ConstraintLayout 比 LinearLayout 慢?
【发布时间】:2019-03-12 23:47:34
【问题描述】:

我正在将recyclyerView 的xml 项从LinearLayout 更改为ConstraintLayout。 当我水平滚动 recyclerView 时,它比 LinearLayout 滞后并且渲染速度非常慢。

约束布局

线性布局

我在这里分享我的 ConstraintLayout 的 xml。

我的 ConstraintLayout 项目

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="85dp"
    android:layout_height="wrap_content">

<android.support.constraint.ConstraintLayout
    android:id="@+id/merchant_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="5dp"
    android:layout_marginStart="5dp">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/merchant_img"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:transitionName="profile"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="UnusedAttribute"
        tools:src="@drawable/avatar" />

    <com.max.xclusivekotlin.customViews.MyTextView
        android:id="@+id/merchant_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:maxWidth="72dp"
        android:minWidth="72dp"
        android:textAlignment="center"
        android:textColor="@color/blackFont"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="@id/merchant_img"
        app:layout_constraintStart_toStartOf="@id/merchant_img"
        app:layout_constraintTop_toBottomOf="@id/merchant_img"
        tools:text="Chili's" />

    <com.max.xclusivekotlin.customViews.MyTextView
        android:id="@+id/merchant_offer_percent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/redColor"
        android:textSize="12sp"
        app:layout_constraintBottom_toTopOf="@id/tv_distance"
        app:layout_constraintEnd_toStartOf="@id/merchant_offer_type"
        app:layout_constraintStart_toStartOf="@id/merchant_name"
        app:layout_constraintTop_toBottomOf="@id/merchant_name"
        app:textBold="bold"
        tools:text="25%" />

    <com.max.xclusivekotlin.customViews.MyTextView
        android:id="@+id/merchant_offer_type"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:textColor="@color/blackFont"
        android:textSize="12sp"
        app:layout_constraintBottom_toTopOf="@id/tv_distance"
        app:layout_constraintEnd_toEndOf="@id/merchant_name"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@id/merchant_offer_percent"
        app:layout_constraintTop_toBottomOf="@id/merchant_name"
        tools:text=" | Refund " />

    <com.max.xclusivekotlin.customViews.MyTextView
         android:id="@+id/tv_distance"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:ellipsize="end"
         android:lines="1"
         android:textAlignment="center"
         android:textColor="@color/greyFont"
         android:textSize="14sp"
         android:visibility="gone"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="@id/merchant_img"
         app:layout_constraintStart_toStartOf="@id/merchant_img"
         app:layout_constraintTop_toBottomOf="@id/merchant_offer_percent" />
    </android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

我的线性布局项

<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="85dp"
    android:layout_height="wrap_content">

<LinearLayout
    android:id="@+id/merchant_layout"
    android:layout_width="75dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="5dp"
    android:layout_marginStart="5dp"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="10dp">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/merchant_img"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:transitionName="profile"
        tools:ignore="UnusedAttribute"
        tools:src="@drawable/avatar" />

    <com.max.xclusivekotlin.customViews.MyTextView
        android:id="@+id/merchant_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:maxWidth="72dp"
        android:minWidth="72dp"
        android:textAlignment="center"
        android:textColor="@color/blackFont"
        android:textSize="14sp"
        tools:text="Chili's" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:orientation="horizontal">

        <com.max.xclusivekotlin.customViews.MyTextView
            android:id="@+id/merchant_offer_percent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/redColor"
            android:textSize="12sp"
            app:textBold="bold"
            tools:text="25%" />

        <com.max.xclusivekotlin.customViews.MyTextView
            android:id="@+id/merchant_offer_type"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:lines="1"
            android:textColor="@color/blackFont"
            android:textSize="12sp"
            tools:text=" | Refund " />
    </LinearLayout>

    <com.max.xclusivekotlin.customViews.MyTextView
        android:id="@+id/tv_distance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:textAlignment="center"
        android:textColor="@color/greyFont"
        android:textSize="14sp"
        android:visibility="gone" />
    </LinearLayout>
</android.support.v7.widget.CardView>

我该如何解决这个问题?

【问题讨论】:

  • 显示您的代码呈现方式。
  • 是的..请张贴xml和java代码..!!

标签: android android-layout android-recyclerview android-constraintlayout


【解决方案1】:

也许您在ConstraintLayoutRecyclerView 性能中使用了很多约束。

我推荐你使用RelativeLayout

这些链接可以帮助你:

Improve RecyclerView performance

RelativeLayout and ConstraintLayout Performance

How Make RecyclerView Smoothly

【讨论】:

  • 好的,我知道了,现在我将使用RelativeLayout,直到谷歌修复这个错误或有人告诉我应该如何使用ConstraintLayout 来处理RecyclerView 项目
【解决方案2】:

相对布局:

RelativeLayout 是一种实现复杂格式的方法,具有水平(一个稳定的水平)轮廓。从它的轮廓来看,它是覆盖事物,一点也不像LinearLayout 那样从不这样做它的名字说独立于其他任何人,RelativeLayout 非常擅长将事物与其他事物相关联。假设您有未确定的视图宽度,并且您需要同时将一个东西放在它的一侧和它的底部。你可以用LinearLayout 做到这一点,但是,这需要两个解决,RelativeLayout 它变得更干净了。

约束布局:

中心是RelativeLayout,可以叠加。尽管如此,让ConstraintLayout 很棒的是它可以取代各种设计:框架、相对、线性。确实,有许多不同的格式,但这些都是包装器 我喜欢ConstraintLayout 的地方在于,在我完全不关心它之前,它在用作设计模式的一部分时非常棒。在大多数情况下,鉴于我的公平设备(对于 Android Studio)和常规渲染错误ConstraintLayout 属性的执行很差,确实如此之长且令人震惊,因此最好利用它设计模式。它可以控制视角的位置和测量(完全不像RelativeLayout)。请记住,以 layout_ 开头的参数隐含用于包装格式。视图本身不处理那些

因此,您可以通过命令链在ConstraintLayout 中使用LinearLayout 中最喜欢的重量,从RelativeLayout 降落左/右/上/下,覆盖简单的FrameLayout 之类的东西我想没有人使用GridLayout,实在是太让人失望了,一直备受青睐的定居LinearLayout,有了ConstraintLayout,你就可以轻松搞定

约束布局缺点:

尽管它很棒,但大纲模式仍然不如 XML 完美。它变得很简单,你必须放大视角,它驱使你只拥有ConstraintLayout,如果你努力包含一些固定的设计 - 你会三思而后行。

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 2017-07-18
    • 2011-03-23
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 2019-12-18
    • 2015-10-04
    • 2012-09-16
    • 2016-10-06
    相关资源
    最近更新 更多