Some time ago encountered problems with the ListView and ScrollView layouts, and now provides a solution
if (ListAdapter = = NULL | | Listadapter.getcount () = = 0) { return;} int totalheight = 0;for (int i = 0; i < Listadapter.getcount (); i++) { View ListItem = Listadapter.getview (i, NULL, ListView); Listitem.measure (0, 0); Totalheight + = Listitem.getmeasuredheight ();} Viewgroup.layoutparams params = Listview.getlayoutparams ();p arams.height = Totalheight + (Listview.getdividerheight () * (Listadapter.getcount ()-1)); Listview.setlayoutparams (params); Listview.clearfocus ();
This has a very important problem in calling Listitem.measure (0, 0); Sometimes error, the root cause lies in the layout of the ListItem, the outermost layout with LinearLayout, in the call listitem.measure (0, 0), is essentially called linearlayout.measure method; Relativelayout layout is the same, however, calling the Relativelayout. Measure method will cause an error.
Workaround:
The outermost layer of the 1.listitem layout is linearlayout.
2. ListItem layout, relativelayout layout, custom relativelayout and override Onmeasure method
@Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) { int widthsize = Measurespec.getsize (WIDTHMEASURESPEC); int heightsize = measurespec.getsize (heightmeasurespec); Restrict the aspect ratio to 1:1, fitting within original specified dimensions int chosendimension = math.min (width Size, heightsize); Widthmeasurespec = Measurespec.makemeasurespec (chosendimension, measurespec.at_most); Heightmeasurespec = Measurespec.makemeasurespec (chosendimension, measurespec.at_most); Getlayoutparams (). height = chosendimension; Getlayoutparams (). width = chosendimension; Super.onmeasure (Widthmeasurespec, Heightmeasurespec); }
Android Listview,gridview and ScrollView