Android How To Reuse A Header Layout As An Empty View In A Listview
I've been grappling with this problem throughout the life of my project. I have many lists in my project and most of them have headers. I have been making a separate layout file
Solution 1:
If you want to show the header of a ListView
when the list is empty, what you really want to do is to show the list itself rather than a separate empty view. So, the simplest way is to check whether your list is empty and set the list visible if it is not already:
getListView().setVisibility(View.VISIBLE);
You can test this very easily with the following code:
publicclassTestListActivityextendsListActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_listview);
// create the empty grid item mapping
String[] from = newString[] {};
int[] to = newint[] {};
// prepare the empty list
List<HashMap<String, String>> fillMaps = newArrayList<HashMap<String, String>>();
// fill in the grid_item layoutSimpleAdapteradapter=newSimpleAdapter(this, fillMaps, R.layout.test_listitem, from, to);
ViewlistHeader=
((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.test_listheader, null, false);
getListView().addHeaderView(listHeader);
getListView().setAdapter(adapter);
}
}
The list is empty, but the header is visible.
Solution 2:
My solution is to create a view with the layout for the header, but set the background to be the same as the list items. android:background="@android:drawable/list_selector_background"
But my solution does not work if I use 'include' to embed the header layout from another layout file. Do not know why.
Post a Comment for "Android How To Reuse A Header Layout As An Empty View In A Listview"