Android knowledge 500 - View.getTag () View.setTag () usage

Foreword

First of all we need to know setTag method is doing, SDK interpreted as

Tags

Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure. 

Tag ID is not the designated view. Tag view is that additional information associated with the essence. They are often used to store data for some of view, this is very easily deposited without an additional separate structure. 

convertView of TAG

1. View expanded for use LayoutInflater objects using Tag

    Before, in the ListView extended BaseAdapter implementation does not store items in the list , so we are writing code in getView:

public class BaseAdapterEx extends BaseAdapter{

     /*
     * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) 
     */
    @Override
    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的妙用
          convertView.setTag(holder);
       } else {
          // setTag的妙用
          holder = (ViewHolder) convertView.getTag();
       } 

       // ToDo 去更新 img, title, info的信息。因为传入的position是列表的ID。
    }
}

        First of all we need to know setTag method is doing, he is a label for the View object, the label can be anything, we are here to set him become a target, because we are the vlist2.xml elements abstracted into a class ViewHolder , with the setTag, the label is, after ViewHolder a property of an object is instantiated. Then we ViewHolder for the operation of the instantiated object holder, will be cited as the mechanism of java and has survived and change the content convertView, rather than each time a new go. We just reached reuse - and I hope I make myself clear. If there is a simpler explanation, please advise.

This is what we use in the Adapter, then we do not use Tag label what will happen here?

We imagine that if we do not Tag label, so our object cache and how to combine and convertView achieve a reasonable efficiency use? Looks like the answer is not clear - so use Tag is a more sensible approach.

 

2. Tag is not used for LayoutInflater objects View of expansion.

if (convertView != null) {

  view = convertView;

  ...

  } else {

  view = new Xxx(...);

  ...

  }

This is our program, we see that seemingly useless Tag-- Yes, when there is no LayoutInflater be expanded using the View, there is no need to use, although you can also use.

 

3. Tag use the other View

We can operate on all the View objects, as to how to use, to see how you think the author, said the following example a subclass of View button for a use of the tag.

Directly attached to the 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, "我是button1", Toast.LENGTH_LONG).show();
           break;
       } 
       case 2: {
           Toast.makeText(this, "我是button2", Toast.LENGTH_LONG).show();
           break;
       } 
       case 3: {
           Toast.makeText(this, "我是button3", Toast.LENGTH_LONG).show();
           break;
       } 
       default: {
           break;
       }

       }
    }
}

Xml page code is not posted. This example is a three button and then click on the interface the user clicks the button will be displayed. Our program is a global monitor page, set up in front of the listener tag for each button, and then we switch when using getTag remove the label to see what operating.

The advantage of this is that you can listen to centrally manage and improve the readability of the code - of course, this is my self-understanding. 

 

However, it is not often achieved by getId () this method? This is not looking more aware?

    @Override
    public void onClick(View arg0) {
       // TODO Auto-generated method stub
       int tag = (Integer) arg0.getTag();
       switch (arg0.getId()) {
       case R.id.button1: {
           Toast.makeText(this, "我是button1", Toast.LENGTH_LONG).show();
           break;
       } 
       case R.id.button2: {
           Toast.makeText(this, "我是button2", Toast.LENGTH_LONG).show();
           break;
       } 
       case R.id.button3: {
           Toast.makeText(this, "我是button3", Toast.LENGTH_LONG).show();
           break;
       } 
       default: {
           break;
       }

 

Published 112 original articles · won praise 3 · Views 9705

Guess you like

Origin blog.csdn.net/yush34/article/details/104944234