Improve the load efficiency of item view in Android ListView based on "Viewholder" technology

Source: Internet
Author: User

For increasing the load efficiency of the ListView child Item view that is frequently used in Android, based on two points basic considerations:

The creation of the 1,android view is more resource consuming;

2,findviewbyid () is not the quickest.

So the GetView () method in the adapter that is often used in real-world development:

Public View GetView (int position, view Convertview, viewgroup parent);

If each time in the creation of a new view out, will lead to inefficient, more efficient practice is to determine whether Convertview is empty, if not empty, then directly using the Convertview Findviewbyid () The view reconstruction can be.

However, this approach, while improving efficiency, is still not the most efficient approach, since Findviewbyid () is still used.

Thus, further improvements in efficiency are: using a technique called "viewholder", caching the View components in Convertview, using Convertview's Settag () and Gettag (), Each time the ListView needs to create the item's view, call Convertview's Gettag () to find the viewholder and reuse it. The specific use method is as shown in the code:

Package Zhangphil.listview;import Android.app.listactivity;import Android.content.context;import android.os.Bundle ; Import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.textview;public Class Mainactivity extends Listactivity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); ListView LV = This.getlistview (); Setcontentview (LV); Arrayadapter adapter = new Myadapter (this,android. r.layout.simple_list_item_2); Lv.setadapter (adapter);} Private class Myadapter extends Arrayadapter {private int resid;private layoutinflater Minflater;privateviewholder Holder = null; Public Myadapter (context context, int resource) {Super (context, resource); this.resid = Resource;minflater = Layoutinflater.from (context);} The "container" public final class of child view Viewholder {public TextView text1;public TextView Text2;} @Overridepublic int GetCount () {///huge data RetuRN 10000;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {if (Convertview = = null) {Convertview = MI  Nflater.inflate (resId, null); holder = new Viewholder (); holder.text1= (TextView) Convertview.findviewbyid (Android. R.ID.TEXT1); holder.text2= (TextView) Convertview.findviewbyid (Android. R.ID.TEXT2); Convertview.settag (holder); }elseholder= (Viewholder) Convertview.gettag () Holder.text1.setText ("Text1:" +position); Holder.text2.setText (" Text2: "+position); return Convertview;}}}



Improve the load efficiency of item view in Android ListView based on "Viewholder" technology

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.