这是我在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>

像这样,我已经设置了TextView(@ + id / year_of_group_memory)了,但是它仍然在第二项中占用空间,而在第三项中不占用空间。

我尝试过各种。那么,我该如何解决。

我打开布局范围。

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

最佳答案

您的代码是否在RecyclerView适配器中?如果是这样,由于 View 回收机制,您必须显式隐藏它。隐藏/显示 View 时,始终具有明确的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

08-03 18:55