user interface - Multi-level ExpandableListView with CheckBoxes in Android -
i'm trying implement 3-level expandablelistview
in android.
it looks this:
- level 1
- level 2
- level 2
- level 3
- level 2
- level 1
- level 2
- level 2
level 2 , level 3 supposed have checkbox
-es. , when checkbox
in level 2 selected, children in level 3 selected well.
i've done 2 adapters - both extends baseexpandablelistadapter
. in end 2 inserted expandablelistview
-s - 1 level 1 - level 2, , other level 2 - level 3.
also have 2 separate layouts - 1 level 1 , 1 levels 2-3 (since same). its a
textviewwith id inside
relativeview`.
the questions:
when i'm changing
textview
checkbox
es , setting text them, cannot expand list third level. don't understand why it's happening , it, can me?how can hide indicators (those arrow showing expanded/collapsed state) level 2 items, have no children?
i don't why
expandablelistview
doesn't want fit screen vertically, consumes 1/4 of it. i've tried change params incustomexpandablelistview
, not worked.
here screenshots:
screenshot 1:
screenshot 2:
as can see, when expand asia list, europe list hides itself.
here`s code level 1:
public class regionsadapter extends baseexpandablelistadapter { private list<object> objects; private activity activity; private layoutinflater inflater; public regionsadapter(activity activity, list<object> objects) { this.objects = objects; this.activity = activity; this.inflater = (layoutinflater) activity.getsystemservice(context.layout_inflater_service); } @override public object getchild(int groupposition, int childposition) { return objects.get(groupposition).getobjects().get(childposition); } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { object object = (object) getchild(groupposition, childposition); customexpandablelistview subobjects = (customexpandablelistview) convertview; if (convertview == null) { subobjects = new customexpandablelistview(activity); } countriesadapter adapter = new countriesadapter(activity, object); subobjects.setadapter(adapter); return subobjects; } @override public int getchildrencount(int groupposition) { return objects.get(groupposition).getobjects().size(); } @override public object getgroup(int groupposition) { return objects.get(groupposition); } @override public int getgroupcount() { return objects.size(); } @override public long getgroupid(int groupposition) { return groupposition; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { object object = (object) getgroup(groupposition); if (convertview == null) { convertview = inflater.inflate(r.layout.layout_region_item, null); } textview name = (textview) convertview.findviewbyid(r.id.name); name.settext(object.getname()); return convertview; } @override public boolean hasstableids() { return true; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } }
and code level 2-3:
public class countriesadapter extends baseexpandablelistadapter { private object object; private layoutinflater inflater; private activity activity; public countriesadapter(activity activity, object object) { this.activity = activity; this.object = object; this.inflater = (layoutinflater) activity.getsystemservice(context.layout_inflater_service); } @override public object getchild(int groupposition, int childposition) { return object.getobjects().get(childposition); } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { object object = (object) getchild(0, childposition); if (convertview == null) { convertview = inflater.inflate(r.layout.layout_country_item, null); resources r = activity.getresources(); float px40 = typedvalue.applydimension(typedvalue.complex_unit_dip, 40, r.getdisplaymetrics()); convertview.setpadding( convertview.getpaddingleft() + (int) px40, convertview.getpaddingtop(), convertview.getpaddingright(), convertview.getpaddingbottom()); } textview name = (textview) convertview.findviewbyid(r.id.name); name.settext(object.getname()); return convertview; } @override public int getchildrencount(int groupposition) { if (object.getobjects() == null) { return 0; } return object.getobjects().size(); } @override public object getgroup(int groupposition) { return object; } @override public int getgroupcount() { return 1; } @override public long getgroupid(int groupposition) { return groupposition; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { if (convertview == null) { convertview = inflater.inflate(r.layout.layout_country_item, null); resources r = activity.getresources(); float px20 = typedvalue.applydimension(typedvalue.complex_unit_dip, 20, r.getdisplaymetrics()); convertview.setpadding( convertview.getpaddingleft() + (int) px20, convertview.getpaddingtop(), convertview.getpaddingright(), convertview.getpaddingbottom()); } textview name = (textview) convertview.findviewbyid(r.id.name); name.settext(object.getname()); return convertview; } @override public boolean hasstableids() { return true; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } }
also have custom expandablelistview:
public class customexpandablelistview extends expandablelistview { public customexpandablelistview(context context) { super(context); } protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { widthmeasurespec = measurespec.makemeasurespec(960, measurespec.at_most); heightmeasurespec = measurespec.makemeasurespec(600, measurespec.at_most); super.onmeasure(widthmeasurespec, heightmeasurespec); } }
and mainactivity code:
public class mainactivity extends appcompatactivity { public static customexpandablelistview list; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); list<item> objectslvl1 = new arraylist<item>(); list<item> objectsasia = new arraylist<item>(); list<item> objectseurope = new arraylist<item>(); list<item> objectschina = new arraylist<item>(); list<item> objectsgermany = new arraylist<item>(); objectslvl1.add(new item("asia", objectsasia)); objectslvl1.add(new item("europe", objectseurope)); objectsasia.add(new item("china", objectschina)); objectsasia.add(new item("japan")); objectschina.add(new item("central", null)); objectschina.add(new item("north", null)); objectschina.add(new item("south", null)); relativelayout parent = (relativelayout) findviewbyid(r.id.parent); list = new customexpandablelistview(this); regionsadapter regionsadapter = new regionsadapter(this, objectslvl1); list.setadapter(regionsadapter); if (parent != null) { parent.addview(list); } } }
yet again, in end i've figured out. answer.
- expanding list checkboxes:
this 1 pretty simple. i've added onclicklistener
textview
inside getgroupview
method:
view.onclicklistener clicklistener = new view.onclicklistener() { @override public void onclick(view v) { if (!isexpanded) { expandablelistview.expandgroup(groupposition); } else { expandablelistview.collapsegroup(groupposition); } } }; holder.childname.setonclicklistener(clicklistener); holder.childindicator.setonclicklistener(clicklistener);
- hiding indicators:
it easier replace standart indicators , add own. , then, inside getgroupview
work it.
at first, setting default groupindicator
null
:
expandablelistview.setgroupindicator(null);
than, working custom images:
holder.childindicator = (imageview) convertview.findviewbyid(r.id.imgchildindicator); if (getchildrencount(groupposition) == 0) { holder.childindicator.setalpha(0); } else { holder.childindicator.setimageresource(isexpanded ? r.drawable.expanded : r.drawable.collapsed); }
and finally, hiding groupindicator
level 3 items:
holder.childindicator.setalpha(0);
the same changes made level 1 items.
- fitting expandablelistview on screen:
i've put expandablelistview
inside relativelayout
in activity_main
.
and changed realization inside mainactivity:
expandablelistview list = (expandablelistview) findviewbyid(r.id.expandablelistview_parent); if (list != null) { parentadapter adapter = new parentadapter(this, objectslvl1); list.setadapter(adapter); }
this solved issue, have full screen expandablelist
.
Comments
Post a Comment