The understanding of tag in Convertview

Source: Internet
Author: User



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);


The Magical magic of Settag


Convertview.settag (holder);





} else {


The Magical magic of Settag


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 abstract the elements of Vlist2.xml into a class viewholder, using Settag, this tag is a property of the object after the Viewholder instantiation. 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 that. 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 {


/** called when the activity is first created. */


@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 am Button1", Toast.length_long). Show ();


Break


}





Case 2: {


Toast.maketext (This, "I am Button2", Toast.length_long). Show ();


Break


}





Case 3: {


Toast.maketext (This, "I am 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;

Understanding of tags in convertview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.