如何设置ListView高度
问题描述:
我想知道如何通过计算项目视图来设置ListView
的高度,有人这样说。如何设置ListView高度
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();
}
但如果我投入ListView
基于BaseAdapter
一类?
答
BaseAdapter仅实现ListAdapter适配器接口,所以没有理由说明您的代码不适用于BaseAdapter。你可以试试这个:
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();
}
是你想把它放在listview中的数据static吗?或动态? – 2013-03-10 14:41:36
dynamic!然后我将使用“adapter.notifyDataSetChanged();”来更新适配器 – user1659372 2013-03-10 15:02:36
,以便在动态的情况下,您是否使用特定结构保存数据,比如'hashmap','' arraylist'或者让你的适配器从外部来源读取数据,例如'server connection'或'database'? – 2013-03-10 15:09:02