Detailed explanation of ViewHolder mode development in Android

Source: Internet
Author: User
Tags java reference

Detailed explanation of ViewHolder mode development in Android

Detailed explanation of ViewHolder mode development in Android development:
1. Explanation of ViewHolder:
(1 ).It is just a static class, not an Android API method.
(2 ).Its function is to reduce unnecessary calls to findViewById, and thenControl ReferenceIn ViewHolder,In View. setTag (holder), place it in view, and you can directly retrieve it next time.

2. TAG in convertView:

(1)Unlike the ID, A. Tag indicates a view. In essence, a Tag is the additional information of the associated view. They are often used to store some view data, which is very convenient and does not need to be stored in another separate structure.
(2 ).First, we need to know what the setTag method is:It is a label for the View object.*A tag can be any content. Here we set it to an object *, because we abstract the elements of vlist2.xml into a ViewHolder class and use setTag, this label is an attribute of the object after ViewHolder is instantiated. The subsequent operations on the object holder instantiated by ViewHolder will survive and change the convertView content because of the java reference mechanism, instead of each new object. So we can achieve reuse.

(3). Use tags of other views

We canOperate on all View objectsThe following example shows how to use a View subclass button for a tag.

Directly paste the Code:

Public class ButtonTagTestActivity extends Activity implements OnClickListener {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); 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 ;}}}}

In this example, click the three buttons on the interface and the user-clicked buttons are displayed. Our program implements global page listening, sets the tag of each button before listening, and then uses the tag retrieved by getTag to see what the operation is.

The advantage of this operation is that the listener can be centrally managed to improve the ease of coding.
Summary:
Tag is used to set tags. tags can be anything.
And how convertView makes code running more efficient in a program: Use the cached convertView to instantiate objects of the same struct as little as possible;

 

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.