我需要在运行时获得准确的 listView 高度。
当我使用下面的代码时,每个 listItem 的高度都不正确。
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();
}
params.height = totalHeight + listview.getDividerHeight()* (listAdapter.getCount() -1);
listview.setLayoutParams(params);
listview.requestLayout();
当我使用 getChild 版本时,高度是准确的,但总计数是关闭的......
int total = 0;
for (int i = 0; i < listview.getChildCount(); i++) {
View childAt = listview.getChildAt(i);
if (childAt == null)
continue;
int childH = childAt.getMeasuredHeight();
total += childH;
}
int div = listview.getDividerHeight();
total += (div * (listAdapter.getCount() - 1));
if (params.height != total) {
getLogger().info("setting measured buttons list to height: " + total);
params.height = total;
listview.requestLayout();
ViewParent parent = listview.getParent();
你遇到过这个问题吗?