hasStableIds的作用
2015-11-10 20:41
495 查看
如果hasStableIds返回false的话 每次调用notifyDataSetChanged方法时 adapter就会判断getItemId 并且在只调用那些Item发生变化的getView方法,说白了就是通过getItemId来判断那些需要getView从而达到局部刷新的效果,在getView比较耗时的情况下起到优化的效果。下面是stackoverflow的原文,并给出了用例。
If
false then each time you call
Adapter will look at the returned value of
will eventually call
Using this technique you can easelly update only one Item in the ListView
If you implement
then it might be very useful.
Example :
You have a list of albums :
And you implement
this :
Now your item id depends on the values of coverUrl and title fields and if you change then and call
your adapter, then the adapter will call getItemId() method of each element and update only thouse items which id has changed.
This is very useful if are doing some "heavy" operations in your
If
hasStableIds()returns
false then each time you call
notifyDataSetChanged()your
Adapter will look at the returned value of
getItemIdand
will eventually call
getView(int position, View convertView, ViewGroup parent)only for thous items which id has changed.
Using this technique you can easelly update only one Item in the ListView
If you implement
getItemIdcorrectly
then it might be very useful.
Example :
You have a list of albums :
class Album{ String coverUrl; String title; }
And you implement
getItemIdlike
this :
@Override public long getItemId(int position){ Album album = mListOfAlbums.get(position); return (album.coverUrl + album.title).hashcode(); }
Now your item id depends on the values of coverUrl and title fields and if you change then and call
notifyDataSetChanged()on
your adapter, then the adapter will call getItemId() method of each element and update only thouse items which id has changed.
This is very useful if are doing some "heavy" operations in your
getView().
相关文章推荐
- lightoj 1030 - Discovering Gold 【期望】
- windows环境下搭建react native0.12.0 android环境
- Codeforces 543B Destroying Roads 【暴力 SPFA】
- quartz详解
- Json的学习
- 机器学习算法之:KNN
- hdu 5113 Black And White
- Android 中的dp,px,pt,sp比较
- linux常用命令
- linux字体操作,安装字体
- linux常用命令
- HDU5195 线段树+拓扑
- Linux多线程实践(6) --Posix读写锁解决读者写者问题
- hdu1058 Humble Numbers (动态规划)
- 随笔
- LeetCode 21 Merge Two Sorted Lists(合并两个已排序的链表)(Linked List)
- java jni 入门3 - 字符串参数
- 1006. 换个格式输出整数 (15)
- 数据挖掘5-K近邻
- AT&T汇编指令总结