Nested Gridview,listview in Android ScrollView show only one line solution

Source: Internet
Author: User

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.

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.