设置视图已消失但仍占用空间

时间:2018-04-02 05:18:05

标签: android kotlin

这是我在RecyleViewAdapater中使用的代码!

val yearOfMemory = SimpleDateFormat("yyyy").format(memory.timeOfMemory)
// if year not show before, show it.
if (!yearSet.contains(yearOfMemory)) {
  holder.yearOfMemories.visibility = View.VISIBLE
  holder.yearOfMemories.text = yearOfMemory
  yearSet.add(yearOfMemory)
}

这是我的ReccleView 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"
        tools:layout_editor_absoluteY="81dp">

        <TextView
            android:id="@+id/month_and_day_of_memory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/content_of_memory"
            app:layout_constraintHorizontal_bias="0.8"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/content_of_memory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.6"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/year_of_group_memory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:textSize="36sp"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@+id/month_and_day_of_memory"
            app:layout_constraintEnd_toStartOf="@+id/month_and_day_of_memory"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

enter image description here

像这样,我已经设置了TextView(@ + id / year_of_group_memory)Gone,但它仍占用第二项的空间,并且它不会占用第三项。

我尝试了各种各样的。那么,我该如何解决呢?

我打开了布局边界。

enter image description here

那么,现在怎样才能切断下面的高度?

2 个答案:

答案 0 :(得分:2)

您的代码是否在RecyclerView适配器中?如果是这样,由于视图回收机制,您必须明确隐藏它。隐藏/显示视图时始终有明确的if-else语句。

val yearOfMemory = SimpleDateFormat("yyyy").format(memory.timeOfMemory)
if (!yearSet.contains(yearOfMemory)) {
  holder.yearOfMemories.visibility = View.VISIBLE
  holder.yearOfMemories.text = yearOfMemory
  yearSet.add(yearOfMemory)
} else {
  holder.yearOfMemories.visibility = View.GONE
}

还尝试使用以下xml替换您的viewholder内容:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/year_of_group_memory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:textSize="36sp"
        android:lines="2"
        tools:text="2017"
        />

    <TextView
        android:id="@+id/month_and_day_of_memory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/year_of_group_memory"
        android:layout_alignBottom="@id/year_of_group_memory"
        android:layout_margin="8dp"
        android:textSize="18sp"
        tools:text="03-11"
        />

    <TextView
        android:id="@+id/content_of_memory"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/month_and_day_of_memory"
        android:layout_alignTop="@id/month_and_day_of_memory"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:textSize="18sp"
        tools:text="第一次接吻"
        />

</RelativeLayout>

如果您使用此功能,则必须相应地将yearOfMemories的高度设置为0或wrap_content

答案 1 :(得分:0)

试试这个:

<TextView
android:id="@+id/year_of_group_memory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>

你隐藏了这个TextView,但是你要将android:textSize="36sp"放在它不应该和其他边距上app:layout_constraintBottom_toTopOf=