如何计算的ListView共行高在android系统? [英] How to calculate total row heights of listView in android?

查看:127
本文介绍了如何计算的ListView共行高在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code得到的ListView行项目的总高度,但它没有返回实际高度。下面是使用code

 公共静态无效setListViewHeightBasedOnChildren(ListView控件ListView控件){
        ListAdapter listAdapter = listView.getAdapter();
        如果(listAdapter == NULL){
            // pre-条件
            返回;
        }        INT totalHeight = 0;
        的for(int i = 0; I< listAdapter.getCount();我++){
            查看的listItem = listAdapter.getView(I,空,ListView控件);
            listItem.measure(0,0);
            totalHeight + = listItem.getMeasuredHeight();
        }        ViewGroup.LayoutParams PARAMS = listView.getLayoutParams();
        params.height = totalHeight +(listView.getDividerHeight()*(listAdapter.getCount() - 1));
        listView.setLayoutParams(PARAMS);
        listView.requestLayout();
    }

例如:我有20行的ListView和各行高度不同,每个人的假设200300500。当我使用这个上面code,它并没有为我返回实际高度。另外我想这样的回答:<一href=\"http://stackoverflow.com/questions/15039913/android-how-to-measure-total-height-of-listview\">Android:如何衡量的ListView 总高度
但没有作品。我怎样才能摆脱这个问题。谁能解释这个解决方案?


解决方案

 查看的listItem = listAdapter.getView(I,空,ListView控件);
listItem.measure(0,0);
totalHeight + = listItem.getMeasuredHeight();

功能的核心是这三行,它试图测量每个视图。 0在listItem.measure(0,0)等于 MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED)

主要是它会计算列表视图的准确高度。有一个例外,当视图内容实在是太多了,将包线,即有文本的许多行。在这种情况下,你应该指定一个准确的widthSpec来衡量()。因此,改变 listItem.measure(0,0)

  //试图给列表视图的宽度估计
INT listViewWidth =屏幕宽度 - leftPadding - rightPadding;
INT widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth,MeasureSpec.AT_MOST);
listItem.measure(listViewWidth,0)

更新关于这里的公式

  INT listViewWidth =屏幕宽度 -  leftPadding  -  rightPadding;

这只是一个例子来说明如何估算的ListView的宽度的宽度,计算公式是基于一个事实,即屏幕的列表视图≈宽度的宽度。填充由您自行设置的,也许在这里0。 此页面讲述如何让屏幕宽度。
一般情况下,它只是一个样本,你可以在这里写你自己的公式。

I used this code to get the total heights of listview row items but it did not returns the actual height. Here is the used code

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

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

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

For example: I have a listview with 20 rows and each rows height is different from each others suppose that 200,300,500. When I use this above code, it did not returns actual height for me. Also I tried this answer : Android: How to measure total height of ListView But did not works. How can I get rid of this problem. Can anyone explain this solution??

解决方案

View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();

The core of the function is these three lines, it try to measure each view. The 0 in listItem.measure(0, 0) is equals to MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)

Mostly it will calculate the accurate height of the listview. There is one exception, when the view content is too much and will wrap line, i.e. there are to many lines of text. In such situation, you should specified a accurate widthSpec to measure(). So change listItem.measure(0, 0) to

// try to give a estimated width of listview
int listViewWidth = screenWidth - leftPadding - rightPadding; 
int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth, MeasureSpec.AT_MOST);
listItem.measure(listViewWidth, 0)

UPDATE about the formula here

int listViewWidth = screenWidth - leftPadding - rightPadding; 

It's just an example to show how you can estimate the width of the width of listview, the formula is based on the fact that width of listview ≈ width of screen. The padding is set by your self, maybe 0 here. This page tells how to get screen width. In general, it's just a sample, and you can write your own formula here.

这篇关于如何计算的ListView共行高在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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