Android:如何测量 ListView 的总高度 [英] Android: How to measure total height of ListView

查看:37
本文介绍了Android:如何测量 ListView 的总高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测量 ListView 的总高度,但似乎我经常得到错误的值.我正在使用此代码:

I need to measure total height of the ListView but it seems I'm constantly getting wrong values. I'm using this code:

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

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
            MeasureSpec.AT_MOST);
    Log.w("DESIRED WIDTH", String.valueOf(listAdapter.getCount()));
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
        Log.w("HEIGHT"+i, String.valueOf(totalHeight));
    }

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

问题是我得到的高度比我应该得到的要大.每个列表项的测量方式都不同,并且所有项都具有相同的高度,因此绝对不应该这样做.每个元素的高度至少是其实际高度的两倍.

Problem is that I'm receiving larger height than I should receive. Every list item is measured differently and all items have the same height so it definitely shouldn't do that. Height of each element is at least double of it's real height.

提前致谢.

编辑 1:

我有两个 ListView,它们都有包含 6-7 个 EditText 字段的项目.如果用户想在 EditText 中写入/删除值,我会显示/隐藏 ListView.一切都很好,除非我想显示列表,它需要很多空间,因为方法计算 ListView 需要很多空间.因此,我在 ListView 下方有很多空白空间.就像需要的空间多出三倍.

I have two ListViews and they both have items with 6-7 EditText fields. I show/hide ListView if user wants to write/delete values in EditText. Everything works well except when I want to show list, it takes a lot of space because method calculated ListView needs a lot of space. Because of that I have a lot of empty space below that ListView. Like three times more empty space that needed.

推荐答案

我终于搞定了!这是测量 ListView 高度并将 ListView 设置为全尺寸的工作代码:

Finally I've done it! This is the working code which measures ListView height and sets ListView in full size:

public static void getTotalHeightofListView(ListView listView) {

    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
        View mView = mAdapter.getView(i, null, listView);

        mView.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),

                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

        totalHeight += mView.getMeasuredHeight();
        Log.w("HEIGHT" + i, String.valueOf(totalHeight));

    }

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

}

这篇关于Android:如何测量 ListView 的总高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆