10

我有一个ViewHolder顶部的标题,并且该标题在特定情况下变得可见。在大多数其他情况下,标头设置为GONE. 问题是当一个标题设置为 GONE 时,它的高度仍然被计算并且其他视图是spread不同的(之间有更多的空间)。

这是一个布局蓝图: 蓝图

蓝图说明:

  1. 标头被限制为top,leftright
  2. 下面的两个视图在 中,被限制在、和 上的packed chain标题以及和的父级上。topImageViewrightleftbottom

这是布局检查器的屏幕截图,其中突出显示的标题视图设置为GONE在此处输入图像描述

根据文档,当设置为时,标题视图GONE应缩小为指向仍然应用其他视图的约束,但标题不应占用布局空间并影响ConstraintLayout设置为的高度wrap_content

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

我不确定这是一个错误还是我应该强制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

经过额外的观察,这个问题只发生在调用notifyDataSetChangedin之后RecyclerView.Adapter。这是单击favorite其中一个项目的图标之前和之后的布局状态的屏幕截图。 在此处输入图像描述

截图说明:

  • 在左侧,ViewHolder可见header视图处于打开状态position: 2。以上项目显示正确。
  • 单击favorite图标(值为 242 的项目)后,ViewHolderonposition: 1是具有可见header视图的图标,而ViewHolderonposition: 2具有header视图设置为GONE. 我期待高度降低并具有与onViewHolder相同的高度。ViewHolderposition: 0

考虑到这ViewHolder已经header设置为VISIBLE以前的状态,它可能与回收有关,不确定。

4

3 回答 3

2

view.requestLayout()#getView通常调用binding.executePendingBindings().

*我现在使用的是老式的Adapter,所以你应该搜索RecyclerView.Adapter' 的等价物,可能#onBindViewHolder()

于 2017-06-27T15:33:47.663 回答
0

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

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

在此处输入图像描述

将第一个元素标记为消失后:

在此处输入图像描述

于 2017-04-28T19:29:25.813 回答
-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);
}
于 2020-05-12T17:47:50.740 回答