Android ListView Universal Adapter

Source: Internet
Author: User

Reference Https://github.com/hongyangAndroid/base-adapter

public class Commonviewholder {

Private context context;
private int position;
private int layoutid;

Private View Convertview;
Private sparsearray<view> views;

Private Commonviewholder (context context, viewgroup parent, int layoutid, int position) {
This.context = context;
This.layoutid = LayoutID;
This.position = position;
views = new sparsearray<> ();
Convertview = view.inflate (context, layoutid, null);
Convertview.settag (this);
}

public static Commonviewholder getinstance (context context, viewgroup parent, int resId, int position, View Convertview) {
if (Convertview = = null) {
return new Commonviewholder (context, parent, resId, position);
} else {
Commonviewholder holder = (commonviewholder) convertview.gettag ();
Holder.position = position;
return holder;
}
}

public int getPosition () {return position; }

Public View Getconvertview () {return convertview; }

Public <t extends view> T getView (int viewId) {
View view = Views.get (viewId);
if (view = = null) {
View = This.convertView.findViewById (viewId);
Views.put (viewId, view);
}
Return (T) view;
}

Public Commonviewholder setText (int viewId, String text) {
((TextView) Views.get (viewId)). SetText (text);
return this;
}

Public Commonviewholder setimageresource (int viewId, int resId) {
((ImageView) Views.get (viewId)). Setimageresource (ResId);
return this;
}

Public Commonviewholder setonclicklistener (int viewId, View.onclicklistener onclicklistener) {
Views.get (viewId). Setonclicklistener (Onclicklistener);
return this;
}
}

Public abstract classCommonadapter<T> extends Baseadapter {
Private list<t> datas;
Private context context;
private int layoutid;

Public Commonadapter (context context, list<t> datas, int layoutid) {
Super ();
This.datas = datas;
This.context = context;
This.layoutid = LayoutID;
}

@Override
public int GetCount () {return datas = = null? 0:datas.size ();}

@Override
Public T getItem (int position) {return datas.get (position);}

@Override
public long getitemid (int position) {return position;}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Commonviewholder Holder = commonviewholder.getinstance (context, parent, layoutid, position, Convertview);
CONVERT (holder, datas.get (position));
return Holder.getconvertview ();
}

public abstract void Convert (Commonviewholder holder, T T);
}

Android ListView Universal Adapter

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.