Nested ListView in ScrollView

Source: Internet
Author: User

Put another listview in one ListView, or put another listview in each ListItem of a ListView. found that the small ListView will show incomplete, its height is always a problem, by default, Android is forbidden to put another scrollview in the ScrollView, its height is not calculated.

After the experiment found that can solve the problem, the idea is to set the adapter of the ListView, according to the ListView sub-project to recalculate the height of the ListView, and then set the height again as layoutparams to the ListView, So it is the right height, the following is the source:


[HTML]View PlainCopyPrint?
  1. public  class utility {  
  2.     public static void  Setlistviewheightbasedonchildren (Listview listview)  {  
  3.         listadapter listadapter  =  listview .getadapter ();   
  4.         if  (listadapter  == null)  {   
  5. Pre-condition
  6. Return
  7. }
  8. int totalheight = 0;
  9. for (intI = 0; I< Listadapter.getcount ();  i++)  {  
  10.             view < span class= "attribute" style= "margin:0px; padding:0px; Border:none; color:red; Background-color:inherit ">listitem &NBSP;=&NBSP; listadapter .getview (i, null,  ListView);   
  11.              Listitem.measure (0, 0);   
  12. Totalheight + = Listitem.getmeasuredheight ();
  13. }
  14. &NBSP;&NBSP;
  15.         viewgroup.layoutparams params &NBSP;=&NBSP; listview .getlayoutparams ();  
  16.         params.height &NBSP;=&NBSP; totalheight  +  ( Listview.getdividerheight ()  *  (Listadapter.getcount ()  - 1))   
  17. Listview.setlayoutparams (params);
  18. }
  19. }

This static method is called after the adapter of the ListView is set so that the ListView is displayed correctly in the ListItem of its parent listview. Note, however, that each item of the child ListView must be linearlayout, not the other, because other layout (such as relativelayout) does not rewrite onmeasure (), so it will be in the Onmeasure () Throws an exception.

Another problem with nesting a ListView (or ScrollView) in ScrollView is that the child ScrollView cannot slide (if it is not shown), because the sliding event is eaten by the parent scrollview. If you want to let the sub-ScrollView can also slide, can only force interception of sliding events, there are cows in the forum sent code to say yes. Although I have not tried it myself, I think it is feasible.

While the technical challenges of ScrollView can be compromised in ScrollView, this design is a very poor user experience because it is not easy for users to see and manipulate the content in sub-scrollview. For example, a good design is that each item of the parent ListView only displays a general description, and then clicking on its item will go to another page to describe and display the item in detail.

Reference: Http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing

Nested ListView in ScrollView

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.