Nested ListView in Android ScrollView

Source: Internet
Author: User

Because to make a similar group of buy products. There is also an embedded ListView in the ScrollView, if the ListView is directly embedded into the ScrollView. The height of the listview is fixed and cannot be slid. By default, Android is forbidden to put additional scrollview in ScrollView, and its height cannot be calculated. This causes the listview height in the inside to be indeterminate, so only the code can be dynamically set up in the program such as the following:

public class Utility {public        static void Setlistviewheightbasedonchildren (ListView listview) {            ListAdapter ListAdapter = Listview.getadapter ();             if (ListAdapter = = null) {                //pre-condition                return;            }            int totalheight = 0;            for (int i = 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));            Listview.setlayoutparams (params);        }    }


Simply calling this static method after setting the adapter of the ListView allows the ListView to display correctly in the ListItem of its parent listview.

However, it is important to note that each item of the child ListView must be linearlayout, not other, because other layout (such as relativelayout) does not rewrite onmeasure (), so it will be in the Onmeasure () Throws an exception.

This enables the nested complex layout of the ScrollView plus ListView to be implemented.


Nested ListView in Android 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.