简体   繁体   English

ConstraintLayout不使用RecyclerView中的Wrap_Content动态更改高度

[英]ConstraintLayout not dynamically changing Height with Wrap_Content inside RecyclerView

I have a StaggeredLayout RecyclerView that is filled with different cards using CardView , inside those cards there is normally a ConstraintLayout with all the fields empty. 我有一个使用CardView填充不同卡的StaggeredLayout RecyclerView ,在这些卡中通常有一个ConstraintLayout ,所有字段都是空的。 Everything has wrap_content as height. 一切都有wrap_content作为高度。

My problem is that it seems the CardView height is being calculated before any data is filled and not updated afterwards. 我的问题是,似乎在填充任何数据之前计算了CardView高度,之后没有更新。 I have already tried directly invalidating, setting measureAllChildren to true, using requestLayout on the itemView and making so that the bottom items have a constraint to the bottom of the parent but the problem perseveres. 我已经尝试过直接无效,设置measureAllChildren为true,使用requestLayoutitemView和制作,使得底部的项目有一个约束到父的底部,但问题持之以恒。

<?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>

The fields are filled inside the method onBindViewHolder on the adapter as follows: 这些字段填充在适配器上的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());

The RecyclerView is initialized as follows: 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);

and the layout which it is inside of is a fragment as defined here: 它所在的布局是这里定义的片段:

<?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>

Scrolling and so on works without a problem, the size of the RecyclerView is also updated on the fly . 在作品滚动,因此没有问题,大小RecyclerView也更新on the fly

EDIT: I already tried changing the StaggeredLayout to a LinearLayoutManager and even removing the CardView altogether. 编辑:我已经尝试将StaggeredLayout更改为LinearLayoutManager ,甚至完全删除了CardView After these tests I seemed to confirm that the problem is in the ConstraintLayout itself. 经过这些测试后,我似乎确认问题出在ConstraintLayout本身。

After searching for quite some time, I realized that the problem was a big oversight of my part. 经过一段时间的搜索,我意识到这个问题对我来说是一个很大的疏忽。

On the ImageView we see that the height is defined by the Constraints, and the constraint bottom is the guideline: ImageView我们看到高度由约束定义,约束底部是指南:

<ImageView
    ...
    android:layout_width="0dp"
    android:layout_height="0dp"
    ...
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    ... />

The guideline itself is has a percentage position based on the final height of the card. 指南本身具有基于卡的最终高度的百分比位置。

<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" />

So what I got here was a case of mutual dependencies, the height of the whole card is defined by the items inside and the height of the Image is percentage of that height. 所以我在这里得到的是相互依赖的情况,整张卡的高度由里面的项目定义,图像的高度是该高度的百分比。 What I think happens is that the height of the card is looked up based on the combined heights of what is inside(0dp for the image) and then the image is scaled and placed moving everything down with it. 我认为发生的是根据内部的组合高度(图像的0dp)查找卡的高度,然后缩放图像并放置所有内容。

TLDR: I oversaw a mutual dependency and setting the size of the image veforehand fixes the problem. TLDR:我监督了相互依赖,并设置了图像的大小来解决问题。

I believe this could help you: Change: 我相信这可以帮到你:改变:

<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>

to

    <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"/>

Then remove this lines from your code (where you initialize recyclerview): 然后从代码中删除这一行(初始化recyclerview的地方):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM