How to force height of listView to be size so that every row is visible?

我是研究僧i 提交于 2019-12-06 06:31:38

问题


I have nested listView. How to force height of listView to be size so that every row is visible ? ListView is nested inside ScrollView so scroll doesn't work for ListView so I need to make that all items are visible inside ListView which is nested ion ScrollView ( there is above and bellow more things).


回答1:


To calculate hieght of listView

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
            MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

Pass listview object to this methode.

 setListViewHeightBasedOnChildren(listView)



回答2:


you can use this code.listview contains this attribute

android:minheight="20dp";


来源:https://stackoverflow.com/questions/9484038/how-to-force-height-of-listview-to-be-size-so-that-every-row-is-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!