android 根據Item數設定ListView高度

//此方法在setAdapter以前調用ide

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
     return;
    }
    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
     View listItem = listAdapter.getView(i, null, listView);
     listItem.measure(0, 0);
     totalHeight += listItem.getMeasuredHeight();
    }
  
    ViewGroup.LayoutParams params = listView.getLayoutParams();
   
    params.height = totalHeight
      + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
 }get

相關文章
相關標籤/搜索