The difference between using View. setTag () in getView () in a custom Adapter and not using it ., Getviewview. settag

Source: Internet
Author: User

The difference between using View. setTag () in getView () in a custom Adapter and not using it ., Getviewview. settag

First, let's look at the use of tags.

@ Overridepublic View getView (int position, View view, ViewGroup 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.




How to Use the getView method of the Adapter in Android? What are the three parameters?

After your Adapter inherits BaseAdapter, it also inherits the getView method of BaseAdapter. The three parameters in it can be used as the number of items in ListView or GridView, from 0, View data is the current item.

What are the three parameters in getView () in BaseAdapter for android development?

Int position. Generally, BaseAdapter displays a lot of data of the same type on the interface. This attribute determines the number of data displayed on the interface, set the position value in an array or set customized by BaseAdapter. And displayed on the interface.
View converView is an item displayed on the interface. Because the mobile phone screen is so big, the content displayed to users at a time is fixed. If you have 1000 pieces of data in the List, new1000 converView should not be used, so the memory is definitely insufficient, you should learn how to reuse controls. converView that slides out of the screen will be re-used in the new item below, but the displayed value will be modified.

The Code is as follows:
View v = null;
If (convertView = null ){
V = LayoutInflater. from (context). inflate (
R. layout. adapter _ item, parent, false );
Holder h_= new Holder ();
Vl. text = (TextView) v. findViewById (R. id. text1 );
V. setTag (vh );
} Else {
V = convertView;
}
Holder h_= (Holder) v. getTag ();
Vl. text. setText (data [position]);
Return v;

ViewGroup parent is used to load the xml view.
Inflate (R. layout. adapter _ item, parent, false); determine its parent control to reduce the calculation of width and height

Related Article

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.