在ScrollView中嵌套ListView空间,无法正确的计算ListView的大小,故可以通过代码,根据当前的ListView的列表项计算列表的尺寸。
实现代码如下:
<pre name="code" class="html">private void setListHeightByChild(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if(listAdapter==null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View view = listAdapter.getView(i, null, listView);
view.measure(0,0);
totalHeight += view.getMeasuredHeight();
}
//设置控件的高度
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight+(listView.getDividerHeight()*(listAdapter.getCount()-1));
params.height+=5;
listView.setLayoutParams(params);
}
不需要硬着算,只需要自定义ListView,重写下OnMeasured方法就好了,受教了,谢谢
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, mExpandSpec);
}
//解决的其他方法
转载:http://bbs.anzhuo.cn/thread-982250-1-1.html