scrollview嵌套listview时 动态设置listview高度


public class GetListViewHeight {

   public static void setListViewHeightBasedOnChildren(ListView listView) { 

       ListAdapter listAdapter = listView.getAdapter();  

       if (listAdapter == null) { 

           // pre-condition 

           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); 

   } 

}


调用的时候

new GetListViewHeight().setListViewHeightBasedOnChildren(ListView_Name);

你可能感兴趣的:(ListView,嵌套,scrollview)