在开发过程中,会写很多的adapter类,其中很多公共的部分,不需要每次都去书写,可以为开发者省下很多时间
提取一个ListViewAdapter
public abstract class ListViewAdapter<T> extends BaseAdapter { //为了让子类访问,于是将属性设置为protected protected Context mContext; protected List<T> mDatas; protected LayoutInflater mInflater; private int layoutId; //不同的ListView的item布局肯能不同,所以要把布局单独提取出来 public ListViewAdapter(Context context, List<T> datas, int layoutId) { this.mContext = context; mInflater = LayoutInflater.from(context); this.mDatas = datas; this.layoutId = layoutId; } @Override public int getCount() { return mDatas.size(); } @Override public T getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } /** * 添加数据, * * @param aList */ public void addData(List<T> aList) { if (aList != null && aList.size() > 0) { mDatas.addAll(aList); } notifyDataSetChanged(); } /** * 获取数据 * * @return */ public List<T> getData() { return mDatas; } /** * 设置数据, * * @param sList */ public void setData(List<T> sList) { mDatas.clear(); addData(sList); } /** * 清空数据 */ public void clear() { mDatas.clear(); } @Override public View getView(int position, View convertView, ViewGroup parent) { //初始化ViewHolder,使用通用的ViewHolder,一样代码就搞定ViewHolder的初始化咯 ViewHolder holder = ViewHolder.get(mContext, convertView, parent, layoutId, position);//layoutId就是单个item的布局 convert(holder, getItem(position)); return holder.getConvertView(); //这一行的代码要注意了 } //将convert方法公布出去 public abstract void convert(ViewHolder holder, T t); }
可复用ViewHolder代码
public class ViewHolder { private SparseArray<View> mViews; private int mPosition; private View mConvertView; public ViewHolder(Context context, ViewGroup parent, int layoutId, int position) { this.mPosition = position; this.mViews = new SparseArray<View>(); mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false); mConvertView.setTag(this); } public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) { if (convertView == null) { return new ViewHolder(context, parent, layoutId, position); } else { ViewHolder holder = (ViewHolder) convertView.getTag(); holder.mPosition = position; //即时ViewHolder是复用的,但是position记得更新一下 return holder; } } /* 通过viewId获取控件 */ //使用的是泛型T,返回的是View的子类 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; } public View getConvertView() { return mConvertView; } }
再看看现在的adapter类
public class ExampleAdapter extends ListViewAdapter<String> { public MsgTemplateAdapter(Context context, List<String> datas) { super(context, datas, R.layout.template_list_item); } @Override public void convert(ViewHolder holder, String s) { ((TextView) holder.getView(R.id.name)).setText(s); } }
看到这么简洁的代码,是不是感觉整个人都精神了
1.android studio使用maven repositories { maven { url 'https://dl.bintray.com/ping/maven/' } } dependencies { ... compile 'com.github.liuping123.commadapter:commadapter:0.0.3@aar' } 2.android studio使用jcenter compile 'com.github.liuping123.commadapter:commadapter:0.0.3'
Github地址:https://github.com/liuping123/CommAdapter
参考:http://www.cnblogs.com/smyhvae/p/4477079.html
【声明】
欢迎转载,但请保留文章原始出处
博客地址:http://www.cnblogs.com/lping/
文章来源:http://www.cnblogs.com/lping/p/5470781.html