Android How to reuse a header layout as an empty view in a ListView -
i've been grappling problem throughout life of project. have many lists in project , of them have headers. have been making separate layout file , adding list using addheaderview(). problem when data (arraylist, in case) empty, header doesn't appear. after hours of searching way reuse header layout empty view, gave , decided replicate layout in code , assign empty view using setemptyview(). problem lot of headers contain clickable views , have double of clickable logic every view duplicated. tried include header before failed, because still don't quite understand how layouts inflated, etc.
finally, have come solution share community, in case others having similar problem. don't know if best way solve problem , feedback or suggestions welcomed.
here code layout contains list list view:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <listview android:id="@+id/lv_my_list" style="@style/liststyle" android:headerdividersenabled="false" /> <include layout="@layout/my_list_header" android:id="@+id/my_list_empty" /> </linearlayout>
the style defines layout width , height among other things.
now have layout file contains header view
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/my_list_header" > <button android:id="@+id/my_list_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="click me" /> </linearlayout>
i know linearlayout not efficient , experimenting using merge or other efficiency measures, first version works i'm going now. code:
// set header mylistview = (listview)findviewbyid(r.id.lv_my_list); view header = view.inflate(this, r.layout.my_list_header, null); button b = (button)header.findviewbyid(r.id.my_list_button); b.setonclicklistener(this); mylistview.addheaderview(header); // set empty view linearlayout ll = (linearlayout)findviewbyid(r.id.my_list_empty); b = (button)ll.findviewbyid(r.id.my_list_button); b.setonclicklistener(this); melocallistview.setemptyview(ll);
the reason wrapped button in layout because can set each instance of button use onclicklistener , refer both of them single id: r.id.my_list_button in onclick method. need test lot more seems work now. haven't implemented on lists yet, 1 might not work in situations. if have suggestions please let me know because has been problem me long time now. 1 problem might if want instantiate button id have go through entire process again access correct ids?
if want show header of listview
when list empty, want show list rather separate empty view. so, simplest way check whether list empty , set list visible if not already:
getlistview().setvisibility(view.visible);
you can test following code:
public class testlistactivity extends listactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.test_listview); // create empty grid item mapping string[] = new string[] {}; int[] = new int[] {}; // prepare empty list list<hashmap<string, string>> fillmaps = new arraylist<hashmap<string, string>>(); // fill in grid_item layout simpleadapter adapter = new simpleadapter(this, fillmaps, r.layout.test_listitem, from, to); view listheader = ((layoutinflater)getsystemservice(context.layout_inflater_service)) .inflate(r.layout.test_listheader, null, false); getlistview().addheaderview(listheader); getlistview().setadapter(adapter); } }
the list empty, header visible.
Comments
Post a Comment