写listview优化的时候,发现Listview初次创建的时候会多次执行getView方法。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hang.myapplication.MainActivity">
<ListView
android:id="@+id/list_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>

这是listview的布局,执行后结果为:

06-30 20:04:14.094 1166-1166/com.example.hang.myapplication D/hello: convertView为空0
06-30 20:04:14.104 1166-1166/com.example.hang.myapplication D/hello: convertView为空1
06-30 20:04:14.104 1166-1166/com.example.hang.myapplication D/hello: convertView为空2
06-30 20:04:14.114 1166-1166/com.example.hang.myapplication D/hello: convertView为空3
06-30 20:15:31.944 13339-13339/com.example.hang.myapplication D/hello: convertView为空0
06-30 20:15:31.944 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:15:31.984 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:15:31.994 13339-13339/com.example.hang.myapplication D/hello: convertView为空1
06-30 20:15:31.994 13339-13339/com.example.hang.myapplication D/hello: convertView为空2
06-30 20:15:31.994 13339-13339/com.example.hang.myapplication D/hello: convertView为空3
06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView为空0
06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:15:32.124 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3

这显然不正常,执行了多次getview方法。经过群友的提示修改为fill_partent后结果显示正常。

06-30 20:04:14.094 1166-1166/com.example.hang.myapplication D/hello: convertView为空0
06-30 20:04:14.104 1166-1166/com.example.hang.myapplication D/hello: convertView为空1
06-30 20:04:14.104 1166-1166/com.example.hang.myapplication D/hello: convertView为空2
06-30 20:04:14.114 1166-1166/com.example.hang.myapplication D/hello: convertView为空3

后来联想到UI的优化,恍然大悟~

  RelativeLayouts经常需要measure所有子节点两次才能把子节点合理的布局。如果子节点设置了weights属性,LinearLayouts也需要measure这些节点两次,才能获得精确的展示尺寸。

  在UI布局优化中,推荐扁平式布局。也是因为view开始被measure时,该view所有的子view都会被重新layout,再把该view传递给它的父view,如此重复一直到最顶部的根view。layout完成之后,所有的view都被渲染到屏幕上,并不是只有用户看得见的view才会被渲染,所有的view都会。这样一个view会被多次measure。getiew也执行了多次。

后来我嵌套了一层RelativeLayout,执行后发现getview执行次数比不嵌套多了一次~,嵌套linearlayout是没有任何变化的

06-30 20:25:18.314 13339-13339/com.example.hang.myapplication D/hello: convertView为空0
06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:25:18.334 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:25:18.334 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:25:18.334 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:25:18.334 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:25:18.334 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:25:18.344 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:25:18.344 13339-13339/com.example.hang.myapplication D/hello: convertView为空1
06-30 20:25:18.354 13339-13339/com.example.hang.myapplication D/hello: convertView为空2
06-30 20:25:18.354 13339-13339/com.example.hang.myapplication D/hello: convertView为空3
06-30 20:25:18.404 13339-13339/com.example.hang.myapplication D/hello: convertView为空0
06-30 20:25:18.404 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:25:18.404 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3

  解决方案,设置为fill_partent或者设置为固定的数值,优化UI布局。这个问题也可能会导致加载图片等错位。

Android LIstView初次创建getview方法执行多次问题的更多相关文章

  1. [Android]ListView的Adapter.getView()方法中延迟加载图片的优化

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4139998.html 举个例子吧,以好友列表为例 ListVi ...

  2. [Android]在Adapter的getView方法中绑定OnClickListener比较好的方法

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4146512.html  给ListView中每个item绑定点 ...

  3. Android子线程创建Handler方法

    如果我们想在子线程上创建Handler,通过直接new的出来是会报异常的比如: new Thread(new Runnable() { public void run() { Handler hand ...

  4. ListView / GirdView Adpater的getView方法,首项多次调用

    通过Adapter为AbslistView提供内容是一个常见的做法:在ListView或者GridView的Adapter中的getView()方法中,加入一行日志,看getView()被调用的情况 ...

  5. 自定义adapter 的getView方法被重复执行了n次的解决方法

    1. getView执行的次数和你的getCount没有直接的关系   ,getCount和你listView里面的条目数量(行数量)有关系 ,getView方法执行次数取决于你屏幕上显示几个条目,比 ...

  6. Android ListView的优化

    最近的项目中有通讯录这个模块,里面的通讯录涉及的联系人数量很大,导致在加载页面的时候有点卡,所以就必须得进行优化,优化的最终实现理论是什么?就是让ListView一次性加载的数据较少,后续根据用户操作 ...

  7. android Listview 软引用SoftReference异步加载图片

    首先说一下,android系统加载大量图片系统内存溢出的3中解决方法: (1)从网络或本地加载图片的时候,只加载缩略图.这个方法的确能够少占用不少内存,可是它的致命的缺点就是,因为加载的是缩略图,所以 ...

  8. android ListView 多次调用 getView方法

    <ListView            android:layout_width="match_parent"            android:layout_heig ...

  9. Android ListView getView()方法重复调用导致position错位

    问题现状:Android ListView getView()方法重复调用导致position错位 解决办法:把ListView布局文件的layout_height属性改为fill_parent或者m ...

随机推荐

  1. unity, setting standard shader by script

    http://forum.unity3d.com/threads/change-standard-shader-render-mode-in-runtime.318815/

  2. org.hibernate.MappingException: duplicate import异常

    在开发hibernate时,一起多谢ORM类和映射文件时,报出:org.hibernate.MappingException: duplicate import com.XXX异常 解决方案: 检查你 ...

  3. 跨平台移动开发工具:PhoneGap与Titanium全方位比拼

    PhoneGap和Appcelerator Titanium,对于封装和配置移动应用程序而言,二者都是非常受欢迎的开源JavaScript框架.本文为Appcelerator开发者Kevin Whin ...

  4. Informatica9.6.1在Linux Red Hat 5.8上安装遇到的有关问题整理_3

    3.Repository Service启动后的页面编码问题 1)错误信息: 2)原因分析及解决步骤 原因分析: informatica产品安装背后AdminConsole的Code page默认为U ...

  5. [再寄小读者之数学篇](2014-11-24 Abel 定理)

    设幂级数 $\dps{g(x)=\sum_{n=0}^\infty a_nx^n}$ 在 $|x|<1$ 内收敛, 且 $\dps{\sum_{n=0}^\infty a_n=s}$ 收敛. 则 ...

  6. strus2中获取表单数据 两种方式 属性驱动 和模型驱动

    strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值 * 如果一个属性在对象栈,在页面 ...

  7. Folding

    题意: 给定一个串,求能化成的最短循环节串(把重复字符串转化成循环节形式) 分析: 不是太好想,如果让求最短长度还好,dp[i][j],表示区间[i,j]化成的最小长度,dp[i][j]=min(dp ...

  8. 网页元素定位神器之Xpath详解

    摘要: 经常在工作中会使用到XPath的相关知识,但每次总会在一些关键的地方不记得或不太清楚,所以免不了每次总要查一些零碎的知识,感觉即很烦又浪费时间,所以对XPath归纳及总结一下. ...     ...

  9. Linux下安装loadrunner步骤及遇到的问题

    Linux下实现压力测试,只能在Linux下安装加压器---load-generator,然后通过本地录制脚本,通过loadrunner controller 的Scenario-->Load ...

  10. ISO-8859-1

    ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII一致,0x80-0x9F之间是控制字符,0xA0-0xFF之间是文字符号 ...