How to get listview height in android?

江枫思渺然 提交于 2020-01-12 14:07:08

问题


I need to find the height of android ListView with custom ListVAdapter. Each of ListView items can be of varying height. I have tried the following code which I found here:

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, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

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

But it doesn't give actual height; it gives same height for all list items. Is it possible to find ListView height with list items of varying height?


回答1:


private int getTotalHeightofListView() {

    ListAdapter LvAdapter = lv.getAdapter();
    int listviewElementsheight = 0;
    for (int i = 0; i < mAdapter.getCount(); i++) {
        View mView = mAdapter.getView(i, null, lv);
        mView.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        listviewElementsheight += mView.getMeasuredHeight();
    }
    return listviewElementsheight;
}

try this code.




回答2:


When you call measure(0, MeasureSpec.UNSPECIFIED), you're telling the view layout system to use an infinite width, so for example TextViews may just flow to a single line. Try passing the width of your list view:

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter(); 
    int totalHeight = 0;
    int listWidth = listView.getMeasuredWidth();
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(
            MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }
    // ...update list height
} 

You also need to be sure you call this after onMeasure has been called on the list view, or listView.getMeasuredWidth() will return 0.




回答3:


You can use getHeight():

System.out.println("height of list view..."+listview.getHeight());

Or follow this answer.




回答4:


Based on @jeevamuthu code.

public static int getLVHeight(ListView listView) {
    ListAdapter adapter = listView.getAdapter();
    int height = 0;
    int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        View view = adapter.getView(i, null, listView);
        view.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        height += view.getMeasuredHeight();
    }
    height += listView.getDividerHeight() * (count - 1);
    return height;
}


来源:https://stackoverflow.com/questions/13814396/how-to-get-listview-height-in-android

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