I want to know how to set ListView
's height through calculating the item view, someone says in this way.
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
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();
}
But what if I put into the ListView
for a class based on BaseAdapter
?
BaseAdapter only implement ListAdapter adapter interface, so there is no reason why your code shouldn't work with BaseAdapter. You can try this:
BaseAdapter listAdapter = (BaseAdapter)listView.getAdapter();
if (listAdapter == null) {
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();
}