Reprint: http://blog.csdn.net/luohai859/article/details/39347583
about why only one line, the personal understanding is: If you use the GridView and the ListView, the contents are fixed, the underlying system is easy to calculate the control to occupy the width of the high, when the outside nest layer of scrollview, can do up and down or left and right sliding, But you can slide how many systems do not know, then you need to calculate a sliding area for the ScrollView. This value is the sum of the total width and height of the contents of the GridView and ListView. Have a more accurate understanding welcome to enlighten. There are two ways to solve this problem:
Method One: That's what it says. By calculating the height and display of the child columns 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.layoutparamsparams=Listview.getlayoutparams (); params. Height = totalheight + (listview.getdividerheight () * (Listadapter.getcount ()-1)); ((Marginlayoutparams)params). SetMargins ( the, the, the, the); Listview.setlayoutparams (params); }
Method Two: Rewrite the GridView and ListView Onmeasure method, directly giving it a sufficiently large height:
Rewrite the ListView:
Public classMylistview extends ListView { PublicMylistview (Context context) {//TODO auto-generated Method Stubsuper (context); } PublicMylistview (Context context, AttributeSet attrs) {//TODO auto-generated Method StubSuper (context, attrs); } PublicMylistview (context context, AttributeSet attrs,intDefstyle) { //TODO auto-generated Method StubSuper (context, attrs, Defstyle); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { //TODO auto-generated Method Stub intExpandspec = Measurespec.makemeasurespec (Integer.max_value >>2, Measurespec.at_most); Super.onmeasure (Widthmeasurespec, Expandspec); } }
Overriding the GridView:
Public classMyGridView extends gridview{ PublicMyGridView (Context context, AttributeSet Attrs) {Super (context, attrs); } PublicMyGridView (Context context) {super (context); } PublicMyGridView (context context, AttributeSet attrs,intDefstyle) {Super (context, attrs, Defstyle); } @Override Public voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { intExpandspec = Measurespec.makemeasurespec (Integer.max_value >>2, Measurespec.at_most); Super.onmeasure (Widthmeasurespec, Expandspec); } }
ScrollView nested Gridview,listview Show only one row solution