Android Combat tips: How to nest a ListView in ScrollView

Source: Internet
Author: User

A few days ago because of the needs of the project, to put another ListView in one ListView, you put another listview in each ListItem of a ListView. But in the beginning, you will find that the small ListView is not fully displayed, and its height is always problematic. Check the Internet, found that others have encountered such problems, and most people do not recommend such a design, because by default, Android is forbidden to put another scrollview in the ScrollView, its height is not calculated.

Search again, found that there are stackoverflow on the cow has solved the problem, after the experiment found that can solve the problem, its idea is to set up the ListView adapter, according to the ListView sub-project to recalculate the height of the ListView, Then set the height again as layoutparams to the ListView, so that its height is correct, 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 (int i = 0; I < listadapter.getcount (); i++) {
  10. View ListItem = Listadapter.getview (i, NULL, ListView);
  11. Listitem.measure (0, 0);
  12. Totalheight + = Listitem.getmeasuredheight ();
  13. }
  14. Viewgroup.layoutparams params = listview.getlayoutparams ();
  15. params.height = totalheight + (Listview.getdividerheight () * (Listadapter.getcount ()-1));
  16. Listview.setlayoutparams (params);
  17. }
  18. }

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

Android Combat tips: How to nest a 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.