android set listview height dynamically

前端 未结 4 1360
情话喂你
情话喂你 2021-01-02 23:39

i have ExpandableListview inside ScrollView and i know that\'s not good but i had too, the only solution to show the whole list is by set its heigh

4条回答
  •  自闭症患者
    2021-01-02 23:57

    try this, Use Child based on listview. setListViewHeightBasedOnChildren() this will set your listview child based height

     public class Utils {
    
    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);
        listView.requestLayout();
    }
    
    
    
    }
    

提交回复
热议问题