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

泡在网上的日子 / 文 发表于2013-09-19 10:33 次阅读 listview,getview,布局

我们知道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的调用次数问题,还涉及到布局的效率。

收藏 赞 (5) 踩 (5)
上一篇:安卓中webview与javascript的交互
当我们要实现丰富的图文混排效果的时候,我们一般会使用webview,这是一个功能十分强大的的控件,来看看官方的解释: A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some onlin
下一篇:用双缓存技术优化listview异步加载网络图片
缓存简单的来讲就是内存,缓存技术的目的是为了更高效的利用内存,防止out of memory 发生。双缓存又称为二级缓存,它的实现利用了java中的强引用(直接对对象的引用都是强引用)和弱引用(SoftReference)各自的同特点。 在这一篇博客中将会为大家讲解如何