我有一个使用 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
本身。
ConstraintLayout
的整个目的不就是为了避免嵌套吗?如果你把它包在CardView
或者FrameLayout
里面,那不就又嵌套了一层,造成了无用的过度绘制。 - Rachit MishraConstraingLayout
的目的是最小化嵌套,但在某些情况下仍可能是最佳选择。在这里,我想要一个具有向后兼容阴影的卡片布局,这正是CardView
所做的(而我遇到的问题来自于ConstraintLayout
本身)。 - Raeglan