Listview loading performance optimization viewholder

Source: Internet
Author: User
 

In Android development, listview is a very important component. It automatically displays the specific content in the form of a list based on the length of the data. You can freely define the layout of each column of listview, however, when the listview has a large amount of data to be loaded, it will occupy a large amount of memory and affect the performance. In this case, you need to fill in on-demand and re-use the view to reduce object creation.

The data loaded by listview is all carried out in the public view getview (INT position, view convertview, viewgroup parent) {} method (listadapter needs to be rewritten for custom listview: such as baseadapter, simpleadapter, the getvview method of cursoradapter). To optimize the loading speed of listview, The convertview must match the list type and use convertview to the maximum extent.

Getview can be loaded in the following ways:

The slowest loading method is to re-define a view loading layout every time and then load data.

Public View getview (INT position, view convertview, viewgroup parent ){

View Item = minflater. Inflate (R. layout. list_item_icon_text, null );

(Textview) item. findviewbyid (R. Id. Text). settext (data [position]);

(Imageview) item. findviewbyid (R. Id. Icon). setimagebitmap (

(Position & 1) = 1? Micon1: micon2 );

Return item;

}

 

The correct loading method is to directly re-use convertview when convertview is not empty, which reduces unnecessary view creation and then loads data.

Public View getview (INT position, view convertview, viewgroup parent ){

If (convertview = NULL ){

Convertview = minflater. Inflate (R. layout. Item, parent, false );

}

(Textview) convertview. findviewbyid (R. Id. Text). settext (data [position]);

(Imageview) convertview. findviewbyid (R. Id. Icon). setimagebitmap (

(Position & 1) = 1? Micon1: micon2 );

Return convertview;

}

 

The fastest way is to define a viewholder, set the tag of convetview to viewholder, and use it again when it is not empty.

Static class viewholder {

Textview text;

Imageview icon;

}

 

Public View getview (INT position, view convertview, viewgroup parent ){

Viewholder holder;

 

If (convertview = NULL ){

Convertview = minflater. Inflate (R. layout. list_item_icon_text,

Parent, false );

Holder = new viewholder ();

Holder. Text = (textview) convertview. findviewbyid (R. Id. Text );

Holder. Icon = (imageview) convertview. findviewbyid (R. Id. Icon );

Convertview. settag (holder );

} Else {

Holder = (viewholder) convertview. gettag ();

}

Holder. Text. settext (data [position]);

Holder. Icon. setimagebitmap (Position & 1) = 1? Micon1: micon2 );

Return convertview;

}

 

Three loading efficiency methods are shown in the following figure:

 

Note: The above three examples are taken from the Google 2010 I/O conference.

 

When processing time-consuming resource loading, you need to do the following to make your loading faster and smoother:

1. Modify the adapter in the main thread of the interface

2. data can be obtained anywhere, but data should be requested in another place

3. Submit the adapter changes in the main interface thread and call the yydatasetchanged () method.

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.