ListView中嵌套ListView

/**

* 设置Listview的高度

*/

public void setListViewHeight(ListView listView) {

ListAdapter listAdapter = listView.getAdapter();

if(listAdapter ==null) {

return;

}

inttotalHeight =0;

for(inti =0; i < listAdapter.getCount(); i++) {

View listItem = listAdapter.getView(ia,null, listView);

listItem.measure(0,0);

totalHeight += listItem.getMeasuredHeight();

}

ViewGroup.LayoutParamsparams = listView.getLayoutParams();

params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() -1));

listView.setLayoutParams(params);

}


这个方法设置的item的Layout必须是带有onMeasure()方法的控件,否则在计算的时候会报错,建议使用LinearLayout。


第二种(自定义ListView)


@OverridepublicvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){intexpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>2,

MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expandSpec);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容