如何显示与RecyclerView一个空的观点? [英] How to show an empty view with a RecyclerView?
问题描述
我习惯了把一个特殊的视图布局文件中作为在 ListActivity
文档是当没有数据。这种观点具有ID 。机器人:ID /空
I am used to put an special view inside the layout file as described in the ListActivity
documentation to be displayed when there is no data. This view has the id "android:id/empty"
.
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_data" />
我不知道这是如何与新的<一做href="http://developer.android.com/reference/android/support/v7/widget/RecyclerView.html"><$c$c>RecyclerView$c$c>?
推荐答案
在那里的定义相同的布局 RecyclerView
添加的TextView
:
On the same layout where is defined the RecyclerView
, add the TextView
:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="@string/no_data_available" />
在的onCreate
或适度回调,你检查的饲料数据集的 RecyclerView
是空的。
如果数据集是空的, RecyclerView
是空的了。在这种情况下,屏幕上显示的消息。
如果不是,改变它的可见性:
At the onCreate
or the appropriate callback you check if the dataset that feeds your RecyclerView
is empty.
If the dataset is empty, the RecyclerView
is empty too. In that case, the message appears on the screen.
If not, change its visibility:
private RecyclerView recyclerView;
private TextView emptyView;
// ...
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
emptyView = (TextView) rootView.findViewById(R.id.empty_view);
// ...
if (dataset.isEmpty()) {
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}
这篇关于如何显示与RecyclerView一个空的观点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!