Dynamically sets the height of the GridView, fixed column, and sets the height according to the item number in the GridView:
Call the following methods:
public static void Setlistviewheightbasedonchildren (GridView listview) {//Get ListView Adapter ListAdapter List Adapter = Listview.getadapter (); if (ListAdapter = = null) {return; }//fixed column width, how many columns int col = 4;//Listview.getnumcolumns (); int totalheight = 0; I add 4 each time, equivalent to Listadapter.getcount () is less than or equal to 4 o'clock cycles once, calculates the height of the item once,//Listadapter.getcount () is less than or equal to 8 o'clock calculates two times the height of the add for (int i = 0; I < Listadapter.getcount (); i + = col) {//Get ListView for each item View ListItem = Listadapter.getview (i, NULL, ListView); Listitem.measure (0, 0); Gets the height of item and Totalheight + = Listitem.getmeasuredheight (); }//Get the layout parameters of the ListView viewgroup.layoutparams params = Listview.getlayoutparams (); Set height params.height = totalheight; Set the margin ((marginlayoutparams) params. SetMargins (10, 10, 10, 10); Set the parameter listview.setlayoutparams (params);}
After calling this method, you need to invoke the Notifydatasetchanged () method to implement the interface refresh
Dynamically set the GridView height, fixed column, for highly adaptive