我有一个ViewHolder,其中标题在顶部,并且在特定情况下该标题变为可见状态。在大多数其他情况下,标题被设置为GONE。问题是,当标题被设置为GONE时,它的高度仍然被计算,并且其他视图会以不同的方式展开(之间有更多的空间)。
这是布局蓝图:
![blueprint](https://istack.dev59.com/xh5lI.webp)
解释如下:
- 标题受到顶部、左侧和右侧的限制。
- 下面的两个视图处于紧凑链中,限制为标题在顶部,ImageView在右侧,父级在左侧和底部。
这是布局检查器的屏幕截图,突出显示了设置为GONE的标题视图:
![enter image description here](https://istack.dev59.com/f2fPK.webp)
根据文档,当将标题视图设置为GONE时,应将其缩小到点,并仍应将其他视图的约束条件应用于其上,但是由于它被设置为wrap_content,因此标题不应占用布局空间或影响ConstraintLayout的高度。
在此检查器屏幕截图中,实际发生了什么并不清楚。标题不可见,底部视图明显受限于父级顶部,但是在检查器中显示的标题仍然是全宽度,并具有指定的高度。
我不确定这是一个错误还是我应该强制ConstraintLayout重新测量自身。
XML UPDATE:
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:id="@+id/list_item_step_conversion_tv_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/gray_very_light"
android:padding="@dimen/activity_vertical_margin"
android:text="@string/favorites"
android:textColor="@android:color/black"
android:textSize="@dimen/default_text_size"
android:textStyle="bold"
android:visibility="gone"
tools:visibility="visible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/list_item_step_conversion_tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/gray_dark"
android:textSize="@dimen/medium_text_size"
app:layout_constraintBottom_toTopOf="@+id/list_item_step_conversion_tv_description"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/list_item_step_conversion_iv_favorite"
app:layout_constraintTop_toBottomOf="@+id/list_item_step_conversion_tv_header"
app:layout_constraintVertical_chainStyle="packed"
tools:text="Bicycling - light (10-11.9 mph)"/>
<TextView
android:id="@+id/list_item_step_conversion_tv_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="16dp"
android:layout_marginTop="4dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@android:color/black"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="@+id/list_item_step_conversion_tv_title"
app:layout_constraintRight_toLeftOf="@+id/list_item_step_conversion_iv_favorite"
app:layout_constraintTop_toBottomOf="@+id/list_item_step_conversion_tv_title"
tools:text="182 steps/minute"/>
<ImageView
android:id="@+id/list_item_step_conversion_iv_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginRight="24dp"
android:layout_marginTop="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/list_item_step_conversion_tv_header"
app:srcCompat="@drawable/ic_not_liked"/>
</android.support.constraint.ConstraintLayout>
更新2
经过进一步观察,这个问题仅在RecyclerView.Adapter
中调用notifyDataSetChanged
后发生。以下是单击其中一个项目的favorite
图标之前和之后的布局状态的屏幕截图。
屏幕截图说明:
- 左侧为
position: 2
上可见header
视图的ViewHolder
。上面的项目正常显示。 - 单击
favorite
图标(值为242的项目)后,position: 1
上的ViewHolder
成为具有可见header
视图的ViewHolder
,而position: 2
上的ViewHolder
将header
视图设置为GONE
。我原本期望ViewHolder
的高度会减小,并与position: 0
上的ViewHolder
具有相同的高度。
考虑到这个ViewHolder
在先前的状态下已设置header
为VISIBLE
,这可能与回收有关,但不确定。