Viewholder mode ultra-concise notation

Source: Internet
Author: User
Tags phoneview

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

Related Article

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.