ScrollView中嵌套ListView控件,數據沒法顯示徹底

  1. public void setListViewHeightBasedOnChildren(ListView listView) {   ide

  2.         // 獲取ListView對應的Adapter   spa

  3.         ListAdapter listAdapter = listView.getAdapter();   get

  4.         if (listAdapter == null) {   sed

  5.             return;   List

  6.         }   統計

  7.    

  8.         int totalHeight = 0;   數據

  9.         for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   vi

  10.             // listAdapter.getCount()返回數據項的數目   return

  11.             View listItem = listAdapter.getView(i, null, listView);   void

  12.             // 計算子項View 的寬高   

  13.             listItem.measure(00);    

  14.             // 統計全部子項的總高度   

  15.             totalHeight += listItem.getMeasuredHeight();    

  16.         }   

  17.    

  18.         ViewGroup.LayoutParams params = listView.getLayoutParams();   

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

  20.         // listView.getDividerHeight()獲取子項間分隔符佔用的高度   

  21.         // params.height最後獲得整個ListView完整顯示須要的高度   

  22.         listView.setLayoutParams(params);   

  23.     }   

相關文章
相關標籤/搜索