ConstraintLayout GONE视图占用空间

时间:2017-04-28 12:43:39

标签: android android-constraintlayout constraint-layout-chains

我有一个ViewHolder标题位于顶部,标题在特定情况下可见。在大多数其他情况下,标头设置为GONE。问题是当标题设置为GONE时,仍然会计算其高度,而其他视图的spread也不同(两者之间的空间更大)。

这是一个布局蓝图: blueprint

蓝图说明:

  1. 标题约束为topleftright
  2. 以下两个视图位于packed chain,约束为top上的标题,ImageView约束right,父left和{{1} }}。
  3. 以下是布局检查器的截图,其中突出显示的标题视图设置为bottomenter image description here

    根据文档,当设置为GONE时,标题视图应该缩小到指向仍然应用于其他视图的约束,但标题不应占用布局空间并影响{{的高度1}}因为它被设置为GONE

    在这个检查员截图中,目前尚不清楚实际发生了什么。标题不可见,底部视图显然约束到父顶部,但标题仍在检查器中显示为具有指定高度的全宽。

    我不确定这是不是一个错误,或者我应该强迫ConstraintLayout重新衡量一下。

    XML更新:

    wrap_content

    更新2

    在进行其他观察后,只有在ConstraintLayout中调用<?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> 后才会出现此问题。以下是点击其中一个项目的notifyDataSetChanged图标之前和之后的布局状态的屏幕截图。 enter image description here

    屏幕截图说明:

    • 在左侧,有RecyclerView.Adapter视图的favorite位于ViewHolder。上面的项目正确显示。
    • 点击header图标(值为242的项目)后,position: 2上的favorite是具有可见ViewHolder视图的图片,而position: 1上的header ViewHolderposition: 2视图设置为header。我期待GONE身高ViewHolder降低,与ViewHolder position: 0的身高相同。

    请注意,此ViewHolder在之前的状态中header设置为VISIBLE,可能会有回收的问题,但不确定。

3 个答案:

答案 0 :(得分:2)

我在通常拨打view.requestLayout()之后,在我的适配器#getView的末尾调用了binding.executePendingBindings() *来解决了这个问题。

*我现在正在使用旧学校Adapter,因此您应该搜索RecyclerView.Adapter的等效内容,可能是#onBindViewHolder()

答案 1 :(得分:0)

看起来布局实际上是正确的 - 忽略检查器中已消失对象的框架,当小部件被标记为已消失时,我们不会再次将其布局,因为它将被简单地跳过(在Studio中我们做,提供一种更简单的方法来查看正在发生的事情,但在真实设备上做这件事毫无意义。)

您使用的是哪种版本的ConstraintLayout?我似乎在这里得到了正确的行为:

enter image description here

标记第一个元素后:

enter image description here

答案 2 :(得分:-1)

对我有用的是将对象的初始可见性设置为“消失”,并通过代码更改其可见性。

类似这样:

我的xml:

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvMyobject"
    style="@style/myStyle"
    tools:text="@string/dummy_text"
    android:textAlignment="center"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/anyotherobject"
    android:visibility="gone"
/>

我的活动:

if (isSomethingToShow) {
    tvMyobject.setVisibility(View.VISIBLE);
    tvMyobject.setText(R.string.my_string_to_show);
}
相关问题