MU Class Network Universal Adapter Learning Notes

Source: Internet
Author: User

First of all, thank you again for the technology of the network, Daniel shared the course.

A. Traditional mode of the ListView adapter: 1. Create a ListView and create the item layout for the ListView 2. Encapsulating Data 3. Create a specific adapter for the item layout for data presentation. (In this process, if there are too many controls in a ListView item, the life and invocation of the control will cause the code to rise, and if there are many places in an app that need to be used in a ListView, writing multiple adapters separately can lead to many repetitive operations, In order to avoid this situation, make coding easier, you can refer to Mu-class network Android Universal adapter writing. )。

The traditional adapter coding style is as follows:

 PackageCom.imooc.imooc_adapter;Importjava.util.List;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.TextView; Public classMyadapterextendsBaseadapter {Privatecontext Context; PrivateList<bean>mlist;  PublicMyadapter (context context, list<bean>mlist) {        Super();  This. Context =context;  This. mlist =mlist; }         PublicList<bean>getmlist () {returnmlist; }     Public voidSetmlist (list<bean>mlist) {         This. mlist =mlist; } @Override Public intGetCount () {returnmlist.size (); } @Override PublicObject GetItem (intarg0) {        returnMlist.get (arg0); } @Override Public LongGetitemid (intarg0) {        returnarg0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholer Holder=NULL; if(Convertview = =NULL) {Holder=NewViewholer (); Convertview=( (layoutinflater) context. Getsystemservice (Context.layout_inflater_service)). Inflate (R.layout.item, parent,false); Holder.name=(TextView) Convertview.findviewbyid (r.id.name); Holder.phone=(TextView) Convertview.findviewbyid (R.id.phone);        Convertview.settag (holder); }Else{convertview.settag (holder); }        FinalBean Bean =Mlist.get (position);        Holder.name.setText (Bean.getusername ());        Holder.phone.setText (Bean.getphone ()); returnConvertview; }    classviewholer{TextView name;    TextView phone; }    }

The universal adapter code is as follows:

Package Com.imooc.imooc_adapter;import Java.util.list;import Android.content.context;import Com.imooc.util.commonadapter;import Com.imooc.util.viewholder;public class Newadapter  extends CommonAdapter <bean>{public Newadapter (Context context, list<bean> Mdatas,int LayoutID) {Super (context, Mdatas,layoutid );} @Overridepublic void Convert (viewholder holder, bean Bean) {holder.settext (R.id.phone, Bean.getphone ()). SetText ( R.id.name, Bean.getusername ());}}

This shows that the universal adapter can be reduced the amount of code.

The universal adapter encapsulates user-defined Viewholder and Baseadapter declarations, and calls Commonadapter and Viewholer two classes.

Universal Adapter Authoring Logic:

1. Package Viewholder, free the view Convertview, implement a code to complete the instantiation of the Convertview:

Viewholder holder = Viewholder.get (Mcontext, Convertview, parent, postion, LayoutID);

Implementation method:

public class Viewholder {private sparsearray<view> mviews;private Context mcontext;private View Mconvertview; private int mposition;public View Getmconvertview () {return mconvertview;} public int getmposition () {return mposition;} Public Viewholder (Context context, viewgroup parent, int layoutid,int position) {this.mposition = Position;this.mviews = n EW sparsearray<view> (); Mconvertview = Layoutinflater.from (context). Inflate (LayoutID, parent,false); Mconvertview.settag (this);} public static Viewholder Get (context context, View convertview,viewgroup parent, int position, int layoutid) {if (CONVERTV Iew = = null) {return new Viewholder (context, parent, layoutid, position);} else {Viewholder holder = (viewholder) CONVERTV Iew.gettag (); holder.mposition = Position;return holder;}} Public <t extends view> T getView (int viewid) {View view = Mviews.get (Viewid); if (view = = null) {view = Mconvertview . Findviewbyid (Viewid); Mviews.put (Viewid, view);} Return (T) view;}

The use of Sparearray here is similar to the:list<int,object> form of storage, to compare hashmap<int,object> to save time and improve efficiency.

To create an abstract class Commonadapter:

Public abstract class Commonadapter<t> extends Baseadapter {protected Context mcontext;protected list<t> mdatas;protected layoutinflater minlater;private int layoutid;public commonadapter (Context context,list<t> Datas,int layoutid) {this.mcontext = context;this.mdatas= Datas;this.layoutid = Layoutid;minlater = LayoutInflater.from (context);} Public Context Getmcontext () {return mcontext;} public void Setmcontext (Context mcontext) {this.mcontext = Mcontext;} Public list<t> Getmdatas () {return mdatas;} public void Setmdatas (list<t> mdatas) {this.mdatas = Mdatas;} @Overridepublic int GetCount () {return mdatas.size ();} @Overridepublic T getItem (int arg0) {return mdatas.get (arg0);} @Overridepublic long getitemid (int arg0) {return arg0;} @Overridepublic view GetView (int postion, view Convertview, ViewGroup parent) {Viewholder holder = Viewholder.get (Mcontex T, Convertview, parent, postion, LayoutID); convert (holder, GetItem (postion)); return Holder.getmconvertview ();}; PublIC abstract void convert (Viewholder holder,t T);} 

Open the Convert method to the developer for a simple assignment operation.

Inherit the Commonadapter in the custom adapter:

public class Newadapter  extends commonadapter<bean>{public    newadapter (context context, List<bean > Mdatas,int layoutid) {        Super (context, Mdatas,layoutid);    }    @Override public    void convert (viewholder holder, bean Bean) {        holder.settext (R.id.phone, Bean.getphone ()). SetText (R.id.name, Bean.getusername ());}    }

When you initialize a custom adapter in mainactivity, you can use:

New Newadapter (Getapplicationcontext (), Mlist,r.layout.item);

Where you can encapsulate the operation of a control in a adapter in a Viewholder wrapper class

 Public Viewholder setText (int  viewId, String text) {        = GetView (viewId);        Tv.settext (text);         return  This ;    }  Public Viewholder setimageresource (int viewId,int  resId) {        ImageView img=  GetView (viewId);        Img.setimageresource (resId);         return  This ;            }

Examples are as above.

MU Class Network Universal Adapter Learning Notes

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.