RecyclerView 中的 Wrap_Content ConstraintLayout 高度不会动态变化

8

我有一个使用 CardView 填充的 StaggeredLayout RecyclerView,卡片内部通常有一个所有字段均为空的 ConstraintLayout。所有元素高度都设置为 wrap_content

我的问题是,在填充数据之前似乎会计算 CardView 的高度,并在此后不再更新高度。我已经尝试了直接无效化、将 measureAllChildren 设为 true、在 itemView 上使用 requestLayout,并使底部项目与父项目的底部有约束条件,但问题仍然存在。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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:layout_margin="@dimen/home_card_half_min_gutter">

<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"
    android:layout_gravity="center"
    android:padding="@dimen/content_spacing">


    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="@dimen/content_spacing"
        android:contentDescription="@string/icon"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_airplanemode_inactive_accent_dark_24dp"
        android:scaleType="fitCenter"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.35" />

    <TextView
        android:id="@+id/tv_item_1_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Caption"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/guideline5"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        tools:text="TextView" />

    <TextView
        android:id="@+id/tv_item_1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/content_half_spacing"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1"
        app:layout_constraintLeft_toLeftOf="@+id/tv_item_1_label"
        app:layout_constraintRight_toLeftOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/tv_item_1_label"
        tools:text="TextView" />

    <TextView
        android:id="@+id/tv_item_2_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Caption"
        app:layout_constraintLeft_toLeftOf="@+id/guideline5"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        tools:text="TextView" />

    <TextView
        android:id="@+id/tv_item_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/content_half_spacing"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1"
        app:layout_constraintLeft_toLeftOf="@+id/tv_item_2_label"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_item_2_label"
        tools:text="TextView" />

    <TextView
        android:id="@+id/tv_item_3_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/content_spacing"
        android:textAppearance="@style/TextAppearance.AppCompat.Caption"
        app:layout_constraintLeft_toLeftOf="@+id/tv_item_1"
        app:layout_constraintRight_toLeftOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/tv_item_1"
        tools:text="TextView" />

    <TextView
        android:id="@+id/tv_item_3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/content_half_spacing"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1"
        app:layout_constraintLeft_toLeftOf="@+id/tv_item_3_label"
        app:layout_constraintRight_toLeftOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/tv_item_3_label"
        tools:text="TextView" />

    <TextView
        android:id="@+id/tv_item_4_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/content_spacing"
        android:textAppearance="@style/TextAppearance.AppCompat.Caption"
        app:layout_constraintLeft_toLeftOf="@+id/tv_item_2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_item_2"
        tools:text="TextView" />

    <TextView
        android:id="@+id/tv_item_4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/content_half_spacing"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1"
        app:layout_constraintLeft_toLeftOf="@+id/tv_item_4_label"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_item_4_label"
        tools:text="TextView" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />
</android.support.constraint.ConstraintLayout>

下面是填充数据的代码,它位于适配器的 onBindViewHolder 方法内:

DataExample example = mItems.get(position);
Card4ViewHolder vhCon = (Card4ViewHolder) holder;

vhCon.mIcon.setImageResource(R.drawable.ic_data);

vhCon.mItem1Label.setText(R.string.A_label);
vhCon.mItem1.setText(example.getA());

vhCon.mItem2Label.setText(R.string.B_label);
vhCon.mItem2.setText(example.getB());

vhCon.mItem3Label.setText(R.string.C_label);
vhCon.mItem3.setText(example.getC());

vhCon.mItem4Label.setText(R.string.D_label);
vhCon.mItem4.setText(example.getD());

RecyclerView 的初始化如下:

DisplayMetrics displayMetrics = res.getDisplayMetrics();
float pixelWidth =  displayMetrics.widthPixels;
int noOfColumns = (int) (pixelWidth / res.getDimension(R.dimen.home_card_min_width));
mLayoutManager = new StaggeredGridLayoutManager(noOfColumns,
        StaggeredGridLayoutManager.VERTICAL);
mLayoutManager.setGapStrategy(StaggeredGridLayoutManager
        .GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
mHomeCardAdapter = new HomeCardAdapter(activity);
mCardsRecyclerView.setAdapter(mHomeCardAdapter);
mCardsRecyclerView.setLayoutManager(mLayoutManager);

它所在的布局是一个片段,如此定义:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/home_rv_dashboard_cards"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</ScrollView>

滚动等操作没有问题,RecyclerView的大小也可以实时更新。
编辑:我已经尝试将StaggeredLayout更改为LinearLayoutManager,甚至完全删除CardView。经过这些测试,我似乎确认问题出在ConstraintLayout本身。

1
你能发布一下你的Recyclerview吗? - Farid
我编辑了原始帖子并添加了更多信息。 - Raeglan
ConstraintLayout 的整个目的不就是为了避免嵌套吗?如果你把它包在 CardView 或者 FrameLayout 里面,那不就又嵌套了一层,造成了无用的过度绘制。 - Rachit Mishra
ConstraingLayout 的目的是最小化嵌套,但在某些情况下仍可能是最佳选择。在这里,我想要一个具有向后兼容阴影的卡片布局,这正是 CardView 所做的(而我遇到的问题来自于 ConstraintLayout 本身)。 - Raeglan
2个回答

1
在搜索了相当长时间后,我意识到问题是我的重大疏忽。
ImageView上,我们可以看到高度由约束定义,约束底部是指南线:
<ImageView
    ...
    android:layout_width="0dp"
    android:layout_height="0dp"
    ...
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    ... />

该指南本身基于卡片的最终高度而具有百分比位置。
<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.35" />

我在这里遇到了一种相互依赖的情况,整个卡片的高度由内部项目定义,而图像的高度是该高度的百分比。我认为发生的事情是根据内部项目的组合高度(图像为0dp)查找卡片的高度,然后缩放并移动图像,将所有内容一起向下移动。
简而言之:我忽视了相互依赖关系,预先设置图像大小可以解决问题。

0

我相信这可能会对你有所帮助: 更改为:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/home_rv_dashboard_cards"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</ScrollView>

    <android.support.v7.widget.RecyclerView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/home_rv_dashboard_cards"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="LinearLayoutManager"/>

然后从您的代码中删除这些行(在初始化recyclerview的位置):

mLayoutManager = new StaggeredGridLayoutManager(noOfColumns,
        StaggeredGridLayoutManager.VERTICAL);
mLayoutManager.setGapStrategy(StaggeredGridLayoutManager
        .GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
mCardsRecyclerView.setLayoutManager(mLayoutManager);

这从根本上改变了我所追求的目标。在我正在工作的项目中,我们需要一个非线性卡片布局,其中网格中有不同大小的卡片。但即使按照建议进行更改,wrap_content 仍然无法正常工作。 我认为问题出在 CardView 上,因为即使使用 LinearLayoutManager,问题仍然存在。 - Raeglan
也许添加你得到的内容和想要实现的内容的图片会更有帮助。那样我会更有帮助 ;) - Farid
1
我刚刚发布了一个回答,解决了我的问题。这是一种相互依赖的情况,图像大小基于视图大小,而视图大小又基于其内容。但是感谢您的帮助,根据您的评论和一些实验,我放弃了CardView和RecyclerView。 - Raeglan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接