`
stephen830
  • 浏览: 2978124 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

android listview 连续调用 getview问题分析及解决

 
阅读更多

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

 

分享到:
评论
2 楼 q284985092 2011-12-07  
谢谢你了 找了好久的问题!~
1 楼 ableouou 2011-10-11  
有个问题请教下:关于position的值 说得不是很清楚,position既然是数据在adpter里面的位置,就不会是屏幕的最大显示数的循环,比如说0-7。附上log如下:
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 listview getview多调用俩周期

    分析这个适配器的代码,我们可以找出可能导致`getView()`多调用的问题,并进行优化。 总之,理解`ListView`的`getView()`调用机制并优化其使用是提高Android应用性能的关键。在实际开发中,我们需要仔细检查`...

    Android ListView与getView调用卡顿问题解决办法

    主要介绍了Android ListView与getView调用卡顿问题解决办法的相关资料,这里提供实例及解决办法帮助大家解决这种问题,需要的朋友可以参考下

    ListView中getView重用好多次

    ListView中getView重用好多次,有头像的布局

    Android ListView 下拉刷新、上拉加载

    在Android开发中,ListView是常用的一种控件,用于展示大量数据列表。`下拉刷新`和`上拉加载`功能的实现,极大地提升了用户体验,让用户能够实时获取到最新的数据。本篇文章将深入探讨如何在ListView中实现这两种...

    android的listview嵌套listview,列表嵌套列表 android studio版本

    当调用getView()方法时,根据数据创建或复用一个View,并在这个View中添加子ListView。 子ListView的Adapter则需要处理其自身的数据源,这可能是一个简单的数组或者更复杂的对象列表。在子ListView的Adapter中,...

    Android listview嵌套listview

    在Android开发中,ListView是一种常用的组件,用于展示可滚动的列表数据。然而,有时我们可能需要在一个ListView的项中再嵌套另一个ListView,这被称为ListView的嵌套。这样的设计可以用于展示复杂的数据结构,比如...

    android listview控件复用问题解决

    总的来说,解决ListView中CheckBox复用问题需要对ListView的工作机制有深入理解,并且在Adapter的设计上做好状态管理和视图复用。通过以上方法,我们可以确保ListView在滚动时每个item的CheckBox状态都能正确显示,...

    android listview分页查询显示

    在Android开发中,ListView是一种常用的组件,用于展示大量的列表数据。分页查询是优化用户体验、减少内存消耗的有效方法,特别是当数据源庞大时。在这个"android listview分页查询显示"的Demo中,我们将深入探讨...

    android ListView简单用法

    在Android开发中,ListView是一个非常重要的组件,它用于展示大量数据的列表,用户可以通过滚动查看更多的条目。这个教程将详细介绍Android ListView的基本用法。 ### 1. 添加ListView到布局 首先,在XML布局文件...

    android listView 适配器demo

    在Android开发中,ListView是一个非常重要的组件,它用于展示大量数据列表,通常配合适配器(Adapter)使用。本示例“android listView 适配器demo”将详细讲解如何利用适配器机制来动态填充ListView的数据。 首先...

    androidlistview里面使用radiobutton

    2. 在Adapter的getView方法中,根据position判断当前RadioButton是否应该被选中,避免因ListView的复用导致的显示问题。 3. 考虑使用RadioGroup包裹每个ListView项的RadioButton,这样可以自动处理单选逻辑,但可能...

    android ListView实现树形结构

    在Android开发中,ListView是一种常用的UI控件,用于展示大量数据列表。然而,要实现一个树形结构,即节点有层级关系的数据展示,就需要进行一些定制化的工作。本篇文章将详细探讨如何在不继承`ListActivity`而是...

    Android listview 下拉刷新

    在Android开发中,ListView是广泛使用的控件,用于展示大量数据列表。然而,随着移动应用交互体验的提升,用户期望更多的动态功能,如下拉刷新(Pull-to-Refresh)。下拉刷新功能允许用户通过在ListView顶部向下拉动...

    Android Listview 按钮点击状态错乱之解决方法

    在Android开发中,ListView是一种常用的组件,用于展示大量的列表数据。然而,在实现ListView时,我们可能会遇到一个问题,即按钮点击状态错乱。这个问题通常表现为点击一个列表项中的按钮后,显示的是其他列表项...

    Android应用源码 ListView下拉刷新 Demo

    在Android开发中,ListView是一种常用的组件,用于展示...通过分析和理解这个"Android应用源码 ListView下拉刷新 Demo",开发者能够更好地掌握在实际项目中实现ListView下拉刷新的方法,提高应用的交互性和用户体验。

    Android完美解决ListView复用导致的Checkbox状态混乱问题

    在Android开发中,ListView是常用的一种控件,用于展示大量数据列表。然而,ListView的复用机制有时会导致一些问题,特别是在涉及复选框(Checkbox)的状态管理时。本篇文章将详细探讨这个问题,并提供一个完美的...

    Android ListView详细demo源码

    在Android开发中,ListView是一个非常重要的组件,常用于展示大量数据列表。本示例"Android ListView详细demo源码"提供了一个可以直接使用的ListView实现,适用于学习和快速开发。在这个项目中,我们将深入探讨...

    Android 解决ListView的复用问题 demo

    通过这个"Android 解决ListView的复用问题 demo",开发者可以学习到如何在实际项目中应用以上策略,以优化ListView的性能并避免可能出现的问题。博客文章提供了更详细的步骤和代码示例,可以帮助开发者深入理解...

    android listview单击事件

    在Android开发中,ListView是应用最广泛的视图组件之一,尤其在展示大量数据时,它以列表形式高效地展示信息。本教程将深入探讨ListView的单击事件处理,以及如何在实际项目中实现这一功能。 首先,理解ListView的...

Global site tag (gtag.js) - Google Analytics