使BaseAdapter.hasStableIds()返回true会解决什么? [英] What does having BaseAdapter.hasStableIds() return true solve?

查看:256
本文介绍了使BaseAdapter.hasStableIds()返回true会解决什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图了解BaseAdapter.hasStableIds()的工作方式.

据我所读,从此方法返回true有助于适配器确定视图是否必须重新创建或保持不变.我猜适配器会以某种方式将位置映射到id,如果映射保持不变,则调用notifyDataSetChanged()时适配器将不执行任何操作.

但是,如果id(和位置)保持不变的话,如果数据已更改,这不是问题吗?如果确实存在问题,那么什么时候才返回真正的帮助?

解决方案

hasStableIds() == true表示基础数据项的项ID不变,而不是该项的数据永不变.例如.相同的联系人具有相同的ID,无论其出现在列表中的什么位置,但是其名称可能会随时间而改变-用户可以对其进行编辑,添加昵称等.可以想象在每个项目没有固定ID的情况下显示数据(也许数据不是您的,并且您无法访问适合从getItemId()返回的唯一ID,例如与long返回类型不兼容的唯一ID.)

在浏览AdapterViewAbsListView的源代码时,在我看来,这在您在ListView/GridView上设置选择模式时最有用,因为那样一来,该视图可以对选中的对象进行更有效的管理.项目ID,它们是视觉状态,包括跨数据集更改的位置,在这些数据更改中,那些选中的项目可能已移至新位置(但它们具有相同的ID).

I have been trying to understand how BaseAdapter.hasStableIds() works.

From what I have read, returning true from this method helps the adapter determine if the views must be recreated or left the same. I am guessing the adapter is somehow mapping the position to an id, and if the mapping stays the same, the adapter does nothing when notifyDataSetChanged() is called.

But wouldn't this be a problem if the data has been changed while the id(and the position) remains the same? If this is indeed an issue, when exactly does returning true help?

解决方案

hasStableIds() == true means that item IDs do not change for the underlying data items, not that the data of that item never changes. E.g. the same contact has the same ID regardless where it appears in a list, but its name may change over time--user could edit it, add a nickname, etc. It's possible to imagine displaying data where each item does not have some stable ID (perhaps the data is not yours and you have no way to access a unique ID that is suitable to return from getItemId(), e.g. not compatible with the long return type).

In browsing the source code for AdapterView and AbsListView, it seems to me that this is mostly useful when you set a choice mode on a ListView/GridView, because then the view can do some more efficient management of checked item IDs and they're visual state, including across data set changes where those checked items may have moved to new positions (but they have the same ID).

这篇关于使BaseAdapter.hasStableIds()返回true会解决什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆