我需要在運行時獲得準確的listView高度。如何在運行時獲得準確的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();
你有沒有遇到這個問題?
什麼是在'ScrollView'使用'ListView'如果你要顯示它的點完成與其所有的孩子一起? – Luksprog
如果您有3個ListView,每個listview有30個項目,您如何看待所有列表?相信我這是一個很大的方式來找到listview的高度,也有助於惠特問題,我之前提到 –