最简单的解决方案是使用RecyclerView。AdapterDataObserver并在适配器初始化后将其注册到recyclerview中。
val emptyDataObserver = EmptyDataObserver(recycler_view, empty_data_parent)
yourAdapter.registerAdapterDataObserver(emptyDataObserver)
其中,recycler_view & empty_data_parent是布局在你的活动,约束这些视图,你想要的,使其可见性消失。然后用图像和文本创建自己的空数据集视图。
<include
android:id="@+id/empty_data_parent"
layout="@layout/item_empty_dataset"
android:layout_width="match_parent"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
下面是empty_data_set_view.xml的示例
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
tools:ignore="RtlHardcoded">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.422"
android:src="@drawable/ic_empty_dataset_1" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Title is called when title is placed"
android:padding="10dp"
android:fontFamily="@font/normal"
android:textStyle="bold"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
<TextView
android:id="@+id/sub_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=" Subtitle is called when title is placed. Subtitle is called when title is placed"
android:padding="5dp"
android:fontFamily="@font/normal"
android:gravity="center"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title" />
</androidx.constraintlayout.widget.ConstraintLayout>
和这个EmptyDataObserver类来做这项工作。
import android.view.View
import androidx.recyclerview.widget.RecyclerView
class EmptyDataObserver constructor(rv: RecyclerView?, ev: View?): RecyclerView.AdapterDataObserver() {
private var emptyView: View? = null
private var recyclerView: RecyclerView? = null
init {
recyclerView = rv
emptyView = ev
checkIfEmpty()
}
private fun checkIfEmpty() {
if (emptyView != null && recyclerView!!.adapter != null) {
val emptyViewVisible = recyclerView!!.adapter!!.itemCount == 0
emptyView!!.visibility = if (emptyViewVisible) View.VISIBLE else View.GONE
recyclerView!!.visibility = if (emptyViewVisible) View.GONE else View.VISIBLE
}
}
override fun onChanged() {
super.onChanged()
checkIfEmpty()
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
super.onItemRangeChanged(positionStart, itemCount)
}
}