一个现在有点想法的IT民工
分类: Android平台
2014-10-26 01:03:27
但是实际情况却不是这样,而是重复了数次,很明显这是异常现象,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的调用次数问题,还涉及到布局的效率。