Android's adapter encapsulation and abstraction

Source: Internet
Author: User

In the development process, often use the Viewpager, ListView, GridView, and so on, these with the Item view control, and these controls have a common point is to use their adapters, when we implement the view display, Generally will write a custom adapter to inherit Pageradapter or adapter or adapter subclass, because the Android source code comes with these adapters are more abstract, often in our development, Some of the custom adapters you write need to override some of the methods of the adapter parent class, and when overridden, there are many more general-purpose code, compared to the coupling, which encapsulates and abstracts Pageradapter and adapter subclasses Baseadapter. Eliminate every time to rewrite these common code, the code is as follows:

1.viewpageradapter

/** * Universal VIEWPAGERADAPT ER * @author jenly * */public class Viewpageradapter extends Pageradapter {private list<view> listviews = Null;publi C Viewpageradapter (list<view> listviews) {this.listviews = ListViews;} @Overridepublic void Destroyitem (View container, int position, object object) {(Viewpager) container). Removeview ( Listviews.get (position));} @Overridepublic int GetCount () {return listviews.size ();} @Overridepublic Object Instantiateitem (View container, int position) {((Viewpager) container). AddView (Listviews.get ( Position), 0); return listviews.get (position);} @Overridepublic boolean isviewfromobject (View Paramview, Object paramobject) {return Paramview = = Paramobject;} Public list<view> Getlistviews () {return listviews;} public void Setlistviews (list<view> listviews) {this.listviews = ListViews;}} 
Because Viewpager's item is basically inherited view, this viewpageradapter is basically a generic adapter for Viewpager controls.


2.abstractadapter

/** * Abstract Adapter (eliminates some common code) * @author jenly * * @param <T> */public abstract class Abstractadapter<t> extends Base Adapter {protected Context context;protected list<t> listdata;protected layoutinflater layoutinflater;public Abstractadapter (Context context,list<t> listData) {this.context = Context;this.listdata = ListData; Layoutinflater = Layoutinflater.from (context);} @Overridepublic int GetCount () {if (listdata!=null) {return listdata.size ();} return 0;} @Overridepublic Object getItem (int position) {if (listdata!=null) {return listdata.get (position);} return null;} @Overridepublic long Getitemid (int position) {return position;} Public list<t> Getlistdata () {return listData;} public void Setlistdata (list<t> listData) {this.listdata = ListData;}}

Abstractadapter<t> the parent class to be overridden by a common method, as long as the abstractadapter<t&gt is inherited by the custom adapter, the GetView method can be overridden.


3.Holderadapter

/** * Universal Adapter (for some conventional adapters) * @author jenly * * @param <T> */public abstract class Holderadapter<t> extends Abstra Ctadapter<t>{public Holderadapter (Context context, list<t> ListData) {Super (context, listData);} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Object holder = null;if (convertview==null {Convertview = Buildconvertview (layoutinflater); holder = Buildholder (Convertview); Convertview.settag (holder);} Else{holder = Convertview.gettag ();} Bindviewdata (Holder,listdata.get (position), position); return Convertview;} /** * Established Convertview * @param layoutinflater * @return */public abstract View buildconvertview (Layoutinflater layoutinflate R);/** * Build View Holder * @param convertview * @return */public abstract Object buildholder (view convertview);/** * Bind data * @pa RAM Holder * @param T * @param position */public abstract void Bindviewdatas (Object holder,t t,int position);}

Holderadapter inherits from the Abstractadapter class above, the GetView method is rewritten and abstracted, making the code more concise and easier to use, as long as it is a custom adapter class that inherits from Baseadapter. Instead of inheriting from Holderadapter basically generic, then just implement Buildconvertview, Buildholder, Bindviewdatas three methods.


Here is a custom test adapter that inherits the three abstract methods that Holderadapter implements it:

public class Testholderadapter extends Holderadapter<string>{public testholderadapter (context context, list< String> listData) {Super (context, listData);} @Overridepublic View Buildconvertview (Layoutinflater layoutinflater) {return layoutinflater.inflate ( R.layout.activity_list_item, null);} @Overridepublic Object Buildholder (View convertview) {Viewholder holder = new Viewholder (); holder.tv = (TextView) Convertview.findviewbyid (r.id.tv); return holder;} @Overridepublic void Bindviewdatas (Object holder, String t, int position) {(viewholder) holder). Tv.settext (t);} private static Class Viewholder{textview TV;}}





Android's adapter encapsulation and abstraction

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.