Objective
When developing Android apps, you often have to deal with lists, such as the ListView. There is a great loss of performance if the details are not handled properly during use.
The ListView display is content-bound through a adapter. As shown below:
1 classAdapterImplementslistadapter{2 3 @Override4 Public voidRegisterdatasetobserver (Datasetobserver observer) {5 6 }7 8 @Override9 Public voidUnregisterdatasetobserver (Datasetobserver observer) {Ten One } A - @Override - Public intGetCount () { the returnitems.size (); - } - - @Override + PublicObject GetItem (intposition) { - returnItems.get (position); + } A at @Override - Public LongGetitemid (intposition) { - returnposition; - } - - @Override in Public BooleanHasstableids () { - return false; to } + - @Override the PublicView GetView (intposition, View Convertview, ViewGroup parent) { *Viewholder VH =NULL; $ if(convertview==NULL){Panax NotoginsengVH =NewViewholder (); -Convertview = Minflater.inflate (R.layout.item,NULL); theVh.tv =(TextView) Convertview.findviewbyid (r.id.tvshow); + A Convertview.settag (VH); the}Else{ +VH =(Viewholder) Convertview.gettag (); - } $ $ Vh.tv.setText (items.get (position)); - - returnConvertview; the } - Wuyi @Override the Public intGetitemviewtype (intposition) { - return1; Wu } - About @Override $ Public intGetviewtypecount () { - return1; - } - A @Override + Public BooleanIsEmpty () { the return false; - } $ the @Override the Public Booleanareallitemsenabled () { the return false; the } - in @Override the Public BooleanIsEnabled (intposition) { the return true; About } the the}
With this adapter you can bind data to the ListView as follows:
Lv.setadapter (new Adapter ());
Getviw
In the adapter class there is an important implementation method GetView used to implement most of the logic, this is the focus of this article.
PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder VH=NULL; if(convertview==NULL) {VH=NewViewholder (); Convertview= Minflater.inflate (R.layout.item,NULL); Vh.tv=(TextView) Convertview.findviewbyid (r.id.tvshow); Convertview.settag (VH); }Else{VH=(Viewholder) Convertview.gettag (); } vh.tv.setText (Items.get (position)); returnConvertview;}
Let's take a look at the official explanation of this method.
Get a View that displays the data on the specified position in the data set.
You can either create a View manually or inflate it from an XML layout file.
This means: Get a view that shows the data in the specified location in the dataset. This view object can be obtained by code or by inflate an XML file.
Parameters (parametric)
position
The position of the item within the adapter ' s data set of the item whose view we want.
The location of the data entry in the dataset we want to show
Convertview
The old view to reuse, if possible. Note:you should check the This view is non-null and of a appropriate type before using.
If It isn't possible to convert this view to display of the correct data, this method can create a new view.
Heterogeneous lists can specify their number of view types, so then this view is always the right type (see Getviewtype Count () and Getitemviewtype (int)).
An old view that can be used. Note: Before using it, you need to check whether the view is non-empty and its type.
If this view cannot be used to properly present the data, then this method needs to create a new view.
If you have multiple view types in a list, you can also use the Getviewtypecount\getitemviewtype method correctly.
Parent
The parent, this view would eventually be attached to
The object to which this view is to be added
Convertview
The last paragraph mentions the content of Convertview's reuse (re-use), meaning that when creating a new view to return to the list, you need to check that the old View object is still available, including checking to see if it is empty, and whether the type is correct, and so on. Of course, most of the time it's available, so this way it lowers the performance overhead of the view that you create a single item .
View can be reused, and the objects in view can be re-used, such as the display text. Of course, this is the title of this article mentioned Viewholder.
Viewholder
Literally understand viewholder,a holder of the view, which is the holder of one of the views, holding the contents of all the specified contents of the view, or the binding content (which is mentioned below). Personal understanding of the meaning of this hold is like a reference or a pointer, the content in the Viewholder changes so that the corresponding view will also change, the content of the view has changed so viewholder also changed. This is the so-called hold bar. Here's a look at the binding of a viewholder.
To define a Viewholder object, the content inside the object is the content of the view that needs hold, for example, every item in the list is a textview to display the information, as follows:
Static Final class viewholder{ TextView TV;}
The XML for the item object, as follows:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "20dip" > <TextViewAndroid:layout_margintop= "5dip"Android:layout_marginbottom= "5dip"Android:id= "@+id/tvshow"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>
The TV in Viewholder is used to hold item to find the Tvshow.
Viewholder, then enter the binding process below. Or go back and look at GetView that method fragment.
Newnull= (TextView) Convertview.findviewbyid (r.id.tvshow); Convertview.settag (VH);
First the Viewholder and Convertview two objects are built, and then through vh.tv = (TextView) Convertview.findviewbyid (r.id.tvshow); are bound so that they have an association between them.
Finally through Convertview.settag (VH); The method makes the view and the viewholder associated, that is, VH really becomes the holder of the convertview view.
In this way, if you re-use this view later, you can pass the vh = (viewholder) Convertview.gettag (); method to come up with this holder, modify the contents of which can be done by the following way Vh.tv.setText (items.get (position));
This eliminates the search for Findviwybyid and lowers the overhead
Postscript:
There are many ways to optimize performance in a ListView, which is one of them.
Complete code: Viewholderdemo
Original link: http://www.cnblogs.com/luoaz/p/3734999.html