ScrollView nested Gridview,listview Show only one row solution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.