- 浏览: 3024995 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (893)
- android (110)
- iphone (198)
- java (79)
- JavaScript手册-目录 (9)
- JavaScript手册-Array (19)
- JavaScript手册-Boolean (5)
- JavaScript手册-Date (50)
- JavaScript手册-Math (30)
- JavaScript手册-Number (14)
- JavaScript手册-RegExp (7)
- JavaScript手册-String (38)
- JavaScript手册-全局函数 (8)
- JavaScript实用脚本 (7)
- Others (21)
- java-jpcap (7)
- java-thread (1)
- ibm文章 (3)
- classloader (2)
- java-filter (2)
- 运行环境 (33)
- java-正则 (2)
- oracle (1)
- linux-shell (26)
- wap (1)
- sqlite (3)
- wow (1)
- jvm (1)
- git (5)
- unity3d (29)
- iap (2)
- mysql (23)
- nginx (14)
- tomcat (9)
- apache (2)
- php (1)
- ubuntu (40)
- rsa (1)
- golang (21)
- appstore (5)
- sftp (2)
- log4j (2)
- netty (18)
- 测试工具 (6)
- memcache (5)
- 设计模式 (1)
- centos (8)
- google_iab (5)
- iOS专题 (4)
- mac (10)
- 安装配置帮助手册 (2)
- im4java_graphicsmagick (5)
- inotify-tools (1)
- erlang (6)
- 微信支付 (1)
- redis (8)
- RabbitMQ (5)
最新评论
-
heng123:
Netty视频教程https://www.douban.com ...
netty4.0.23 初学的demo -
maotou1988:
使用Netty进行Android与Server端通信实现文字发 ...
netty4.0.23 初学的demo -
码革裹尸:
非常感谢,正好用上
android 呼入电话的监听(来电监听) -
rigou:
提示的/222.177.4.242 无法链接到ip地址,是什 ...
通过 itms:services://? 在线安装ipa ,跨过app-store -
duwanbo:
GridView与数据绑定
android listview 连续调用 getview问题分析及解决
转载自:http://blog.csdn.net/f8376904110/article/details/6460934
当我们在使用listview的时候。有时候自定义adapter的时候,是不是会发现在getview里打印日志的时候,重复调用很多次?有时候 4次。有的严重甚至到10次,当我们在listview中移动的时候。每移动一列都会调用很多次,这样大大影响到效率!其实这和listview本身在 android上的机制有关。下面我开始来介绍一下吧:
在布局,我们只有一个listview的时候。那好。我们把高设置成wrap_content的时候。在listview里加载几行看看。日志在 getview里打印一下。是不是重复调用了?那这个办法就好弄了。把高设置成fill_parent就成了。这个时候发现日志还是重复调用?那就要看一 下Listview的上一级而已的高是不是也是设置也fill_parent的,如果不是。请改动吧。如果是。。。那我还真没碰到重复调用的!因为测试好 几次了!
如果我们在而已里不只一个Listview。一个复杂好看的布局可能有很多。listview在布局的某个地方。这个时候有时候运气不好。你会发现你调用 了很多次getview。我测试的时候。最高230次。。。可想而知。这个速度是相当慢。而且每移动一次就是调用这么多次!对于这样的情况,在修改布局的 时候,要考虑以下两点:1.首先考虑需求布局和性能哪个更重要一点。2.考虑listview周边哪个布局控件影响到了它!
如果在性能上没有太大影响,而需求要求必需是那样的布局。那就以布局为主。看看有没有别的方法来优化一下listview,当然前提是布局一点都不能调 整。如果能调整,布局没有太大变动。而listview又能很好的优化。那就当然优化了!当我们优化的时候。首先要看一下有没有影响到Listview重 绘的控件,比如。如果它上面和下面都有控件。而且高都是wrap_content,那么你就要设置成fill_parent或者固定高。这样 listview在高上就不会重绘,这是最主要的一点。那左右是不是也有控件(一般一个手机页面用到list的时候不会有这么多控件)?有,那我们就也要 调整,那就同高一样的设置。一定要让listview是一个固定在那个地方不动的。不然,你就等着让他重复去调用吧!
其实说了这么多。最主要的还是在我们进行布局的时候。要巧妙的运用每个控件的属性,以及了解控件每个的原理。这样在我们进行UI设计的时候,才能很好的去结合!
关于Adapter的getView方法中创建view的原理
position的确是要显示的View在你的adapter里面的位置
你自己心里有一种先入为主的东西,扰乱了你的思维。
当你在滚动屏幕的时候,并不是说你只滚了一行,就只会有一次调用getView,实际上可能出现多次调用getView的(系统完全有可能多生成几行View,以便在摸动的时候,达到平滑不滞后的效果)。
在getView里面,你只能创建View,不可有自己的与界面无关的逻辑,因为你不知道getView什么时候被调用,以什么参数被调用。
而你要创建View,position参数是必须要使用的,所以不要怀疑这个参数。
另外,position绝对不是屏幕上的位置,而是数据在adapter里面的位置,因为你创建View的时候,与它在屏幕上的位置没有关系,你只是创建,至于显示到哪里,你并没有做控制。
这个 先要理解了他的工作原理 才好分析原因。
如果你的屏幕只能显示6行,所以position 的值就是0~5。
比如滚屏造成有一行出了屏,有一行新的入了屏
这个时候 新得到的view的 position 就会又充0计算
比如有2行进入的屏幕 那么position 就是0~1了
所以打印就是 先打印0~5 然后你拖地打印0~1
总之,position绝对是一个合法的值,但你不要想当然的认为,某次getView,一定会传一个position等于多少值的给你,不一定。因为getView是回调用的,什么时候创建是由系统决定的(它很可能会提早创建,原因前面我说过了)。
比如滚屏造成有一行出了屏,有一行新的入了屏,那么,系统会将出了屏的View做为参数(注意getView的第二个参数)调用getView,期望你将
出屏的View改造成入屏的View(这只需要更新一下界面显示即可,不需要再new
View,这就是我在前面某一贴里面跟你说的了——优化),如果每次都new,是很不负责的。
当然,上面只优化策略的一种,不排除还有其它策略,这要看google怎么设计的了。
比如滚屏造成有一行出了屏,有一行新的入了屏
这个时候 新得到的view的 position 就会又充0计算
比如有2行进入的屏幕 那么position 就是0~1了
所以打印就是 先打印0~5 然后你拖地打印0~1
评论
01-02 21:57:41.484: DEBUG/MyListAdatper(2324): the convertView is: null the position is: 0
01-02 21:57:41.491: DEBUG/MyListAdatper(2324): convertView is null, Position is: 0
01-02 21:57:41.492: DEBUG/MyListAdatper(2324): the tvName is: 0: yuyin_0023.amr the Count is: 25
01-02 21:57:41.506: DEBUG/MyListAdatper(2324): the convertView is: null the position is: 1
01-02 21:57:41.513: DEBUG/MyListAdatper(2324): convertView is null, Position is: 1
01-02 21:57:41.515: DEBUG/MyListAdatper(2324): the tvName is: 1: yuyin_0022.amr the Count is: 25
01-02 21:57:41.526: DEBUG/MyListAdatper(2324): the convertView is: null the position is: 2
01-02 21:57:41.532: DEBUG/MyListAdatper(2324): convertView is null, Position is: 2
01-02 21:57:41.533: DEBUG/MyListAdatper(2324): the tvName is: 2: yuyin_0021.amr the Count is: 25
01-02 21:57:41.543: DEBUG/MyListAdatper(2324): the convertView is: null the position is: 3
01-02 21:57:41.553: DEBUG/MyListAdatper(2324): convertView is null, Position is: 3
01-02 21:57:41.554: DEBUG/MyListAdatper(2324): the tvName is: 3: yuyin_0020.amr the Count is: 25
01-02 21:57:41.568: DEBUG/MyListAdatper(2324): the convertView is: null the position is: 4
01-02 21:57:41.575: DEBUG/MyListAdatper(2324): convertView is null, Position is: 4
01-02 21:57:41.576: DEBUG/MyListAdatper(2324): the tvName is: 4: yuyin_0019.amr the Count is: 25
01-02 21:57:41.587: DEBUG/MyListAdatper(2324): the convertView is: null the position is: 5
01-02 21:57:41.597: DEBUG/MyListAdatper(2324): convertView is null, Position is: 5
01-02 21:57:41.599: DEBUG/MyListAdatper(2324): the tvName is: 5: yuyin_0018.amr the Count is: 25
01-02 21:57:41.610: DEBUG/MyListAdatper(2324): the convertView is: null the position is: 6
01-02 21:57:41.616: DEBUG/MyListAdatper(2324): convertView is null, Position is: 6
01-02 21:57:41.617: DEBUG/MyListAdatper(2324): the tvName is: 6: yuyin_0017.amr the Count is: 25
01-02 21:57:41.630: DEBUG/MyListAdatper(2324): the convertView is: null the position is: 7
01-02 21:57:41.654: DEBUG/MyListAdatper(2324): convertView is null, Position is: 7
01-02 21:57:41.655: DEBUG/MyListAdatper(2324): the tvName is: 7: yuyin_0016.amr the Count is: 25
01-02 21:57:44.659: DEBUG/MyListAdatper(2324): the convertView is: android.widget.LinearLayout@40587050 the position is: 8
01-02 21:57:44.659: DEBUG/MyListAdatper(2324): convertView is not null, Position is: 8
01-02 21:57:44.671: DEBUG/MyListAdatper(2324): the tvName is: 8: yuyin_0015.amr the Count is: 25
01-02 21:57:44.832: DEBUG/MyListAdatper(2324): the convertView is: null the position is: 9
01-02 21:57:44.839: DEBUG/MyListAdatper(2324): convertView is null, Position is: 9
01-02 21:57:44.840: DEBUG/MyListAdatper(2324): the tvName is: 9: yuyin_0014.amr the Count is: 25
01-02 21:57:45.365: DEBUG/MyListAdatper(2324): the convertView is: android.widget.LinearLayout@405a5890 the position is: 10
01-02 21:57:45.366: DEBUG/MyListAdatper(2324): convertView is not null, Position is: 10
01-02 21:57:45.377: DEBUG/MyListAdatper(2324): the tvName is: 10: yuyin_0013.amr the Count is: 25
01-02 21:57:45.593: DEBUG/MyListAdatper(2324): the convertView is: android.widget.LinearLayout@405b7160 the position is: 11
01-02 21:57:45.593: DEBUG/MyListAdatper(2324): convertView is not null, Position is: 11
01-02 21:57:45.602: DEBUG/MyListAdatper(2324): the tvName is: 11: yuyin_0012.amr the Count is: 25
01-02 21:57:46.137: DEBUG/MyListAdatper(2324): the convertView is: android.widget.LinearLayout@4061b828 the position is: 12
01-02 21:57:46.138: DEBUG/MyListAdatper(2324): convertView is not null, Position is: 12
01-02 21:57:46.149: DEBUG/MyListAdatper(2324): the tvName is: 12: yuyin_0011.amr the Count is: 25
01-02 21:57:46.273: DEBUG/MyListAdatper(2324): the convertView is: android.widget.LinearLayout@405a0b10 the position is: 13
01-02 21:57:46.273: DEBUG/MyListAdatper(2324): convertView is not null, Position is: 13
01-02 21:57:46.281: DEBUG/MyListAdatper(2324): the tvName is: 13: yuyin_0010.amr the Count is: 25
01-02 21:57:46.617: DEBUG/MyListAdatper(2324): the convertView is: android.widget.LinearLayout@405e3cd8 the position is: 14
01-02 21:57:46.617: DEBUG/MyListAdatper(2324): convertView is not null, Position is: 14
01-02 21:57:46.627: DEBUG/MyListAdatper(2324): the tvName is: 14: yuyin_0009.amr the Count is: 25
01-02 21:57:47.267: DEBUG/MyListAdatper(2324): the convertView is: android.widget.LinearLayout@405c8ce0 the position is: 15
01-02 21:57:47.267: DEBUG/MyListAdatper(2324): convertView is not null, Position is: 15
01-02 21:57:47.279: DEBUG/MyListAdatper(2324): the tvName is: 15: yuyin_0008.amr the Count is: 25
01-02 21:57:47.403: DEBUG/MyListAdatper(2324): the convertView is: android.widget.LinearLayout@40566020 the position is: 16
01-02 21:57:47.404: DEBUG/MyListAdatper(2324): convertView is not null, Position is: 16
发表评论
-
Android Fragment 真正的完全解析(下)
2015-03-25 14:30 1142Android Fragment 真正 ... -
Android Fragment 真正的完全解析(上)
2015-03-25 10:14 939Android Fragment 真正的 ... -
Activity的生命周期方法
2015-03-25 09:54 606一:Activity的生命周期方法 Android ... -
Android在layout xml中使用include
2015-03-25 07:28 1023在Android的layout样式定义中,可以使 ... -
一个支持圆角的快速ImageView:RoundedImageView
2015-03-07 09:50 11121一个支持圆角的快速ImageView:Rounded ... -
ImageView图片设置
2015-03-05 23:08 986显示任意图像,例如图标。ImageView类可以加载 ... -
ListActivity的onListItemClick不响应的解决办法
2015-03-04 17:57 1133ListActivity的onListItemClic ... -
ListView中有控件隐藏或显示造成的显示错误问题
2015-03-03 23:03 1879ListView中有控件隐藏或显示造成的显示错误问题 ... -
Android中Application类用法
2015-03-03 09:12 1024Android中Application类用法 ... -
Android使用Application总结
2015-03-03 09:11 1217Android使用Application ... -
在android中运行java main方法
2015-02-27 10:38 1029在android中运行java main方法 ... -
android BitmapFactory.Options
2015-02-19 21:01 1057public Bitmap inBi ... -
android 基础demo例子
2015-02-16 21:06 858demoDatas.add(new DemoBe ... -
Android大图片裁剪终极解决方案
2015-02-15 17:08 2194Android大图片裁剪终 ... -
Android_照相机Camera_调用系统照相机返回data为空
2015-02-15 14:17 1314Android_照相机Camera_调用系统照相机返回 ... -
Android: 打印Bundle内容
2015-02-15 14:11 3024Android: 打印Bundle内容 有的时 ... -
android线程、UI、AsyncTask
2015-02-13 18:06 1304本文是转摘的,看过了觉得很好!所以转了过来!如果你想要 ... -
Fragment中ListView的使用
2015-02-06 23:01 2152Fragment中ListView的使用 直接 ... -
在安卓开发时用酷派手机无法打印出应用内的日志的解决方法
2014-12-11 20:39 1225在安卓开发时用酷派手机无法打印出应用内的日志的解决方法 ... -
查看安卓APK文件证书的方法
2014-12-04 10:05 1053************************** ...
相关推荐
分析这个适配器的代码,我们可以找出可能导致`getView()`多调用的问题,并进行优化。 总之,理解`ListView`的`getView()`调用机制并优化其使用是提高Android应用性能的关键。在实际开发中,我们需要仔细检查`...
### ListView中的getView重用机制详解 ...综上所述,对于题目中描述的问题,开发者可以通过以上几个方面的优化措施来解决`getView()`方法被多次调用的问题,从而提高`ListView`的性能和用户体验。
综上所述,通过合理地利用缓存、优化数据绑定以及异步处理,我们可以有效地解决ListView在`getView()`调用中出现的卡顿问题。同时,关注Android的更新和新组件,如RecycleView,也是保持应用流畅性的重要手段。在...
当调用getView()方法时,根据数据创建或复用一个View,并在这个View中添加子ListView。 子ListView的Adapter则需要处理其自身的数据源,这可能是一个简单的数组或者更复杂的对象列表。在子ListView的Adapter中,...
在描述中提到的问题,开发者在测试时发现ListView的同一项数据在不断地重复执行`getView()`。这种情况通常是由于ListView的滚动机制引起的。ListView为了提高性能,采用了视图复用(View Recycling)策略,即当一个...
在Android开发中,ListView是常用的一种控件,用于展示大量数据列表。`下拉刷新`和`上拉加载`功能的实现,极大地提升了用户体验,让用户能够实时获取到最新的数据。本篇文章将深入探讨如何在ListView中实现这两种...
在Android开发中,ListView是一种常用的组件,用于展示可滚动的列表数据。然而,有时我们可能需要在一个ListView的项中再嵌套另一个ListView,这被称为ListView的嵌套。这样的设计可以用于展示复杂的数据结构,比如...
总的来说,解决ListView中CheckBox复用问题需要对ListView的工作机制有深入理解,并且在Adapter的设计上做好状态管理和视图复用。通过以上方法,我们可以确保ListView在滚动时每个item的CheckBox状态都能正确显示,...
在Android开发中,ListView是一个非常重要的组件,它用于展示大量数据的列表,用户可以通过滚动查看更多的条目。这个教程将详细介绍Android ListView的基本用法。 ### 1. 添加ListView到布局 首先,在XML布局文件...
在Android开发中,ListView是一个非常重要的组件,它用于展示大量数据列表,通常配合适配器(Adapter)使用。本示例“android listView 适配器demo”将详细讲解如何利用适配器机制来动态填充ListView的数据。 首先...
2. 在Adapter的getView方法中,根据position判断当前RadioButton是否应该被选中,避免因ListView的复用导致的显示问题。 3. 考虑使用RadioGroup包裹每个ListView项的RadioButton,这样可以自动处理单选逻辑,但可能...
在Android开发中,ListView是一种常用的UI控件,用于展示大量数据列表。然而,要实现一个树形结构,即节点有层级关系的数据展示,就需要进行一些定制化的工作。本篇文章将详细探讨如何在不继承`ListActivity`而是...
当每条数据进入可见区域时,adapter的getview()会被调用,返回代表具体数据的视图。触摸滚动时,频繁调用。支持成百上千条数据。 下面为显示每条数据的xml文件: <LinearLayout nxmlns:android=...
在Android开发中,ListView是广泛使用的控件,用于展示大量数据列表。然而,随着移动应用交互体验的提升,用户期望更多的动态功能,如下拉刷新(Pull-to-Refresh)。下拉刷新功能允许用户通过在ListView顶部向下拉动...
在Android开发中,ListView是一种常用的组件,用于展示大量的列表数据。然而,在实现ListView时,我们可能会遇到一个问题,即按钮点击状态错乱。这个问题通常表现为点击一个列表项中的按钮后,显示的是其他列表项...
在Android开发中,ListView是一种常用的组件,用于展示...通过分析和理解这个"Android应用源码 ListView下拉刷新 Demo",开发者能够更好地掌握在实际项目中实现ListView下拉刷新的方法,提高应用的交互性和用户体验。
在Android开发中,ListView是一种常用的UI组件,用于展示大量数据列表。这个"Android实现ListView的增删改查Demo"是一个实战教程,旨在教你如何在Android应用中实现对ListView中的数据进行添加、删除、修改和查询...
在Android开发中,ListView是常用的一种控件,用于展示大量数据列表。然而,ListView的复用机制有时会导致一些问题,特别是在涉及复选框(Checkbox)的状态管理时。本篇文章将详细探讨这个问题,并提供一个完美的...
在Android开发中,ListView是一个非常重要的组件,常用于展示大量数据列表。本示例"Android ListView详细demo源码"提供了一个可以直接使用的ListView实现,适用于学习和快速开发。在这个项目中,我们将深入探讨...