hasStableIds的作用

如果hasStableIds返回false的话 每次调用notifyDataSetChanged方法时 adapter就会判断getItemId 并且在只调用那些Item发生变化的getView方法,说白了就是通过getItemId来判断那些需要getView从而达到局部刷新的效果,在getView比较耗时的情况下起到优化的效果。下面是stackoverflow的原文,并给出了用例。


If hasStableIds() returns false then each time you call notifyDataSetChanged() your Adapter will look at the returned value of getItemId and 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 getItemId like 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().



  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值