Android addHeaderView disappears when no items in ListView
I am using addHeaderView
to add a view item to the top of a ListView
. I also have a TextView
to display a message saying there are no items in the list.
Here is the layout:
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/list_empty"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
And the Java code:
final ListView listView = getListView();
final View view = getLayoutInflater().inflate(R.layout.list_item_add,
listView, false);
listView.开发者_StackOverflow中文版addHeaderView(view, null, true);
When there are items in the ListView then the header is shown but if I delete all items in the list (except the header view) then the header view disappears.
I would like the header view to be visible in the list view whether there are items in the list or not.
Thanks,
Remove the @android:id/empty
view from your layout, or override/subclass your adapter to return false from isEmpty()
From my experience (SDK version 10):
Overriding isEmpty() in the adapter makes it work.
Then, it is optional to remove the @android:id/empty view.
精彩评论