android 计算listview的高度

来源:互联网 发布:虚拟定位是什么软件 编辑:程序博客网 时间:2024/05/20 02:28
   1.定义:
int totalHeight = 0;
2.使用
// 获取ListView对应的AdapterListAdapter listAdapter = comment_list.getAdapter();for (int i = 0, len = listAdapter.getCount(); i < len; i++) {    // listAdapter.getCount()返回数据项的数目    View listItem = listAdapter.getView(i, null, comment_list);    // 计算子项View 的宽高    listItem.measure(0, 0);    // 统计所有子项的总高度    totalHeight += listItem.getMeasuredHeight();}ViewGroup.LayoutParams params_comment = comment_list.getLayoutParams();params_comment.height = totalHeight+ (comment_list.getDividerHeight() * (listAdapter.getCount() - 1));// listView.getDividerHeight()获取子项间分隔符占用的高度// params.height最后得到整个ListView完整显示需要的高度comment_list.setLayoutParams(params_comment);
3.注意:要是重新获取数据的话
一定要把
totalHeight = 0;
这样的就不会出现空白的状况。
0 0
原创粉丝点击