View 的 setTag() getTag() 方法的主要用途是什么?
View
类型对象的setTag()
、getTag()
等方法的主要用途是什么?
我认为我可以将任意数量的对象与单个视图关联起来,这样的想法正确吗?
What is the main purpose of such methods as setTag()
and getTag()
of View
type objects?
Am I right in thinking that I can associate any number of objects with a single View?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
假设您生成了一堆相似的视图。您可以为每个视图单独设置一个
OnClickListener
:然后您必须为每个视图创建一个唯一的
onClick
方法,即使它们执行类似的操作,例如:这是因为 < code>onClick 只有一个参数,即
View
,并且它必须从封闭范围内的实例变量或最终局部变量获取其他信息。我们真正想要的是从视图本身获取信息。输入
getTag
/setTag
:现在我们可以为每个按钮使用相同的 OnClickListener:
这基本上是视图拥有记忆的一种方式。
Let's say you generate a bunch of views that are similar. You could set an
OnClickListener
for each view individually:Then you have to create a unique
onClick
method for each view even if they do the similar things, like:This is because
onClick
has only one parameter, aView
, and it has to get other information from instance variables or final local variables in enclosing scopes. What we really want is to get information from the views themselves.Enter
getTag
/setTag
:Now we can use the same OnClickListener for every button:
It's basically a way for views to have memories.
我想补充几句话。
尽管在 ViewHolder 模式的特定情况下使用 get/setTag(Object) 似乎非常有用,但我建议在其他情况下使用它之前要三思而后行。几乎总是有另一种具有更好设计的解决方案。
主要原因是这样的代码很快就会变得不支持。
对于其他开发人员来说,您设计在视图中存储为标记的内容并不明显。
setTag/getTag
方法根本没有描述性。它只存储一个
Object
,当您想要getTag
时需要对其进行强制转换。当您决定更改标记中存储对象的类型时,稍后可能会发生意外崩溃。这是一个真实的故事:我们有一个相当大的项目,有很多适配器、带有视图的异步操作等等。一位开发人员决定在他的代码部分中设置
set/getTag
,但另一位开发人员已经将此标记设置到该视图。最后有人找不到自己的标签,很迷茫。我们花了几个小时才找到这个 bug。setTag(int key, Object tag)
看起来好多了,因为您可以为每个标签生成唯一的键(使用 id 资源),但是对于 Android 有一个显着的限制 << 4.0。来自 Lint 文档:I'd like to add few words.
Although using
get/setTag(Object)
seems to be very useful in the particular case of a ViewHolder pattern, I'd recommend to think twice before using it in other cases. There is almost always another solution with better design.The main reason is that code like that becomes unsupportable pretty quickly.
It is non-obvious for other developers what you designed to store as tag in the view. The methods
setTag/getTag
are not descriptive at all.It just stores an
Object
, which requires to be cast when you want togetTag
. You can get unexpected crashes later when you decide to change the type of stored object in the tag.Here's a real-life story: We had a pretty big project with a lot of adapters, async operations with views and so on. One developer decided to
set/getTag
in his part of code, but another one had already set the tag to this view. In the end, someone couldn't find his own tag and was very confused. It cost us several hours to find the bug.setTag(int key, Object tag)
looks much better, cause you can generate unique keys for every tag (using id resources), but there is a significant restriction for Android < 4.0. From Lint docs:我们可以使用
setTag()
和getTag()
根据我们的要求设置和获取自定义对象。setTag()
方法采用Object
类型的参数,而getTag()
返回一个Object
。例如,
We can use
setTag()
andgetTag()
to set and get custom objects as per our requirement. ThesetTag()
method takes an argument of typeObject
, andgetTag()
returns anObject
.For example,
对于 Web 开发人员来说,这似乎相当于数据......
For web developers, this seems to be the equivalent to data-..
这对于自定义 ArrayAdapter 使用非常有用。这是某种优化。
setTag
用作对布局某些部分(在ListView
中显示)的引用的对象的引用,而不是findViewById
。This is very useful for custom
ArrayAdapter
using. It is some kind of optimization. TheresetTag
used as reference to object that references on some parts of layout (that displaying inListView
) instead offindViewById
.参考: http://developer.android.com/reference/android/view/View .html
Reference: http://developer.android.com/reference/android/view/View.html
当您有 ListView 并想要回收/重用视图时,设置 TAG 非常有用。通过这种方式,ListView 变得与较新的 RecyclerView 非常相似。
Setting of TAGs is really useful when you have a ListView and want to recycle/reuse the views. In that way the ListView is becoming very similar to the newer RecyclerView.