setEmptyView()似乎并不为(福尔摩斯)ListFragment工作 [英] setEmptyView() doesn't seem to work for (Sherlock)ListFragment
问题描述
我有一个 ListFragment
,我想设置其空视图,因此我用下面的code:
I have a ListFragment
and I want to set its empty view, so I use the following code:
View emptyView = getLayoutInflater().inflate(R.layout.collection_empty_view, null);
detailFragment.getListView().setEmptyView(emptyView);
以下XML布局:
for the following xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/empty_collection" />
<Button
android:id="@+id/btn_add_legislation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addText"
android:text="@string/action_add_text" />
</LinearLayout>
但没有显示出来!
But nothing shows up!
我试过 detailFragment.getListView()。setEmptyText()
,而确实的工作!我做domething错了?
I tried detailFragment.getListView().setEmptyText()
, and that did work! Am I doing domething wrong?
推荐答案
空视图必须在同一视图层次结构列表视图。充气这种方式是行不通的。
The empty view has to be in the same view hierarchy as the list view. Inflating it that way won't work.
一般来说,我创建了下格式的布局:
Generally I create a layout in the format below:
<Layout>
<ListView/>
<View android:id="@+id/empty"/>
</Layout>
在哪里空的观点是我想为我的空观来使用视图。然后在创建我得到空的视图的引用,并与参考调用setEmptyView()。
Where empty view is the View I want to use as my "empty view". Then in on create I get a reference to the empty view and call setEmptyView() with that reference.
下面是从Android文档(查看文档为例,它应该适用到SherlockListFragment):
Here's an example from the Android documentation (check out the documentation, it should be applicable to SherlockListFragment):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
这篇关于setEmptyView()似乎并不为(福尔摩斯)ListFragment工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!