The role and usage of Android knowledge--viewholder

Source: Internet
Author: User

Viewholder usually appears in the adapter so that the ListView can quickly set values when scrolling, rather than having to recreate many objects each time to improve performance.

In Android development, the ListView is a very important component, it is in the form of a list of data based on the long adaptive presentation of the content, the user can freely define the layout of each column of the ListView, but when the ListView has a large amount of data to load, will occupy a lot of memory, affect performance, you need to populate and re-use view on demand to reduce the creation of objects.


{} method (to customize the ListView requires overriding ListAdapter: Getvview method such as Baseadapter,simpleadapter,cursoradapter), Optimizing the load speed of the ListView will allow Convertview to match the list type and to maximize the reuse of Convertview.
The GetView loading method generally has the following three ways:
The slowest way to load is to redefine a view load layout every time, and then load the data
1  PublicView GetView (intposition, View Convertview, viewgroup parent)2 {3View item =minflater.inflate (R.layout.list_item_icon_text,4 NULL);5 ((TextView)6 Item.findviewbyid (R.id.text)). SetText (Data[position]);7 ( (ImageView) Item.findviewbyid (R.id.icon)). Setimagebitmap (8(position & 1) = = 1?micon1:micon2);9  returnitem;Ten}


The correct way to load is when the Convertview is not empty and the convertview is reused to reduce the creation of many unnecessary view, then load the data
1  PublicView GetView (intposition, View Convertview, viewgroup parent)2 {3    if(Convertview = =NULL) {4Convertview = Minflater.inflate (R.layout.item, parent,false);5    }6 ((TextView)7 Convertview.findviewbyid (R.id.text)). SetText (Data[position]);8 ((ImageView)9 Convertview.findviewbyid (R.id.icon)). Setimagebitmap (Ten(position & 1) = = 1?micon1:micon2); One  returnConvertview; A}


The quickest way is to define a viewholder, set the Convetview tag to Viewholder, and not be reused when empty.
1 Static classViewholder {2 TextView text;3 ImageView icon;4 }5  PublicView GetView (intposition, View Convertview, viewgroup parent)6 {7 Viewholder Holder;8  if(Convertview = =NULL) {9Convertview =minflater.inflate (R.layout.list_item_icon_text,TenParentfalse); OneHolder =NewViewholder (); AHolder.text =(TextView) Convertview.findviewbyid (r.id.text); -Holder.icon =(ImageView) Convertview.findviewbyid (R.id.icon); - Convertview.settag (holder); the}Else { -Holder =(Viewholder) Convertview.gettag (); - } - Holder.text.setText (data[position]); +Holder.icon.setImageBitmap ((position & 1) = = 1?MIcon1: - mIcon2); + returnConvertview; A}

The role and usage of Android knowledge--viewholder

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.