android - Adding a button for each group in an ExpandableListView -
i'm using expandablelistview
group items , child items generated data get. need button on bottom of each generated group onclicklistener invokes function has know group button affiliated to.
adding list_group.xml
layout file add button on sample data title-header , adding list_item.xml
add every single sample data-textview element.
it's first project using android studio , got stuck trying fix problem.
here screenshot of how list looks like
i'm trying add button button item is.
this function fill list data:
private void preparelistdata(ccypair[] ccypairs) { listdataheader = new arraylist<string>(); listdatachild = new hashmap<string, list<string>>(); int counter = 0; // adding child data (ccypair p : ccypairs){ listdataheader.add("sample data title"); list<string> tempexpinfo = new arraylist<string>(); tempexpinfo.add("sample data"); tempexpinfo.add("sample data"); tempexpinfo.add("sample data"); tempexpinfo.add("sample data"); tempexpinfo.add("sample data"); tempexpinfo.add("sample data"); tempexpinfo.add("button"); listdatachild.put(listdataheader.get(counter), tempexpinfo); counter++; } }
list_group.xml file:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp" android:background="@color/colorexpandablelist"> <textview android:id="@+id/lbllistheader" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingleft="?android:attr/expandablelistpreferreditempaddingleft" android:textsize="17dp" android:paddingtop="10dp" android:paddingbottom="10dp" android:background="@color/colorexpandablelist" android:textcolor="#2282c1" android:layout_alignparenttop="true"/> </relativelayout>
list_item.xml file:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorexpandablelist"><![cdata[ android:orientation="vertical" > ]]> <textview android:id="@+id/lbllistitem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/colorexpandablelist" android:paddingbottom="5dp" android:paddingleft="?android:attr/expandablelistpreferredchildpaddingleft" android:paddingtop="5dp" android:textsize="17dip" /> </linearlayout>
and 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; } }
getchildview() method wanna look
first option:
in getchildview() method have distinguish view want inflate.
if(islastchild){ convertview = inflater.inflate(r.layout.list_item_with_button, null); } else { convertview = inflater.inflate(r.layout.list_item, null); }
for last child create list_item_with_button.xml contains button component. can check islastchild boolean flag inflate new layout button @ last position.
second option: can edit existing list_item.xml , add button it, show @ last position by
view button = convertview.findviewbyid(r.id.mybutton); if(islastchild){ v.setvisibility(view.visible); } else { v.setvisibility(view.gone); }
Comments
Post a Comment