编程视图如何设置唯一 ID?
我正在我的应用程序中创建一堆程序化的View
。看起来它们默认都具有相同的 id=-1
。为了与他们合作,我需要生成唯一的 ID。
我已经尝试了几种方法 - 随机数生成和基于当前时间,但无论如何都不能 100% 保证不同的视图将具有不同的 id
只是想知道是否有更可靠的方法来生成唯一的视图?可能有特殊的方法/类?
I am creating in my app bunch of programmatic View
s. As it appeared to be they all by default have the same id=-1
. In order to work with them I need to generate unique id's.
I have tried several approaches - random number generation and based on current time, but anyway there's no 100% guarantee that different Views will have different id's
Just wondering is there any more reliable way to generate unique ones? Probably there's special method/class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是想添加到 Kaj 的答案中,从 API 级别 17 开始,您可以调用
View.generateViewId()
然后使用 View.setId(int) 方法。
如果您需要它用于低于级别 17 的目标,这里是 View.java 中的内部实现,您可以直接在项目中使用:
大于 0x00FFFFFF 的 ID 号是为 /res xml 文件中定义的静态视图保留的。 (很可能是我项目中 R.java 中的 0x7f******。)
从代码来看,Android 不希望您使用 0 作为视图的 id,并且需要在 0x01000000 之前翻转它以避免与静态资源 ID 冲突。
Just want to add to Kaj's answer, from API level 17, you can call
View.generateViewId()
then use the View.setId(int) method.
In case you need it for targets lower than level 17, here is its internal implementation in View.java you can use directly in your project:
ID number larger than 0x00FFFFFF is reserved for static views defined in the /res xml files. (Most likely 0x7f****** from the R.java in my projects.)
From the code, somehow Android doesn't want you to use 0 as a view's id, and it needs to be flipped before 0x01000000 to avoid the conflits with static resource IDs.
只是对 @phantomlimb 答案的补充,
而
View.generateViewId()
要求 API 级别 >= 17,该工具兼容所有API。
根据当前的 API 级别,
它决定是否使用系统API 来决定天气。
因此您可以在
同一时间,不用担心获得相同的 id
Just an addition to the answer of @phantomlimb,
while
View.generateViewId()
require API Level >= 17,this tool is compatibe with all API.
according to current API Level,
it decide weather using system API or not.
so you can use
ViewIdGenerator.generateViewId()
andView.generateViewId()
in thesame time and don't worry about getting same id
从支持库27.1.0开始,ViewCompat中有generateViewId()
ViewCompat.generateViewId ()
Since support library 27.1.0 there's generateViewId() in ViewCompat
ViewCompat.generateViewId()
创建一个具有原子整数的单例类。碰撞整数,并在需要视图 ID 时返回值。
该 ID 在进程执行期间将是唯一的,但在进程重新启动时将重置。
请注意,如果视图“图表”中已存在具有 id 的视图,则 id 可能不是唯一的。您可以尝试从 Integer.MAX_VALUE 的数字开始,然后减少它,而不是从 1 -> 开始。 MAX_VALUE
Create a singleton class, that has an atomic Integer. Bump the integer, and return the value when you need a view id.
The id will be unique during the execution of your process, but wil reset when your process is restarted.
Note that the id might not be unique, if there already are views that have ids in the view 'graph'. You could try to start with a number that is Integer.MAX_VALUE, and decrease it instead of going from 1 -> MAX_VALUE
关于 API<17 的后备解决方案,我看到建议的解决方案从 0 或 1 开始生成 ID。 View 类有另一个生成器实例,并且也从第一个开始计数,这将导致您和 View 的生成器生成相同的 ID,并且您最终将在视图层次结构中拥有具有相同 ID 的不同视图。不幸的是,没有一个好的解决方案,但这是一个应该有详细记录的黑客:
Regarding the fallback solution for API<17, I see that suggested solutions start generating IDs starting from 0 or 1. The View class has another instance of generator, and also starts counting from number one, which will result in both your and View's generator generating the same IDs, and you will end up having different Views with same IDs in your View hierarchy. Unfortunately there is no a good solution for this but it's a hack that should be well documented: