Viewholder is not explained by what it is.
How do you usually write Viewholder?
Viewholder holder =NULL; if(Convertview = =NULL) {Convertview= Minflater.inflate (r.layout.xxxNULL); Holder=NewViewholder (); Holder.tvxxx=(TextView) Findviewbyid (r.id.xxx); //... A series of Findviewbyid } Else{Holder=(Viewholder) Convertview.gettag (); } Private Static classviewholder{TextView tvxxx; //Many definitions of view}
So write once is OK, but the problem is always have a lot of viewadapter to write so, every time repeat,repeat,repeat tired ah. So, there is such a concise way to share to everyone, first of all, from the foreign web site, not their original, but really like this concise design.
Viewholder This (only provides a static method, in fact, you can add a private constructor to prevent external instantiation), the code is very simple, read it and understand
Public classViewholder {//I added a generic return type to reduce the casting noise in client code@SuppressWarnings ("unchecked") Public StaticTGet(View view,intID) {Sparsearray viewholder=(Sparsearray) View.gettag (); if(Viewholder = =NULL) {Viewholder=NewSparsearray (); View.settag (Viewholder); } View Childview= Viewholder.Get(ID); if(Childview = =NULL) {Childview=View.findviewbyid (ID); Viewholder.put (ID, Childview); } return(T) Childview; }}
It's in GetView.
@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { if(Convertview = =NULL) {Convertview= Layoutinflater. from(context). Inflate (R.layout.banana_phone, parent,false); } ImageView Bananaview= Viewholder.Get(Convertview, R.id.banana); TextView Phoneview= Viewholder.Get(Convertview, R.id.phone); Bananaphone Bananaphone=GetItem (position); Phoneview.settext (Bananaphone.getphone ()); Bananaview.setimageresource (Bananaphone.getbanana ()); returnConvertview;}
Haha, completed, decisively put this viewholder class to join their own utils, once and for all after ~
It is noteworthy that sparsearray this knowledge point, optimized storage of integer and object key value pairs of hashmap, on-line information many here will not nonsense ~
Source: http://www.eoeandroid.com/thread-321547-1-1.html
Some people say that the simple way to rewrite adapter is to rewrite the BindView method using Simpleadapter or Simplecursoradapter. In what way do you decide for yourself according to the actual situation AH smile
Viewholder mode ultra-concise notation