问题描述:
ListView加入到ScrollView中之后,发现只能显示其中一条,具体原因得看一下源代码。现在先贴一下方案
(转自:http://blog.csdn.net/hitlion2008/article/details/6737459)
Example:
public class ViewsUtils {
public static void setListViewHeightBasedOnChildren(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();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
如何使用?
设置完Adapter之后调用方法重新计算
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(new TestAdapter(this,names));
// 使用ViewsUtils重新计算每一个Item的高度,然后重新setListView的LayoutParams
ViewsUtils.setListViewHeightBasedOnChildren(listView);
解决ScrollView中的ListView无法显示全,布布扣,bubuko.com
原文:http://www.cnblogs.com/hellenism/p/3719738.html