checkbox - Expandable listview like tree view in android -
i want create tree view in android. tried implement expandable listview checkboxes. want when list group checkbox checked, list items under group selected. user might able select particular items in listview.
please @ images:
how should in android? issue facing that, if add checkbox group of expandable listview, list can not expanded more.
source code:
activity_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#f4f4f4" > <expandablelistview android:id="@+id/lvexp" android:layout_height="match_parent" android:layout_width="match_parent" android:cachecolorhint="#00000000"/> </linearlayout>
list_group.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp" android:background="#cc7a00"> <checkbox android:id="@+id/lbllistheadercheckbox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:textsize="17dp" android:textcolor="#ffffff"/> <textview android:id="@+id/lbllistheader" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingleft="?android:attr/expandablelistpreferreditempaddingleft" android:textsize="17dp" android:textcolor="#ffffff" /> </linearlayout>
list_item.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="55dip" android:orientation="vertical" android:background="#ffcc80"> <textview android:id="@+id/lbllistitem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="17dip" android:paddingtop="5dp" android:paddingbottom="5dp" android:paddingleft="?android:attr/expandablelistpreferredchildpaddingleft" android:textcolor="#336699"/> </linearlayout>
mainactivity:
public class mainactivity extends activity { expandablelistadapter listadapter; expandablelistview explistview; list<string> listdataheader; hashmap<string, list<string>> listdatachild; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // listview explistview = (expandablelistview) findviewbyid(r.id.lvexp); // preparing list data preparelistdata(); listadapter = new expandablelistadapter(this, listdataheader, listdatachild); // setting list adapter explistview.setadapter(listadapter); // listview group click listener explistview.setongroupclicklistener(new ongroupclicklistener() { @override public boolean ongroupclick(expandablelistview parent, view v, int groupposition, long id) { // toast.maketext(getapplicationcontext(), // "group clicked " + listdataheader.get(groupposition), // toast.length_short).show(); return false; } }); // listview group expanded listener explistview.setongroupexpandlistener(new ongroupexpandlistener() { @override public void ongroupexpand(int groupposition) { toast.maketext(getapplicationcontext(), listdataheader.get(groupposition) + " expanded", toast.length_short).show(); } }); // listview group collasped listener explistview.setongroupcollapselistener(new ongroupcollapselistener() { @override public void ongroupcollapse(int groupposition) { toast.maketext(getapplicationcontext(), listdataheader.get(groupposition) + " collapsed", toast.length_short).show(); } }); // listview on child click listener explistview.setonchildclicklistener(new onchildclicklistener() { @override public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) { // todo auto-generated method stub toast.maketext( getapplicationcontext(), listdataheader.get(groupposition) + " : " + listdatachild.get( listdataheader.get(groupposition)).get( childposition), toast.length_short) .show(); return false; } }); } /* * preparing list data */ private void preparelistdata() { listdataheader = new arraylist<string>(); listdatachild = new hashmap<string, list<string>>(); (int i=0; i<=10; i++) { listdataheader.add("header"+ i); // adding child data list<string> child = new arraylist<string>(); child.add("child"+i); child.add("child"+i); listdatachild.put(listdataheader.get(i), child); // header, child data } } }
expandablelistadapter:
public class expandablelistadapter extends baseexpandablelistadapter { private context _context; private list<string> _listdataheader; // header titles // child data in format of header title, child title private hashmap<string, list<string>> _listdatachild; public expandablelistadapter(context context, list<string> listdataheader, hashmap<string, list<string>> listchilddata) { this._context = context; this._listdataheader = listdataheader; this._listdatachild = listchilddata; } @override public object getchild(int groupposition, int childposititon) { return this._listdatachild.get(this._listdataheader.get(groupposition)) .get(childposititon); } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public view getchildview(int groupposition, final int childposition, boolean islastchild, view convertview, viewgroup parent) { final string childtext = (string) getchild(groupposition, childposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.list_item, null); } textview txtlistchild = (textview) convertview .findviewbyid(r.id.lbllistitem); txtlistchild.settext(childtext); return convertview; } @override public int getchildrencount(int groupposition) { return this._listdatachild.get(this._listdataheader.get(groupposition)) .size(); } @override public object getgroup(int groupposition) { return this._listdataheader.get(groupposition); } @override public int getgroupcount() { return this._listdataheader.size(); } @override public long getgroupid(int groupposition) { return groupposition; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { string headertitle = (string) getgroup(groupposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.list_group, null); } textview lbllistheader = (textview) convertview .findviewbyid(r.id.lbllistheader); lbllistheader.settypeface(null, typeface.bold); lbllistheader.settext(headertitle); return convertview; } @override public boolean hasstableids() { return false; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } }
how should achieve requirement?
try adding checkbox (in list_group.xml file):
android:focusable="false"
Comments
Post a Comment