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.