计算listview的高度,这样才能在scllowView里面嵌套listview不会出现高度有关问题

计算listview的高度,这样才能在scllowView里面嵌套listview不会出现高度问题

最近做项目中用到ScrollView和ListView一起使用的问题,显示的时候ListView不能完全正确的显示,查了好多资料终于成功解决:

 

private void setPullLvHeight(ListView pull){  
        int totalHeight = 0;    
        ListAdapter adapter= pull.getAdapter();
        for (int i = 0, len = adapter.getCount(); i < len; i++) { //listAdapter.getCount()返回数据项的数目    
        View listItem = adapter.getView(i, null, pull);    
        listItem.measure(0, 0); //计算子项View 的宽高    
        totalHeight += listItem.getMeasuredHeight(); //统计所有子项的总高度    
        }    
            
        ViewGroup.LayoutParams params = pull.getLayoutParams();    
        params.height = totalHeight + (pull.getDividerHeight() * (pull.getCount() - 1));    
        pull.setLayoutParams(params);    
    }