Android: assigning getTag () and setTag () in view class

public void setTag(final Object tag) { mTag = tag; } public Object getTag() { return mTag; } 

These are two methods from the View class in Android. The following is the official documentation of these two methods respectively.

  /** * Returns this view tag. * * @return the Object stored in this view as a tag * * @see #setTag(Object) * @see #getTag(int) */ /** * Sets the tag associated with this view. A tag can be used to mark * a view in its hierarchy and does not have to be unique within the * hierarchy. Tags can also be used to store data within a view without * resorting to another data structure. * * @param tag an Object to tag the view with * * @see #getTag() * @see #setTag(int, Object) */ 
Tag Functions

widely used in the implementation of the base adapter, but I could not understand its purpose and how to use them. Could you explain this good example to help me understand the role of these functions.

+6
source share
5 answers

Think of it as simple as possible a tag.

What does the grocery store tag tell you? Basically, a lot of things, such as name, price, country of origin, discount and many others.

Imagine you are creating an application that displays such products in a grid using ImageView. How to easily determine product features from ImageView? Marking is one of the possible solutions.

 class Product { String mName; String mManufacturer; String mOriginCountry; double mPrice; int mDiscount; int mImageId; // A reference to a drawable item id } [...] class My Activity { @Override protected void onCreate() { [...] // Some code // Grab the imageView reference and grab (or create) the tag for it ImageView someItemView = (ImageView) findViewById(R.id.some_product_view); Product someProductTag = new Product( ... some data ...); // Add the tag to the ImageView someItemView.addTag(someProductTag); } private void displayItemInfo(ImageView iv) { // Grab the tag and display the info. String productName = ((Product)iv.getTag()).mName(); Toast.show(mContext, "This is " + productName, Toast.LONG).show(); } } 

Of course, this is very simplified. Ideally, you will have an adapter that creates a view based on your provided tags or automatically creates your tags.

I hope you get the idea

+8
source

A good example is the View Holder template. An implementation can be found in this guide in ListViews.

The View Holder template avoids the use of the findViewById () method in the adapter.

The ViewHolder class is a static inner class in your adapter that contains links to the corresponding views. in your layout. This link is assigned to the representation of the string as a tag through the setTag () method.

If we get a convertView object, we can get an instance of ViewHolder using the getTag () method and assign new attributes to the views via the ViewHolder link.

While this sounds complicated, it's about 15% faster than using the findViewById () method.

Example

The following code shows an adapter implementation with performance optimization that reuses existing views and implements a holder template.

 import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyPerformanceArrayAdapter extends ArrayAdapter<String> { private final Activity context; private final String[] names; static class ViewHolder { public TextView text; public ImageView image; } public MyPerformanceArrayAdapter(Activity context, String[] names) { super(context, R.layout.rowlayout, names); this.context = context; this.names = names; } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; // reuse views if (rowView == null) { LayoutInflater inflater = context.getLayoutInflater(); rowView = inflater.inflate(R.layout.rowlayout, null); // configure view holder ViewHolder viewHolder = new ViewHolder(); viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01); viewHolder.image = (ImageView) rowView .findViewById(R.id.ImageView01); rowView.setTag(viewHolder); } // fill data ViewHolder holder = (ViewHolder) rowView.getTag(); String s = names[position]; holder.text.setText(s); if (s.startsWith("Windows7") || s.startsWith("iPhone") || s.startsWith("Solaris")) { holder.image.setImageResource(R.drawable.no); } else { holder.image.setImageResource(R.drawable.ok); } return rowView; } } 
+2
source

This is just a way to bind Object to a view.
In the BaseAdapter, it is used to store view identifiers to avoid the expensive findViewById operation every time the system requests a view.

+1
source

setTag(int, object) very useful for storing a custom class for presentation. Here is my implementation:

  itemView.setTag(R.id.food_tag, food); 

A food variable is an instance of a custom Food object. The key for setTag must always be a resource identifier.

I could get this custom object this way in OnClickListener() :

 Food selectedFood = (Food) view.getTag(R.id.food_tag); 
+1
source

The Tag field is a good way to save your own state information for any view class. One of them is to save the view position in a ListView . See this answer for an example.

0
source

Source: https://habr.com/ru/post/974423/


All Articles