有什么用视图的setTag方法? [英] What is the use of the View's setTag method?

查看:84
本文介绍了有什么用视图的setTag方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施的ListView 使用自定义适配器。为了提高我回来了性能新充气查看如果 convertView 为空,否则返回回收查看

I am implementing a ListView using custom adapters. To improve the performance I am returning the newly inflated View if convertView is null, else returning the recycled View.

现在,在这个过程中,我使用了 ViewHolder 的模式,并使用了查看<的 setTag()方法/ code>。起初我以为这是某种形式的书签,但我还没有完全了解它的使用。我检查了不同的博客文章,他们只是用这种在code。

Now in this process I used the ViewHolder pattern and used the setTag() method of the View. Initially I thought this is some kind of bookmarking but I've not completely understood the use of it. I've checked different blog post where they just used this in the code.

有人能解释我使用了 setTag()方法?

Can someone please explain me the use of the setTag() method?

推荐答案

基本上,你可以存储任何类型的对象作为标记(和调用时,它转换回 getTag )。这可以是一个简单的ID或一些复杂的数据。这是你联想到这一观点的一些信息。

Basically you can store any kind of object as tag (and cast it back when calling getTag). This can be a simple ID or some complex data. It's some information which you associate with this view.

在列表中的情况下和的视图架的模式是其中提到的观点被标记的视图(组)的一个简单的对象。所以,你不必叫 findViewById 每次当你更新视图的内容时。这只是一个性能优化。

In the case of lists and the view holder pattern it's a simple object which contains references to views of the tagged view (group). So you don't have to call findViewById every time when you're updating the content of the view. It's just an performance optimization.

我们能存储列表项的数据视图中的标记?
号因为从回收你有(EG)这是重复使用1000列表项10次。在标签中存储的数据使得这里没有任何意义。这是更好地使用自定义数据对象存储列表项的状态(可能是在同一个阵列,其中包含所显示的数据),或者你坚持它的时候了列表项的变化。

Can we store data of list item in the view tag?
No. Because of view recycling you have (e.g.) 10 views which are reused for 1000 list items. Storing data in the tag makes no sense here. It's better to use an custom data object to store the list item state (probably the same array which contains the displayed data) or you persist it right away on list item change.

另请参见<一个href="http://developer.android.com/reference/android/view/View.html#setTag%28java.lang.Object%29">setTag文档。

这篇关于有什么用视图的setTag方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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