introduction
In Android development, we sometimes need to use the requirement of nesting ListView in ScrollView. For example: There is also a part of the information display area on the ListView that displays information, and this part of the information display area is required to slide when the ListView has a lot of information, rather than fixed. At this time, we usually nest ScrollView and ListView together. But this will cause ListView to be incomplete and then there will be sliding conflict issues. Let's see how to solve it.
Solution
Actively calculate and set the height of the ListView so that the height of the ListView can be displayed reasonably. The code is as follows:
public void setListViewHeightBasedOnChildren(ListView listView) { // Get the Adapter corresponding to ListView ListAdapter listAdapter = (); if (listAdapter == null) { return; } int totalHeight = 0; // ()Return the number of data items for (int i = 0; i < (); i++) { View listItem = (i, null, listView); // Calculate the width and height of the child item View (0, 0); // Statistics the total height of all children totalHeight += (); } params = (); = totalHeight + (() * (() - 1)); // () Get the height occupied by the separator between children // Finally get the height required for the complete display of the entire ListView (params); }
The above article ScrollView nesting ListView and ListView nesting height calculation method is all the content I share with you. I hope you can give you a reference and I hope you can support me more.