Most of the time we use ListView and GridView because the height of the item is uncertain, it is dynamic change, at this time we need to adapt the height, we can consider creating a tool class to put the following static method, when using the class name to call the method into the GridView or ListView object can achieve the effect
/ * * * ListView method of adaptive height * * @ param ListView * / public static void setListViewHeightBasedOnChild (ListView ListView) {/ / ListAdapter = listView.getAdapter(); ListAdapter = ListView.getAdapter (); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); View listItem = ListAdapter.getView (I, null, listView); listAdapter.getView(I, null, listView); listItem.measure(0, 0); / / calculation item View wide high totalHeight + = listItem. GetMeasuredHeight (); } viewgroup.layoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); / / listView. GetDividerHeight () to obtain the height of the separator between items take up / / params. Height finally get the whole listView display need to complete the height of the listView. SetLayoutParams (params); }}Copy the code