How to allow single child selection in android expandable list view? -
i using expandable list view child.i tried set single child selection in parent.please me dont know how do.
public view getchildview(final int i, final int i1, boolean b, view view, viewgroup viewgroup) { final childviewholder childviewholder; if(view==null) { childviewholder=new childviewholder(); view=((layoutinflater)context.getsystemservice(context.layout_inflater_service)). inflate(r.layout.product_listview_child_itemnew,null); childviewholder.choice1=(textview)view.findviewbyid(r.id.textview1); childviewholder.choice1.settextcolor(color.parsecolor("#000000")); childviewholder.checkbox1=(checkbox)view.findviewbyid(r.id.checkbox1); //childviewholder.checkbox1.settag(getquestion.get(i)); view.settag(childviewholder); } else { childviewholder=(childviewholder)view.gettag(); } childviewholder.checkbox1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { setquestion=getquestion.get(i).getoptions().get(i1); log.d("vikisimp",":"+setquestion); } }); childviewholder.choice1.settext(getquestion.get(i).getoptions().get(i1)); return view; }
please find solution of single selection in expandable listview below: expandableactivity.java public class expandableactivity extends appcompatactivity { private expandablelistview expandablelistview; arraylist<string> grouplist = new arraylist<string>(); arraylist<arraylist<answer>> childlist = new arraylist<arraylist<answer>>(); expandablelistadapter expandablelistadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_expandable); expandablelistview = (expandablelistview)findviewbyid(r.id.expandablelistview); grouplist.add("question 1"); grouplist.add("question 2"); grouplist.add("question 3"); grouplist.add("question 4"); grouplist.add("question 5"); arraylist<answer> answerlist1 = new arraylist<>(); arraylist<answer> answerlist2 = new arraylist<>(); arraylist<answer> answerlist3 = new arraylist<>(); arraylist<answer> answerlist4 = new arraylist<>(); arraylist<answer> answerlist5 = new arraylist<>(); setdata("answer 1", 1, false, answerlist1); setdata("answer 2 ", 2, false, answerlist1); setdata("answer 3", 3, false, answerlist1); setdata("answer 4", 4, false, answerlist1); setdata("answer 1", 5, false, answerlist2); setdata("answer 2 ",6, false, answerlist2); setdata("answer 3", 7, false, answerlist2); setdata("answer 4", 8, false, answerlist2); setdata("answer 1", 9, false, answerlist3); setdata("answer 2 ",10, false, answerlist3); setdata("answer 3", 11, false, answerlist3); setdata("answer 4", 12, false, answerlist3); setdata("answer 1", 13, false, answerlist4); setdata("answer 2 ", 14, false, answerlist4); setdata("answer 3", 15, false, answerlist4); setdata("answer 4", 16, false, answerlist4); setdata("answer 1", 17, false, answerlist5); setdata("answer 2 ", 18, false, answerlist5); setdata("answer 3", 19, false, answerlist5); setdata("answer 4", 20, false, answerlist5); childlist.add(answerlist1); childlist.add(answerlist2); childlist.add(answerlist3); childlist.add(answerlist4); childlist.add(answerlist5); expandablelistadapter = new expandablelistadapter(this,grouplist,childlist); expandablelistview.setadapter(expandablelistadapter); expandablelistview.setonchildclicklistener(new expandablelistview.onchildclicklistener() { @override public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) { for(int =0;i<childlist.get(groupposition).size();i++) { if(childposition!=i) childlist.get(groupposition).get(i).setstate(false); } childlist.get(groupposition).get(childposition).setstate(!childlist.get(groupposition).get(childposition).isstate()); expandablelistadapter.notifydatasetchanged(); return false; } }); } public void setdata(string text, int id, boolean state, arraylist<answer> answerlist) { answer answer = new answer(); answer.setanswer(text); answer.setanswerid(id); answer.setstate(state); answerlist.add(answer); } } activity_expandable.xml <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="?attr/colorprimary" app:popuptheme="@style/apptheme.popupoverlay" /> <expandablelistview android:id="@+id/expandablelistview" android:layout_height="match_parent" android:layout_width="match_parent" /> </linearlayout> expandablelistadapter.java public class expandablelistadapter extends baseexpandablelistadapter { private context _context; private arraylist<string> groupitems; arraylist<arraylist<answer>> childitems = new arraylist<arraylist<answer>>(); public expandablelistadapter(context context, arraylist<string> groupitems, arraylist<arraylist<answer>> childitems) { this._context = context; this.groupitems = groupitems; this.childitems = childitems; } @override public object getchild(int groupposition, int childposititon) { return childitems.get( groupposition ).get(childposititon); } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public view getchildview(final int groupposition, final int childposition, boolean islastchild, view convertview, viewgroup parent) { final answer answer = (answer)getchild( groupposition, childposition ); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.label_child, null); } textview textviewchild = (textview) convertview .findviewbyid(r.id.textviewchild); checkbox checkboxchild = (checkbox)convertview.findviewbyid(r.id.checkboxchild); textviewchild.settext(answer.getanswer()); checkboxchild.setchecked(answer.isstate()); return convertview; } @override public int getchildrencount(int groupposition) { return childitems.get(groupposition).size(); } @override public object getgroup(int groupposition) { return groupitems.get(groupposition); } @override public int getgroupcount() { return groupitems.size(); } @override public long getgroupid(int groupposition) { return groupposition; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { string grouptitle = (string) getgroup(groupposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.label_group, null); } textview textviewgroup = (textview) convertview .findviewbyid(r.id.textviewgroup); textviewgroup.settext(grouptitle); expandablelistview mexpandablelistview = (expandablelistview) parent; mexpandablelistview.expandgroup(groupposition); return convertview; } @override public boolean hasstableids() { return true; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } } labelgroup.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="@android:color/white"> <textview android:id="@+id/textviewgroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingleft="?android:attr/expandablelistpreferreditempaddingleft" android:textsize="16sp" android:textcolor="@android:color/black" /> </linearlayout>
label_child.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="horizontal" android:padding="8dp" android:background="@android:color/white"> <textview android:id="@+id/textviewchild" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:paddingleft="?android:attr/expandablelistpreferreditempaddingleft" android:textsize="16sp" android:textcolor="@android:color/holo_blue_dark" android:text="example"/> <checkbox android:id="@+id/checkboxchild" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:focusableintouchmode="false" android:clickable="false"/> </linearlayout>