android - ConstraintLayout在RecyclerView中卡顿的问题
PHP中文网
PHP中文网 2017-04-18 09:16:51
0
1
1454

1.recyclerview的使用gridlayoutManager布局,每行有四个元素。布局的item使用framelayout作为根布局的时候没有卡顿的现象,在使用ConstraintLayout作为根布局的时候就会卡顿,检测发现cpu消耗比较厉害。

2.

第一种布局:
<FrameLayout
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="match_parent"
android:layout_height="100dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">

<ImageView
    android:id="@+id/iv_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="0dp"
    android:layout_marginEnd="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginStart="0dp"
    android:layout_marginTop="0dp"
    android:background="#dadada"
    android:clickable="false"
    android:contentDescription="@string/app_name"
    android:focusable="false"
    android:scaleType="centerInside"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"/>

<View
    android:id="@+id/lay_mask"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="0dp"
    android:layout_marginEnd="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginStart="0dp"
    android:layout_marginTop="0dp"
    android:background="@color/black_alpha_80"
    android:clickable="false"
    android:focusable="false"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="@+id/iv_image"
    app:layout_constraintDimensionRatio="h,1:1"
    app:layout_constraintLeft_toLeftOf="@+id/iv_image"
    app:layout_constraintRight_toRightOf="@+id/iv_image"
    app:layout_constraintTop_toTopOf="@+id/iv_image"/>

<ImageView
    android:id="@+id/cb_selected"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_gravity="top|right"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:clickable="false"
    android:contentDescription="@string/app_name"
    android:focusable="false"
    android:scaleType="centerInside"
    android:src="@drawable/cb_select_image_check_selector"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="RtlHardcoded"/>
</FrameLayout>

第二种布局
<android.support.constraint.ConstraintLayout
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="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/iv_image"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#dadada"
    android:clickable="false"
    android:contentDescription="@string/app_name"
    android:focusable="false"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintTop_creator="1"/>

<View
    android:id="@+id/lay_mask"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/black_alpha_80"
    android:clickable="false"
    android:focusable="false"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="@+id/iv_image"
    app:layout_constraintDimensionRatio="h,1:1"
    app:layout_constraintLeft_toLeftOf="@+id/iv_image"
    app:layout_constraintRight_toRightOf="@+id/iv_image"
    app:layout_constraintTop_toTopOf="@+id/iv_image"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"/>

<ImageView
    android:id="@+id/cb_selected"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_gravity="top|right"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:clickable="false"
    android:contentDescription="@string/app_name"
    android:focusable="false"
    android:scaleType="centerInside"
    android:src="@drawable/cb_select_image_check_selector"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="RtlHardcoded"
    android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
PHP中文网
PHP中文网

认证高级PHP讲师

répondre à tous(1)
刘奇

De même, en remplaçant l'élément rootlayout par constraintlayout, j'ai constaté qu'il ne pouvait pas remplir l'élément. . . ?
Votre disposition est-elle normale ?

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!