Set ListView Height Programmatically

前端 未结 3 881
小鲜肉
小鲜肉 2021-01-12 17:01

I have a TextView and ListView in my activity.ListView intially set to Visibility = gone I need a functionality like if u

3条回答
  •  误落风尘
    2021-01-12 17:49

    If you want to add ListView on ScrollView and adjust the height of ListView try this code

    if (adapter != null) {
        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
             View listItem = adapter.getView(i, null, listView);
             listItem.measure(0, 0);
             totalHeight += listItem.getMeasuredHeight() + (listItem.getMeasuredHeightAndState()/2);
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    

提交回复
热议问题