background
For ListView, GridView, Gallery, spinner, and so on, it is their adapter, directly inherit from the interface class adapter, use Baseadapter need to rewrite a lot of methods, the most important of which is GetView , because this involves problems such as ListView optimization , Baseadapter is somewhat different from other adapter, and other adapter can set the data directly in its construction method:
New Simpleadapter (Thisnew string[]{"img", "title", "Info",newint[]{r.id.img, R.id.title, R.id.info}});
However, in baseadapter you need to implement a class that inherits from Baseadapter and overrides many of the methods inside it:
classMyadapterextendsBaseadapter {Privatecontext Context; PublicMyadapter (Context context) { This. Context =context; } @Override Public intGetCount () {//How many items is in the data set represented by this Adapter. (Number of entries in the data set represented in this adapter) return0; } @Override PublicObject GetItem (intposition) { //Get The data item associated with the specified position in the data set. (Gets the data item that corresponds to the specified index in the data set) return NULL; } @Override Public LongGetitemid (intposition) { //Get the row ID associated with the specified position in the list. (The row ID that corresponds to the specified index in the list) return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { //Get A View that displays the data on the specified position in the data set. return NULL; } }there is no processing, it is not recommended to write this.
If the amount of data is low, but if the list item data is very large, the view will be recreated each time, set the resources, severely affect performance, so from the beginning do not use this way.
@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {View Item= Minflater.inflate (R.layout.list_item,NULL); ImageView img=(ImageView) Item.findviewbyid (r.id.img) TextView title=(TextView) Item.findviewbyid (r.id.title); TextView Info=(TextView) Item.findviewbyid (r.id.info); Img.setimageresource (R.drawable.ic_launcher); Title.settext ("Hello"); Info.settext ("World"); returnitem; }ListView Optimization
By caching Convertview, this method of caching Contentview can be used to determine if a view is created in the cache that does not exist, and if it already exists, it can improve performance by taking advantage of the view in the cache.
PublicView GetView (intposition, View Convertview, ViewGroup parent) { if(Convertview = =NULL) {Convertview= Minflater.inflate (R.layout.list_item,NULL); } ImageView img=(ImageView) Convertview.findviewbyid (r.id.img) TextView title=(TextView) Convertview.findviewbyid (r.id.title); TextView Info=(TextView) Convertview.findviewbyid (r.id.info); Img.setimageresource (R.drawable.ic_launcher); Title.settext ("Hello"); Info.settext ("World"); returnConvertview; }ListView re-optimization
Implemented by Convertview+viewholder ,Viewholder is a static class, and the key benefit of using Viewholder is to cache the view that displays the data, which speeds up the responsiveness of the UI.
When we judge Convertview = = NULL, if it is empty, it will be assigned to Convertview according to the item layout (XML) of the list that was designed. and generate a viewholder to bind the individual view controls inside the Converview (those controls inside the XML layout). Then use the Convertview Settag to set the Viewholder to tag so that the system draws the ListView from the tag the second time it is drawn. (See the code below)
If Convertview is not empty, it will use Convertview's Gettag () to obtain a viewholder.
//defined outside, Viewholder static class Static classViewholder { PublicImageView img; PublicTextView title; PublicTextView Info; }//then rewrite GetView@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder holder; if(Convertview = =NULL) {Holder=NewViewholder (); Convertview= Minflater.inflate (R.layout.list_item,NULL); Holder.img=(ImageView) Item.findviewbyid (r.id.img) Holder.title=(TextView) Item.findviewbyid (r.id.title); Holder.info=(TextView) Item.findviewbyid (r.id.info); Convertview.settag (holder); }Else{Holder=(Viewholder) Convertview.gettag (); Holder.img.setImageResource (R.drawable.ic_launcher); Holder.title.setText ("Hello"); Holder.info.setText ("World"); } returnConvertview; }
To work efficiently the adapter implemented here uses the techniques:
-it reuses the Convertview passed to GetView () to avoid inflating View when It's not necessary
(Translate: Reuse cache Convertview pass to GetView () method to avoid populating unnecessary views)
-it uses the Viewholder pattern to avoid calling Findviewbyid () when It's not necessary
- (Translate: Use Viewholder mode to avoid unnecessary calls to Findviewbyid (): Because too many Findviewbyid also affect performance)
The role of the Viewholder class
-the Viewholder pattern consists in storing a data structure in the tag of the view returned by GetView (). This data structures contains references to the views we want to bind data to, thus avoiding calling to Findviewbyid () Eve Ry Time GetView () is invoked
(translation: Viewholder mode stores a data structure in the label (tag) of the view returned by the GetView () method, which contains a reference to the view we want to bind the data to, thus avoiding the call to GetView each time the call is made () Findviewbyid ())
I'm the dividing line of the king of the land Tiger.