SoFunction
Updated on 2025-03-08

Solve the problem of using ScrollView and ListView in combination (correctly calculate the height of ListView)


public void setListViewHeightBasedOnChildren(ListView listView) {

// Get the Adapter corresponding to ListView

  ListAdapter listAdapter = ();

  if (listAdapter == null) {

   return;

  }

  int totalHeight = 0;

for (int i = 0; i < (); i++) { // ()Return the number of data items

   View listItem = (i, null, listView);

(0, 0); // Calculate the width and height of the child item View

totalHeight += (); // Statistics the total height of all children

  }

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

 }