Nested ListView in ScrollView, scrollviewlistview

Source: Internet
Author: User

Nested ListView in ScrollView, scrollviewlistview

As a group buying product similar to Meituan, The scrollview is embedded with the listview. If the listview is directly embedded in the scrollview, the height of the listview is fixed and cannot be slide. By default, Android prohibits the placement of another ScrollView in ScrollView, and its height cannot be calculated. As a result, the height of the listview cannot be determined, so the code can only be set dynamically in the program as follows:

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 );
}
}

You only need to call this static method after setting the ListView Adapter so that the ListView is correctly displayed in the ListItem of its parent ListView. However, it should be noted that each Item in the sublistview must be LinearLayout, but not others, because other Layout (such as RelativeLayout) does not overwrite onMeasure (), so it will be in onMeasure () throw an exception.

In this way, the nested Complex layout of scrollview and listview can be implemented.



ScrollView nested listview for android

The listItem method can only be used to display static data, that is, the data is written to death.
To Display Dynamic Data, you must customize a ListView and write a class to inherit Linearlayout.

Nested Listview in Android ScrollView, a large amount of Listview data

There is a problem with the design. android cannot slide on both sides. Second, is it because the data you load has images ?? If yes, it can be asynchronously loaded. The most common method is to load the first 20 data records by page.

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.