Objective
First we need to know what the Settag method is, and the SDK is interpreted as
Tags
Unlike IDs, tags is not used to identify views. Tags is essentially an extra piece of information that can is associated with a view. They is most often used as a convenience to store data related to views in the views themselves rather than by putting th EM in a separate structure.
Tag is not the same as the ID is marked with the view. Tag is essentially the additional information of the associated view. They are often used to store some view data, which is very handy and does not have to be stored in separate structures.
tag in the Convertview
1. Use of the tag using the Layoutinflater object for view expansion
Before, in adapter, we were the same code in GetView:
Public View GetView (int position, view Convertview, ViewGroup parent) {
Viewholder holder = null;
if (Convertview = = null) {
Holder = new viewholder ();
Convertview = inflater. Inflate (r.layout. Vlist2, null);
Holder. img = (ImageView) Convertview.findviewbyid (r.id. IMG);
Holder. title = (TextView) Convertview.findviewbyid (r.id. Title);
Holder. info = (TextView)
Convertview. Findviewbyid (r.id. Info);
//Settag's Magical
convertview. Settag (holder);
} Else {
//Settag's Magical
holder = (viewholder) convertview.gettag ();
}
...... Slightly
}
Pay attention to the marked red place, they are using the tag.
First we need to know what the Settag method is, he is a label to the View object, the tag can be anything, we set him up as an object here, because we are to abstract vlist2.xml elements into a class viewholder, with the Settag, This tag is a property of the Viewholder object after it is instantiated. the subsequent operation of the Viewholder instantiated object holder will always survive and change the contents of the Convertview because of the Java reference mechanism, instead of going to the new one every time. We're just going to make it so reusable --I hope I'm clear about it. If there is a simpler explanation, please advise.
This is what we use in adapter, so what happens when we don't use tag tags here?
Let's just imagine, if we don't use tag tags, how do our objects combine with the Convertview cache and achieve reasonable efficiency? It seems that the answer is not clear-so using tag is a wise thing to do.
2. Use of tags that do not use the Layoutinflater object for view expansion.
if (Convertview! = null) {
view = Convertview;
...
} else {
view = new Xxx (...);
...
}
This is our program, we see, seemingly no use tag--yes, when there is no use of layoutinflater to expand the view, there is no need to use, although it can also be used.
3. For other view's tag use
We can manipulate all the view objects, as for how to use, see what the author thinks, the following example is a view of the sub-class button for the use of tag.
Directly post code:
Public class buttontagtestactivity extends Activity implements Onclicklistener {
@Override
Public void onCreate (Bundle savedinstancestate) {
Super . OnCreate (savedinstancestate);
Setcontentview (r.layout. Main);
Button button1 = (button) Findviewbyid (r.id. Button1);
Button button2 = (button) Findviewbyid (r.id. Button2);
Button Button3 = (button) Findviewbyid (r.id. Button3);
Button1.settag (1);
Button2.settag (2);
Button3.settag (3);
Button1.setonclicklistener (this);
}
@Override
Public void OnClick (View arg0) {
// TODO auto-generated method stub
int tag = (Integer) arg0. Gettag ();
Switch (tag) {
Case 1: {
Toast. Maketext (This, "I'm button1", Toast. ) Length_long). Show ();
Break ;
}
Case 2: {
Toast. Maketext (This, "I'm button2", Toast. ) Length_long). Show ();
Break ;
}
Case 3: {
Toast. Maketext (This, "I'm button3", Toast. ) Length_long). Show ();
Break ;
}
default : {
Break ;
}
}
}
}
The XML page code is not posted. This example is 3 buttons on the Click interface and then the button that the user clicked on is displayed. Our program is to achieve the page global monitoring, before listening to set the tag of each button, then we in the switch, using the Gettag out of the label to see what the operation.
The benefit is that monitoring can be centrally managed to improve the readability of the code--which, of course, is my self-understanding.
Postscript
Having seen so many examples, I think I have understood the tag and Convertview.
Yes, we know that tag is about setting the tag, and the tag can be any thing.
And Convertview is how to make the code run efficiently in the program: using the cache Convertview as little as possible to instantiate the same structure object;
http://blog.csdn.net/dinko321/article/details/7329504
From:http://blog.sina.com.cn/s/blog_4f1c99de01015skb.html
"Turn" Android Settag