gpt4 book ai didi

android - ConstraintLayout GONE View 占用空间

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:59:31 29 4
gpt4 key购买 nike

我有一个 ViewHolder,标题位于顶部,该标题在特定情况下变得可见。在大多数其他情况下, header 设置为 GONE。问题是,当 header 设置为 GONE 时,它的高度仍会计算,而其他 View 的传播不同(之间有更多空间)。

这是一个布局蓝图: blueprint

蓝图说明:

  1. header 被限制在顶部左侧右侧
  2. 下面的两个 View 在打包链中,限制在顶部的标题,ImageView右侧leftbottom 的父级。

下面是布局检查器的屏幕截图,其中突出显示的标题 View 设置为 GONE: enter image description here

根据文档,当设置为 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 图标之前和之后布局状态的屏幕截图。 enter image description here

截图说明:

  • 在左侧,具有可见header View 的ViewHolder 位于位置:2。上面的项目显示正确。
  • 点击 favorite 图标(值为 242 的项目)后,position: 1 上的 ViewHolder 是带有可见 header 的那个 View ,而 position: 2 上的 ViewHolderheader 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com