Tag: Android tag
First, let's look at the use of tags.
@ Overridepublic view getview (INT position, view, viewgroup group Group) {viewholder holder = new viewholder (); If (view = NULL) {view = Inflater. inflate (R. layout. note_list_item, null); // loads the layout file of the list item. holder. title = (textview) view. findviewbyid (R. id. note_title); holder. createtime = (textview) view. findviewbyid (R. id. note_createtime); holder. clock = (imageview) view. findviewbyid (R. id. note_clock); view. settag (Holder ); // Bind the view and holder} else {holder = (viewholder) view. gettag () ;}string Title = items. get (position); Title = title. length ()> 10? Title. substring (0, 10) + "... ": title; holder. title. settext (title); holder. createtime. settext (times. get (position ));}
As you can see, this example uses a special viewholder class to save the component.
Private class viewholder {
Private textview title;
Private textview createtime;
Private imageview clock;
}
Let's take a look at the case where tags are not used:
@ Override
@Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null) { convertView = mInflater.inflate(R.layout.multiple_checkbox_main_row, null); } TextView tN = (TextView) convertView.findViewById(R.id.multiple_title); tN.setText((String)mList.get(position).get(NAME)); TextView tP = (TextView) convertView.findViewById(R.id.multiple_summary); tP.setText((String)mList.get(position).get(PHONE_NUMBER));}
You can find that the view. findviewbyid (XXX) method is called every time you do not use tags to obtain the view.
When tag is used, the view. findviewbyid (XXX) method does not need to be called because the control has been saved to the tag.
Tag is equivalent to a cache, which improves program performance.
The difference between using view. settag () in getview () in a custom adapter and not using it.