Android 设置视图已消失,但仍占用空间
这是我在RecyleViewAdapater中使用的代码Android 设置视图已消失,但仍占用空间,android,kotlin,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
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)设置为不存在,但它仍然在第二项占用空间,而在第三项不占用空间
我试过各种各样的。那么,我该怎么修呢
我打开布局边界
那么,现在如何切断下面的高度呢?试试这个:
<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\u constraint bottom\u toTopOf=
你的代码是在一个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\u content
相应地。显示完整的xml显示您的xml PleaseRetryandroid:visibility=“invisible”
@SiddharthPatel不可见
占用区域我发布完整的xml文本大小是36还是1000并不重要。如果视图被设置为“已消失”,则应立即检查此答案。在模拟器的“开发人员选项”中启用“显示布局边界”。验证是否确实在不应显示的情况下显示了组内存的年。如果是,则验证yearSet.contains(yearOfMemory)
在逻辑上是否正确yearSet是HashSet(),顺便说一句,您的数据内容;)我添加了另一种解决方案ConstraintLayout
可能施加了一些您不知道的约束。