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