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

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

问题描述

我需要测量的ListView的总高度,但似乎我经常收到错误的价值观。我使用这个code:

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.

感谢你在前进。

EDIT1:

我有两个列表视图,它们均与6-7的EditText领域的项目。我显示/隐藏的ListView,如果用户想要写/在EditText上删除值。一切运作良好,除非我想告诉列表,它需要大量的空间,因为计算方法的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.

推荐答案

最后,我已经做到了!这是工作code衡量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天全站免登陆