[布局效率问题]解决ListView的getview调用次数多于子view个数的问题

我们知道listview的getview调用次数是和他的子view个数相关的,执行次数取决于你屏幕上能显示几个条目,比如你有100行 ,但是你一屏只能显示5行,那么启动程序的时候 系统调用5次getView方法。虽然当你下拉的时候会不断的调用getview,但是首次加载的时候getview调用次数是你手机一屏能显示的item个数。比如我这里有一个listview,测试的手机能一次显示7个item(如图),那么正常情况下首次加载的时候log应该是这样的:

09-19 10:01:50.533: I/jianghejie(22297): getview-postion=0
09-19 10:01:50.540: I/jianghejie(22297): getview-postion=1
09-19 10:01:50.595: I/jianghejie(22297): getview-postion=2
09-19 10:01:50.603: I/jianghejie(22297): getview-postion=3
09-19 10:01:50.619: I/jianghejie(22297): getview-postion=4
09-19 10:01:50.626: I/jianghejie(22297): getview-postion=5
09-19 10:01:50.642: I/jianghejie(22297): getview-postion=6

但是实际情况却不是这样,而是重复了数次,很明显这是异常现象,log如下:

09-19 10:21:35.251: I/jianghejie(23718): getview-postion=0
09-19 10:21:35.259: I/jianghejie(23718): getview-postion=1
09-19 10:21:35.259: I/jianghejie(23718): getview-postion=2
09-19 10:21:35.267: I/jianghejie(23718): getview-postion=3
09-19 10:21:35.275: I/jianghejie(23718): getview-postion=4
09-19 10:21:35.275: I/jianghejie(23718): getview-postion=5
09-19 10:21:35.283: I/jianghejie(23718): getview-postion=6
09-19 10:21:35.290: I/jianghejie(23718): getview-postion=0
09-19 10:21:35.290: I/jianghejie(23718): getview-postion=1
09-19 10:21:35.298: I/jianghejie(23718): getview-postion=2
09-19 10:21:35.298: I/jianghejie(23718): getview-postion=3
09-19 10:21:35.306: I/jianghejie(23718): getview-postion=4
09-19 10:21:35.314: I/jianghejie(23718): getview-postion=5
09-19 10:21:35.314: I/jianghejie(23718): getview-postion=6
09-19 10:21:35.431: I/jianghejie(23718): getview-postion=0
09-19 10:21:35.478: I/jianghejie(23718): getview-postion=1
09-19 10:21:35.486: I/jianghejie(23718): getview-postion=2
09-19 10:21:35.494: I/jianghejie(23718): getview-postion=3
09-19 10:21:35.494: I/jianghejie(23718): getview-postion=4
09-19 10:21:35.501: I/jianghejie(23718): getview-postion=5
09-19 10:21:35.509: I/jianghejie(23718): getview-postion=6
09-19 10:21:35.556: I/jianghejie(23718): getview-postion=0
09-19 10:21:35.564: I/jianghejie(23718): getview-postion=1
09-19 10:21:35.564: I/jianghejie(23718): getview-postion=2
09-19 10:21:35.572: I/jianghejie(23718): getview-postion=3
09-19 10:21:35.572: I/jianghejie(23718): getview-postion=4
09-19 10:21:35.580: I/jianghejie(23718): getview-postion=5
09-19 10:21:35.580: I/jianghejie(23718): getview-postion=6
09-19 10:21:35.587: I/jianghejie(23718): getview-postion=0
09-19 10:21:35.595: I/jianghejie(23718): getview-postion=1
09-19 10:21:35.603: I/jianghejie(23718): getview-postion=2
09-19 10:21:35.611: I/jianghejie(23718): getview-postion=3
09-19 10:21:35.619: I/jianghejie(23718): getview-postion=4
09-19 10:21:35.626: I/jianghejie(23718): getview-postion=5
09-19 10:21:35.634: I/jianghejie(23718): getview-postion=6
09-19 10:21:35.665: I/jianghejie(23718): getview-postion=0
09-19 10:21:35.673: I/jianghejie(23718): getview-postion=1
09-19 10:21:35.673: I/jianghejie(23718): getview-postion=2
09-19 10:21:35.681: I/jianghejie(23718): getview-postion=3
09-19 10:21:35.681: I/jianghejie(23718): getview-postion=4
09-19 10:21:35.681: I/jianghejie(23718): getview-postion=5
09-19 10:21:35.689: I/jianghejie(23718): getview-postion=6
09-19 10:21:35.689: I/jianghejie(23718): getview-postion=0
09-19 10:21:35.697: I/jianghejie(23718): getview-postion=1
09-19 10:21:35.697: I/jianghejie(23718): getview-postion=2
09-19 10:21:35.697: I/jianghejie(23718): getview-postion=3
09-19 10:21:35.705: I/jianghejie(23718): getview-postion=4
09-19 10:21:35.705: I/jianghejie(23718): getview-postion=5
09-19 10:21:35.712: I/jianghejie(23718): getview-postion=6
怎么会发生这样的情况呢,我仔细和之前写的一些listview对比,发现和以前的不同之处在布局上,以往我一般都是将listview的高度设置成fill_parent,而这次我是设为wrap_content,这样做的问题在于,ListView没有取到实际的高度,他还要根据计算才能确定,而每一次计算应该会触发listview的渲染,所以就会出现getview的调用次数跟正常情况相比多了好几倍。

所以在一般情况下,我建议把listiview在布局文件中的高度总是设置为:fill_parent(或者match_parent),这不仅仅是getview的调用次数问题,还涉及到布局的效率。