我有一个 ViewHolder
,标题位于顶部,该标题在特定情况下变得可见。在大多数其他情况下, header 设置为 GONE
。问题是,当 header 设置为 GONE 时,它的高度仍会计算,而其他 View 的传播
不同(之间有更多空间)。
蓝图说明:
- header 被限制在
顶部
、左侧
和右侧
。 - 下面的两个 View 在
打包链
中,限制在顶部
的标题,ImageView
在右侧
和left
和bottom
的父级。
下面是布局检查器的屏幕截图,其中突出显示的标题 View 设置为 GONE
:
根据文档,当设置为 GONE
时,header view 应该缩小到指向仍然应用其他 View 的约束,但 header 不应该占用布局空间和影响高度ConstraintLayout
被设置为 wrap_content
。
在此检查器屏幕截图中,不清楚实际发生了什么。标题不可见,底部 View 显然受限于父级顶部,但标题在检查器中仍显示为具有指定高度的全宽。
我不确定这是一个错误还是我应该强制 ConstraintLayout
重新测量自己。
XML 更新:
<?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
图标之前和之后布局状态的屏幕截图。
截图说明:
- 在左侧,具有可见
header
View 的ViewHolder
位于位置:2
。上面的项目显示正确。 - 点击
favorite
图标(值为 242 的项目)后,position: 1
上的ViewHolder
是带有可见header 的那个
View ,而position: 2
上的ViewHolder
将header
View 设置为GONE
。我期望ViewHolder
高度降低并与position: 0
上的ViewHolder
具有相同的高度。
请记住,此 ViewHolder
在之前的状态下将 header
设置为 VISIBLE
,它可能具有回收功能,不确定。
最佳答案
我已经通过在我的适配器的 #getView
的末尾调用 view.requestLayout()
* 解决了这个问题,在通常调用 绑定(bind)之后。 executePendingBindings()
.
*我现在使用的是老式的 Adapter
,因此您应该搜索 RecyclerView.Adapter
的等效项,可能是 #onBindViewHolder( )
关于android - ConstraintLayout GONE View 占用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43680557/