Android Development seventh my Android Application Architecture Design-----Adapter class and Viewholder class

Source: Internet
Author: User
Tags addall event listener

In Android development, for the Adapterview type of control, we need to set the data through the adapter, if the display is relatively simple, we usually use Simpleadapter or arrayadapter, and for complex and changeable data, We generally deal with customizing a adapter that inherits from Baseadapter. For Itemview reuse, we usually use a viewholder to wrap the controls inside the Itemview.

In my Android architecture, use Baseabslistadapter as the base class for all Abslistview adapters:

/** * Baseabslistadapter is the base class for all Abslistview adapters * Created by Naivor on 16-4-12. * * Public Abstract  class baseabslistadapter<T,VH extends baseviewholder <T>> extends baseadapter {    protectedContext context;protectedLayoutinflater Inflater;protectedList<t> Itemdatas;PrivateVH Viewholder; Public Baseabslistadapter(context context, Layoutinflater Inflater) { This. Context = Context; This. Inflater = Inflater; Itemdatas =NewArraylist<> (); }@Override     Public int GetCount() {returnItemdatas.size (); }@Override     PublicTGetItem(intPosition) {if(Position<getcount ()) {returnItemdatas.get (position); }return NULL; }@Override     Public Long Getitemid(intPosition) {returnPosition }@Override     PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {if(convertview==NULL) {Viewholder=oncreateviewholder (Parent,getitemviewtype (position), inflater);            Convertview=viewholder.getconvertview ();        Convertview.settag (Viewholder); }Else{viewholder= (VH) Convertview.gettag (); } viewholder.loaddatatoview (Position,getitem (position));returnConvertview; } Public AbstractVHOncreateviewholder(ViewGroup parent,intViewType, Layoutinflater inflater);/** * Determine if the data is empty */     Public Boolean IsEmpty() {returnItemdatas.isempty (); }/** * Add data * /     Public void Additems(list<t> List) {if(List! =NULL) {Itemdatas.addall (list);        Notifydatasetchanged (); }    }/** * Replace data * * @param originitem * @param newitem */     Public void Replaceitem(t Originitem, T NewItem) {if(Itemdatas.contains (Originitem)) {intPosition = Itemdatas.indexof (Originitem);            Itemdatas.remove (position);            Itemdatas.add (position, newitem);        Notifydatasetchanged (); }    }/** * Add data * /     Public void Additems(intPosition, list<t> List) {if(List! =NULL) {Itemdatas.addall (position, list);        Notifydatasetchanged (); }    }/** * Add a single data * /     Public void AddItem(T Item) {if(Item! =NULL) {Itemdatas.add (item);        Notifydatasetchanged (); }    }/** * Add a single data * /     Public void AddItem(intPosition, T Item) {if(Item! =NULL&& position >=0) {Itemdatas.add (position, item);        Notifydatasetchanged (); }    }/** * Delete data * /     Public void RemoveItem(intPosition) {if(Position >=0&& Position < GetCount ()) {itemdatas.remove (position);        Notifydatasetchanged (); }    }/** * Delete data * /     Public void RemoveItem(T data) {if(Data! =NULL&& itemdatas.contains (data) {itemdatas.remove (data);        Notifydatasetchanged (); }    }/** * Set new data, the original empty */     Public void Setitems(list<t> List) {if(List! =NULL) {itemdatas.clear ();            Itemdatas.addall (list);        Notifydatasetchanged (); }    }/** * Empty * *     Public void ClearItems() {if(!isempty ())            {itemdatas.clear ();        Notifydatasetchanged (); }    } PublicList<t>Getdatas() {returnItemdatas; }}

Baseabslistadapter mainly includes some operations on the data, such as adding one or more data, removing one or more data, replacing one piece of data, emptying the data, etc...

And Baseviewholder is the base class for all Viewholder:

/** * Baseviewholder is the base class for all Viewholder * Created by Naivor on 16-4-12. * * Public Abstract  class baseviewholder<T> {    protectedContext context;protectedBaseabslistadapter Abslistadapter;protectedView Convertview;protected intPositionprotectedT data; Public Baseviewholder(View convertview,baseabslistadapter Abslistadapter) { This. Convertview = Convertview; Context=convertview.getcontext (); This. Abslistadapter=abslistadapter; } PublicViewGetconvertview() {returnConvertview; }/** * Load ItemData to Itemview above, in actual viewholder, you need to override this method * * @param position * @param da TA * @return * *     Public   void Loaddatatoview(intPosition, T data) { This. position=position; This. Data=data; }/** * Find control * * @param viewId * @return * *     PublicViewFind(intVIEWID) {returnConvertview.findviewbyid (VIEWID); }/** * Itemview child control's Click event Listener * * /     Public Static  interface oninnerviewclicklistener<T> {         Public void OnClick(View view, T ItemData,intpostition); }}

Baseviewholder is primarily responsible for loading the ItemData data onto Itemview, and can also handle the click events of controls inside Itemview by implementing View.onclicklistener.

All right, here we go!

If there is a deeper understanding, this article will be modified;
If there is a mistake, please correct it.
If you have a better understanding, welcome to communicate.

This article is the original article, the copyright belongs to Bo Owner, reproduced please indicate the source.

More information:

My github address and the use of Demo:https://github.com/naivor/naivorapp

Android Development seventh my Android Application Architecture Design-----Adapter class and Viewholder class

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.