如何在 Recycler View 为空时显示 TextView?
How to display a TextView when Recycler View is empty?
我想在我的 RecyclerView 为空时显示一个 textView。
我准备了这个功能,但它不起作用。我相信我应该从 RecyclerView 获取列表,但我真的不知道如何获取。
我在碎片中。
XML:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/item_list" />
<TextView
android:id="@+id/tv_no_records"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/nothing_to_display"
android:textSize="16sp"
android:visibility="gone" />
片段:
private fun displayList() {
val list = listOf<Shoe>()
if (list.isEmpty()) {
binding.recyclerViewList.visibility = View.VISIBLE
binding.tvNoRecords.visibility = View.GONE
} else {
binding.recyclerViewList.visibility = View.GONE
binding.tvNoRecords.visibility = View.VISIBLE
}
}
添加鞋子(在 ViewModel 中):
fun addShoe(shoe: Shoe) {
viewModelScope.launch(Dispatchers.IO) {
repository.addShoes(shoe)
}
}
非常感谢,
安娜
条件颠倒了。当列表为空时,您将显示 recyclerView,反之亦然。只需使用不检查。
考虑以下:
private fun displayList() {
val list = listOf<Shoe>()
if (list.isNotEmpty()) {
binding.recyclerViewList.visibility = View.VISIBLE
binding.tvNoRecords.visibility = View.GONE
} else {
binding.recyclerViewList.visibility = View.GONE
binding.tvNoRecords.visibility = View.VISIBLE
}
}
我想在我的 RecyclerView 为空时显示一个 textView。 我准备了这个功能,但它不起作用。我相信我应该从 RecyclerView 获取列表,但我真的不知道如何获取。 我在碎片中。
XML:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/item_list" />
<TextView
android:id="@+id/tv_no_records"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/nothing_to_display"
android:textSize="16sp"
android:visibility="gone" />
片段:
private fun displayList() {
val list = listOf<Shoe>()
if (list.isEmpty()) {
binding.recyclerViewList.visibility = View.VISIBLE
binding.tvNoRecords.visibility = View.GONE
} else {
binding.recyclerViewList.visibility = View.GONE
binding.tvNoRecords.visibility = View.VISIBLE
}
}
添加鞋子(在 ViewModel 中):
fun addShoe(shoe: Shoe) {
viewModelScope.launch(Dispatchers.IO) {
repository.addShoes(shoe)
}
}
非常感谢, 安娜
条件颠倒了。当列表为空时,您将显示 recyclerView,反之亦然。只需使用不检查。 考虑以下:
private fun displayList() {
val list = listOf<Shoe>()
if (list.isNotEmpty()) {
binding.recyclerViewList.visibility = View.VISIBLE
binding.tvNoRecords.visibility = View.GONE
} else {
binding.recyclerViewList.visibility = View.GONE
binding.tvNoRecords.visibility = View.VISIBLE
}
}