ListView Load Performance Optimization Viewholder

Source: Internet
Author: User

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.

The ListView load data is in the public View getView (int position, view Convertview, ViewGroup parent) {} method ( To customize a ListView, you need to override ListAdapter:

such as Baseadapter,simpleadapter,cursoradapter's Getvview method), optimizing the load speed of the ListView will let Convertview match the list type, And to the maximum extent of re-use 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

 Public View GetView (int  position, view Convertview, ViewGroup parent) {    null);    ((TextView) Item.findviewbyid (R.id.text)). SetText (Data[position]);     & 1) = = 1? micon1:micon2);     return item;}

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

 Public View GetView (int  position, view Convertview, ViewGroup parent) {    ifnull) {         false );    }    ((TextView) Convertview.findviewbyid (R.id.text)). SetText (Data[position]);     & 1) = = 1? micon1:micon2);     return Convertview;}

The quickest way is to define a viewholder, set the Convetview tag to Viewholder, and not be reused when empty.

Static classViewholder {TextView text; ImageView icon;} PublicView GetView (intposition, View Convertview, ViewGroup parent)    {Viewholder holder; if(Convertview = =NULL) {Convertview= Minflater.inflate (R.layout.list_item_icon_text,parent,false); Holder=NewViewholder (); 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); returnConvertview;}

Description: The above three example code excerpt from the Google I/O conference

When dealing with time-consuming resource loads, the following points need to be made to make your load faster and smoother:

1. The adapter is modified in the main thread of the interface

2. Data can be obtained from anywhere but the data should be requested in another place

3. Commit the adapter change in the thread of the main interface and call the Notifydatasetchanged () method

ListView Load Performance Optimization 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.