Before talking about settag and gettag, let's talk about viewholder. It is not a fixed API developed by Android, but a design method recommended by Google demo. Viewholder objects generally include all components in the listview subitem. convertview is empty. the ID of each component in the list subitem is stored in viewholder and applied through the settag method, append the object with a view reference to the view. In this way, when the listview is updated, you do not need to repeat the reference and forcibly convert it. findviewbyid (R. id.IMG); Gettag is used to retrieve the reference of each component from the viewholder carried by the view.
The role of settag and gettag is obvious. Tag is the meaning of tag, but here it is not just a view tag, in essence, it is any data appended to the view, and the settag (Object OBJ) parameter is OBJ and any object. While gettag is better understood. Since settag attaches data, it is natural to have a way to take it and use it again.
Please refer to the code --
Code for using settag, gettag, and viewholder
ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item_icon_text, null); holder = new ViewHolder(); holder.icon1 = (ImageView) convertView.findViewById(R.id.icon1); holder.text1 = (TextView) convertView.findViewById(R.id.text1); holder.icon2 = (ImageView) convertView.findViewById(R.id.icon2); holder.text2 = (TextView) convertView.findViewById(R.id.text2); convertView.setTag(holder); } else{ holder = (ViewHolder)convertView.getTag(); } holder.icon1.setImageResource(R.drawable.icon); holder.text1.setText(mData[position]); holder.icon2 .setImageResource(R.drawable.icon); holder.text2.setText(mData[position]);} static class ViewHolder { TextView text1; ImageView icon1; TextView text2; ImageView icon2; }
Settag is not used. When gettag and viewholder are used
if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item_icon_text, null); } ((ImageView) convertView.findViewById(R.id.icon1)).setImageResource(R.drawable.icon); ((TextView) convertView.findViewById(R.id.text1)).setText(mData[position]); ((ImageView) convertView.findViewById(R.id.icon2)).setImageResource(R.drawable.icon); ((TextView) convertView.findViewById(R.id.text2)).setText(mData[position]);
}
When Android loads the first page of listview for the first time, convertview is empty.
Minflater. Inflate (R. layout. list_item_icon_text,
Null );
This line of code instantiates a view to convertview and loads data. Each component (here, imageview and textview) needs to use the findviewbyid method to find the view control. It is tolerable to do such a job when loading the first page. However, when loading the second page, the findviewbyid method is still called and will be loaded each time in turn. It must be known that it is a memory-consuming task to search for Control Reference and load from XML. Every time you do this, the consequences are even worse.
Therefore, I have defined a static viewholder class, which is placed in the static storage area and attached the component reference to the view through the settag method. when loading the second page, you don't have to go to findviewbyid again. You just need to use the gettag method to retrieve the data reference.
This method is called View cache. This is also the optimization suggestions provided by the 2009 Google Io conference.
If you have doubts about this method, some netizens have already tested the two methods, and the other has tested the data separately, this view cache method is indeed optimized. For more information, see this article.
[Android] listviewView cache for Performance Optimization
Disadvantages of View cache --
It should be noted that this is a way to change the time of space, because a static class is added to reduce the number of times of findviewbyid. In addition, attaching data to a view using settag adds additional burden to the view, which leads to the difference between the View Size of the tag setting and the View Size without the tag setting. However, we have always been used to what others told us to do. We didn't compare it.
Another way for netizens to get through decompiling Sina Weibo is to use custom classes (inherited from relativelayout or other containers ), then, the child element findviewbyid is put into the member variable and exposed.
The Code is as follows:
Public View getview (INT position, view convertview, viewgroup parent) {// start timing long starttime = system. nanotime (); testitemlayout item; If (convertview = NULL) {item = new testitemlayout (baseadapteractivity. this);} else item = (testitemlayout) convertview; item. icon1.setimageresource (R. drawable. icon); item. text1.settext (mdata [position]); item. icon2.setimageresource (R. drawable. icon); item. text2.settext (mdata [position]); // stop timing long endtime = system. nanotime (); // long Val = (endtime-starttime)/1000l; log. E ("test", "position:" + Position + ":" + val); If (count <100) {If (Val <2000l) {sum + = val; count ++;} else MTV. settext (string. valueof (sum/100l) + ":" + nullcount); // return item;} testitemlayoutpublic class testitemlayout extends linearlayout {public textview text1; Public imageview icon1; public textview text2; Public imageview icon2; Public testitemlayout (context) {super (context); (layoutinflater) context. getsystemservice (context. layout_inflater_service )). inflate (R. layout. list_item_icon_text, this); icon1 = (imageview) findviewbyid (R. id. icon1); text1 = (textview) findviewbyid (R. id. text1); icon2 = (imageview) findviewbyid (R. id. icon2); text2 = (textview) findviewbyid (R. id. text2 );}}
My personal understanding: Does this practice use reflection? When loading the second page, item = (testitemlayout) convertview; convert the convertview type to a custom Layout container, which can convert the reference of each component? Please explain.
The final verification result of the farmer's uncle is similar to the View cache effect, but the space is optimized, and the view will not become smaller because the tag is not used.