Note: This article is a collection of solutions shared by the great God Online
Method One:
Rewrite the ListView, GridView:
Rewrite the ListView:
1 Public classMylistviewextendsListView {2 3 PublicMylistview (Context context) {4 //TODO auto-generated Method Stub5 Super(context);6 }7 8 PublicMylistview (Context context, AttributeSet attrs) {9 //TODO auto-generated Method StubTen Super(context, attrs); One } A - PublicMylistview (context context, AttributeSet attrs,intDefstyle) { - //TODO auto-generated Method Stub the Super(context, attrs, defstyle); - } - - @Override + protected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { - //TODO auto-generated Method Stub + intExpandspec = Measurespec.makemeasurespec (integer.max_value >> 2, A measurespec.at_most); at Super. Onmeasure (Widthmeasurespec, expandspec); - } -}
Overriding the GridView:
1 /**2 * Custom GridView to troubleshoot nested gridview in ListView display unhealthy problem (1 rows and half)3 * @authorWangyx4 * @version1.0.0 2012-9-145 */6 Public classMyGridViewextendsgridview{7 PublicMyGridView (Context context, AttributeSet attrs) {8 Super(context, attrs);9 } Ten One PublicMyGridView (Context context) { A Super(context); - } - the PublicMyGridView (context context, AttributeSet attrs,intDefstyle) { - Super(context, attrs, defstyle); - } - + @Override - Public voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { + A intExpandspec = Measurespec.makemeasurespec (integer.max_value >> 2, at measurespec.at_most); - Super. Onmeasure (Widthmeasurespec, expandspec); - } -}
Layout in XML:
<com.xxx.MyGridView android:id= "@+id/mygridview" android:layout_width= "Fill_ Parent " android:layout_height=" Wrap_content " android:gravity=" center " Android:horizontalspacing= "5DP" android:numcolumns= "4" Android:stretchmode = "ColumnWidth" android:verticalspacing
This scenario is pro-test effective.
Method two: By calculating the height and display of the child column in the ListView or GridView:
Public voidSetlistviewheightbasedonchildren (ListView listview) {ListAdapter ListAdapter=Listview.getadapter (); if(ListAdapter = =NULL) { return; } intTotalheight = 0; for(inti = 0; I < Listadapter.getcount (); i++) {View ListItem= Listadapter.getview (I,NULL, ListView); Listitem.measure (0, 0); Totalheight+=listitem.getmeasuredheight (); } viewgroup.layoutparams params=Listview.getlayoutparams (); Params.height= Totalheight + (listview.getdividerheight () * (Listadapter.getcount ()-1)); ((marginlayoutparams) params). SetMargins (10, 10, 10, 10); Listview.setlayoutparams (params); }
This scenario, the test too can achieve the effect.