Android ListView反复调用getView和getCount
来源:互联网 发布:用ansys有限元软件 编辑:程序博客网 时间:2024/05/16 09:24
最近做项目发现一个界面当有ListView是,getView和getCount中的log被疯狂调用。一个5个Item的ListView,getView竟然会被反复调用7组。尤其是当ItemView中需要加载图片时,很容易造成GC过多,很容易出现ANR。
原因就在于measure过程,ListView一般都会有好多个Item,而且也会同时显示若干组Item,这些Item的父元素都是这个ListView。
更具Google的解释,View在Draw的时候分成两个阶段:measure和layout,在measure阶段时主要就是为了计算两个参数:height和width。而且要注意的是,这是个递归的过程,从顶向下,DecorView开始依次调用自己子元素的measure。计算完成这两个参数后就开始layout,最后再是draw的调用。
对于ListView,当然每一个Item都会被调用measure方法,而在这个过程中getView和getCount会被调用,而且看用户的需求,可能会有很多次调用。
而为什么会有很多组次调用呢?
问题就在于在layout中的决定ListView或者它的父元素的height和width属性的定义了。fill_parent会好一点,计算方法会比较简单,只要跟父元素的大小相似就行,但是即使是fill_parent,也不能给View当饭吃,还是要计算出来具体的dip,所以measure还是会被调用,只是可能比wrap_content的少一点。至于自适应的它会一直考量它的宽和高,根据内容(也就是它的子Item)计算宽高。可能这个measure过程会反复执行,如果父元素也是wrap_content,这个过程会更加漫长。
所以,解决方法就是尽量避免自适应,除非是万不得已,固定大小或者填充的效果会比较好一些。
- Android ListView反复调用getView和getCount
- Android ListView反复调用getView和getCount
- Android ListView反复调用getView和getCount
- Android ListView反复调用getView和getCount
- Android ListView反复调用getView和getCount
- Android之ListView超过正常次数反复调用getView和getCount问题
- Android AbsListView子类反复调用getView()和getCount()问题分析
- Android中的GridView反复调用getView和getCount,并且getView中的position的值几乎都是0
- Android中的GridView反复调用getView和getCount,并且getView中的position的值几乎都是0
- getview与getcount被反复调用
- Listview的getView调用比getCount少
- AdapterView(Gallery、ListView、GridView) 调用了getCount 方法但是没有调用getView方法
- 关于ListView 中getCount有值getView不调用的问题
- GridView中 反复调用getView
- android ListView 和Adapter 结合,但是不调用getView()方法??
- Android中ListView多次调用getView
- Android ListView 重复调用getView问题
- Android ListView getView疯狂调用的解决办法
- C# 判断文件是否为空
- Activity中启动其他包里的Activity摘要
- Linux忘记密码解决方案 <grub篇>
- 写入Word的自定义属性(转)
- 西蒙iphone-OpenGL ES 教程-01
- Android ListView反复调用getView和getCount
- Spring单例Bean和线程安全
- 吸血鬼教授vs狼人工程师
- atemega8例程:灯闪烁
- Java 多线程的Thread类和Runnable接口
- java 访问数据库
- 初识ubuntu-尝试配置安全可靠的web服务器
- Vim Functions
- 西蒙iphone-OpenGL ES 教程-02