传统的
1 package cct.commonadapter.bean; 2 import android.content.Context; 3 import android.view.LayoutInflater; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import android.widget.TextView; 8 import java.util.List; 9 import cct.commonadapter.R; 10 11 public class MyAdapter extends BaseAdapter { 12 private LayoutInflater inflater; 13 private List<Message> mdata; 14 public MyAdapter(Context context,List<Message> mdata){ 15 inflater=LayoutInflater.from(context); 16 this.mdata=mdata; 17 } 18 @Override 19 public int getCount() { 20 return mdata.size(); 21 } 22 @Override 23 public Object getItem(int position) { 24 return mdata.get(position); 25 } 26 @Override 27 public long getItemId(int position) { 28 return position; 29 } 30 @Override 31 public View getView(int position, View convertView, ViewGroup parent) { 32 ViewHolders holder; 33 if(convertView==null){ 34 convertView=inflater.inflate(R.layout.item,parent,false); 35 holder=new ViewHolders(); 36 holder.mtitle= (TextView) convertView.findViewById(R.id.mtitle); 37 holder.mdesc= (TextView) convertView.findViewById(R.id.mdesc); 38 holder.mtime= (TextView) convertView.findViewById(R.id.mtime); 39 holder.mphone= (TextView) convertView.findViewById(R.id.mphone); 40 convertView.setTag(holder); 41 } 42 else { 43 holder = (ViewHolders) convertView.getTag(); 44 } 45 Message ms = mdata.get(position); 46 holder.mtitle.setText(ms.getTitle()); 47 holder.mdesc.setText(ms.getDesc()); 48 holder.mtime.setText(ms.getTime()); 49 holder.mphone.setText(ms.getPhone()); 50 51 return convertView; 52 } 53 private class ViewHolders 54 { 55 TextView mtitle; 56 TextView mdesc; 57 TextView mtime; 58 TextView mphone; 59 } 60 }
附上Msessage类
1 public class Message { 2 private String title; 3 private String desc; 4 private String time; 5 private String phone; 6 public Message(int position) { 7 } 8 public Message(String title, String desc, String time, String phone) { 9 this.title = title; 10 this.desc = desc; 11 this.time = time; 12 this.phone = phone; 13 } 14 public String getDesc() { 15 return desc; 16 } 17 18 public void setDesc(String desc) { 19 this.desc = desc; 20 } 21 22 public String getTitle() { 23 return title; 24 } 25 26 public void setTitle(String title) { 27 this.title = title; 28 } 29 30 public String getTime() { 31 return time; 32 } 33 34 public void setTime(String time) { 35 this.time = time; 36 } 37 38 public String getPhone() { 39 return phone; 40 } 41 42 public void setPhone(String phone) { 43 this.phone = phone; 44 } 45 46 }
万能适配器
//CommonAdapter
1 package cct.commonadapter.adapterUtil; 2 import android.content.Context; 3 import android.view.LayoutInflater; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import java.util.List; 8 import cct.commonadapter.R; 9 10 public abstract class CommonAapter<T> extends BaseAdapter { 11 protected Context context; 12 protected LayoutInflater inflater; 13 protected List<T> mdata; 14 15 public CommonAapter(Context context, List<T> mdata) { 16 this.context=context; 17 inflater=LayoutInflater.from(context); 18 this.mdata=mdata; 19 } 20 @Override 21 public int getCount() { 22 return mdata.size(); 23 } 24 25 @Override 26 public T getItem(int position) { 27 return mdata.get(position); 28 } 29 30 @Override 31 public long getItemId(int position) { 32 return position; 33 } 34 @Override 35 public View getView(int position, View convertView, ViewGroup parent) 36 { 37 ViewHolder holder=ViewHolder.get(context,convertView,parent, R.layout.item,position); 38 covert(holder,getItem(position)); 39 return holder.getMconvertView(); 40 } 41 public abstract void covert(ViewHolder holder, T t) ; 42 43 }
通用的Viewholder
1 package cct.commonadapter.adapterUtil; 2 import android.content.Context; 3 import android.graphics.Bitmap; 4 import android.util.SparseArray; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.ImageView; 9 import android.widget.TextView; 10 11 public class ViewHolder { 12 private SparseArray<View> mViews; 13 protected int mPosition; 14 private View mConvertView; 15 private Context mContext; 16 protected int mLayoutId; 17 18 /**初始化一个ViewHolder 19 * @param context 20 * @param itemView 21 * @param parent 22 * @param position 23 */ 24 public ViewHolder(Context context, View itemView, ViewGroup parent, int position) { 25 mContext = context; 26 mConvertView = itemView; 27 mPosition = position; 28 mViews = new SparseArray<View>(); 29 mConvertView.setTag(this); 30 } 31 32 /**入口方法:用来判断页面是否加载过 33 * @param context 34 * @param convertView 35 * @param parent 36 * @param layoutId 37 * @param position 38 * @return 39 */ 40 public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) { 41 if (convertView == null) 42 { 43 View itemView = LayoutInflater.from(context).inflate(layoutId, parent, 44 false); 45 ViewHolder holder = new ViewHolder(context, itemView, parent, position); 46 holder.mLayoutId = layoutId; 47 return holder; 48 } else 49 { 50 ViewHolder holder = (ViewHolder) convertView.getTag(); 51 holder.mPosition = position; 52 return holder; 53 } 54 } 55 public View getMconvertView() { 56 return mConvertView; 57 } 58 59 /** 60 * 通过viewId获取View 61 * @param viewId 62 * @param <T> 63 * @return 64 */ 65 public <T extends View>T getView(int viewId){ 66 View view=mview.get(viewId); 67 if(view==null) 68 {view=mconvertView.findViewById(viewId); 69 mview.put(viewId,view); 70 } 71 return (T)view; 72 } 73 public int getLayoutId() 74 { 75 return mLayoutId; 76 } 77 78 public void updatePosition(int position) 79 { 80 mPosition = position; 81 } 82 83 public int getItemPosition() 84 { 85 return mPosition; 86 } 87 88 /**通过得到控件id,和类的属性名 89 * @param viewId 90 * @param text 91 * @return 92 */ 93 public ViewHolder setTexts(int viewId,String text){ 94 TextView tv=getView(viewId); 95 tv.setText(text); 96 return this; 97 } 98 public ViewHolder setImageBitmap(int viewId, Bitmap map){ 99 ImageView iv=getView(viewId); 100 iv.setImageBitmap(map); 101 return this; 102 } 103 public ViewHolder setImageResource(int viewId, int resId) 104 { 105 ImageView view = getView(viewId); 106 view.setImageResource(resId); 107 return this; 108 } 109 public ViewHolder setImageDrawable(int viewId, Drawable drawable) 110 { 111 ImageView view = getView(viewId); 112 view.setImageDrawable(drawable); 113 return this; 114 } 115 116 public ViewHolder setBackgroundColor(int viewId, int color) 117 { 118 View view = getView(viewId); 119 view.setBackgroundColor(color); 120 return this; 121 } 122 123 public ViewHolder setBackgroundRes(int viewId, int backgroundRes) 124 { 125 View view = getView(viewId); 126 view.setBackgroundResource(backgroundRes); 127 return this; 128 } 129 130 public ViewHolder setTextColor(int viewId, int textColor) 131 { 132 TextView view = getView(viewId); 133 view.setTextColor(textColor); 134 return this; 135 } 136 137 public ViewHolder setTextColorRes(int viewId, int textColorRes) 138 { 139 TextView view = getView(viewId); 140 view.setTextColor(mContext.getResources().getColor(textColorRes)); 141 return this; 142 } 143 }
任意一个适配器只需要继承上面封装好的适配器
1 package cct.commonadapter.bean; 2 import android.content.Context; 3 import android.view.LayoutInflater; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import android.widget.TextView; 8 import java.util.List; 9 import cct.commonadapter.R; 10 import cct.commonadapter.adapterUtil.CommonAapter; 11 import cct.commonadapter.adapterUtil.ViewHolder; 12 import cct.commonadapter.bean.Message; 13 14 public class MyCommonAdapter extends CommonAapter<Message> { 15 public MyCommonAdapter(Context context, List<Message> mdata){ 16 super(context,mdata); 17 } 18 19 @Override 20 public void covert(ViewHolder holder, Message ms) { 21 22 holder.setTexts(R.id.mtitle,ms.getTitle()) 23 .setTexts(R.id.mdesc,ms.getDesc()) 24 .setTexts(R.id.mtime,ms.getTime()) 25 .setTexts(R.id.mphone,ms.getPhone()); 26 } 27 }