亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ScrollView與ListView合用(正確計算Listview的高度)的問題解決

 更新時間:2013年05月22日 15:14:13   作者:  
最近做項目中用到ScrollView和ListView一起使用的問題,顯示的時候ListView不能完全正確的顯示,查了好多資料終于成功解決:

首先,ListView不能直接用,要自定義一個,然后重寫onMeasure()方法:

復制代碼 代碼如下:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 
            MeasureSpec.AT_MOST); 
    super.onMeasure(widthMeasureSpec, expandSpec); 

第二步:寫個計算listView每個Item的方法:

復制代碼 代碼如下:

public void setListViewHeightBasedOnChildren(ListView listView) {

  // 獲取ListView對應的Adapter

  ListAdapter listAdapter = listView.getAdapter();

  if (listAdapter == null) {

   return;

  }

  int totalHeight = 0;

  for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回數(shù)據(jù)項的數(shù)目

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

   listItem.measure(0, 0); // 計算子項View 的寬高

   totalHeight += listItem.getMeasuredHeight(); // 統(tǒng)計所有子項的總高度

  }

  ViewGroup.LayoutParams params = listView.getLayoutParams();

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

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

  // params.height最后得到整個ListView完整顯示需要的高度

  listView.setLayoutParams(params);

 }

第三步:listview添加適配器后設(shè)置高度即可:

復制代碼 代碼如下:

listView.setAdapter(adapter); 
new ListViewUtil().setListViewHeightBasedOnChildren(listView); 

相關(guān)文章

最新評論