Recently, when I was doing a project, I ran into a layout
In Android there is a way to implement the folding list is expandablelistview but the official no Expandablegridview
So how do you use Expandablelistview to achieve a expandablegridview?
The approximate principle is: to display a row in each Expandablelistview, use this line to load a GridView
accreditations, put the code on.
1. Load a Expandablelistview
Private Simpledataadapter Simpledataadapter; Private Expandablelistview Mexpandablelistview; Mexpandablelistview = (Expandablelistview) Findviewbyid (r.id.expandablelist); Msuperadapter = new Supervesedataexpandableadapter (this,msupervesedata); Mexpandablelistview.setadapter (msuperadapter); Mexpandablelistview.expandgroup (0);
2. Rewrite Baseexpandablelistadapter
overriding Baseexpandablelistadapter, mostly overriding the Getchildview method to load a GridView dynamic calculation of the GridView height
@Override public View getchildview (int groupposition, int childposition, Boolean islastchild, View Convertview, Vie Wgroup parent) {if (Convertview = = null) {Convertview = layoutinflater.inflate (R.layout.view, NULL); } Customgridview GridView = (customgridview) convertview. Findviewbyid (R.id.gridview_toolbar); Gridview.setnumcolumns (2); Gridview.sethorizontalspacing (10); Gridview.setgravity (Gravity.center); Gridadapter adapter = new Gridadapter (context, Supervesedatas.get (groupposition). Controldatas); Gridview.setadapter (adapter);//Adapter int totalheight = 0; for (int size = 0; size < Adapter.getcount (); size++) {Relativelayout relativelayout = (relativelayout) Ada Pter.getview (size, null, GridView); TextView TextView = (TextView) relativelayout.getchildat (0); Textview.measure (0, 0); Totalheight + = Textview.getmeasuredheight (); if (size = = Adapter.getcount ()-1) {if (size%2 = = 0) {totalheight + = Textview.getmeasuredheight () * 2; }}} gridview.setheight (Totalheight); return convertview; }
Another place to be aware of is:
Getchildrencount returns 1 because the GridView is loaded with an item
@Override public int getchildrencount (int groupposition) { return 1; }
The result of the final implementation is as follows
Implementation of Android Expandablegridview