问题
I have nested listView. How to force height of listView to be size so that every row is visible ? ListView is nested inside ScrollView so scroll doesn't work for ListView so I need to make that all items are visible inside ListView which is nested ion ScrollView ( there is above and bellow more things).
回答1:
To calculate hieght of listView
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Pass listview object to this methode.
setListViewHeightBasedOnChildren(listView)
回答2:
you can use this code.listview contains this attribute
android:minheight="20dp";
来源:https://stackoverflow.com/questions/9484038/how-to-force-height-of-listview-to-be-size-so-that-every-row-is-visible