<span style="font-family:Microsoft YaHei;font-size:12px;">在实际开发的时候,Adapter用的比较多,通常复杂点的项目都会用到BaseAdapter,当然有很多大牛都写了通用的Adapter.</span>
在GitHub上有BaseAdapter的实现:
https://github.com/JoanZapata/base-adapter-helper ;
这是CSDN博客上的实现:
http://blog.csdn.net/lmj623565791/article/details/38902805;
http://blog.csdn.net/bboyfeiyu/article/details/38947721;
以及http://stormzhang.github.io/androidtips/2014/08/31/android-common-simplebaseadapter/;
最后一个是最简单的,而且我觉得是最好的,因为需求总是不断变化的,我们需要改的地方还有很多,所以不需要面
面俱到用的时候在修改即可.
贴出最后一个实现的代码:
<span style="font-family:Microsoft YaHei;font-size:12px;">import java.util.ArrayList; import java.util.List; import android.content.Context; import android.util.SparseArray; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; public abstract class SimpleBaseAdapter<T> extends BaseAdapter { protected Context context; protected List<T> data; public SimpleBaseAdapter(Context context, List<T> data) { this.context = context; this.data = (data == null ? new ArrayList<T>(0) : new ArrayList<T>(data)); } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { if (position > data.size()) { return null; } return data.get(position); } @Override public long getItemId(int position) { return position; } /** * 该方法需要子类实现,需要返回item布局的resource id * * @return 布局的resource id */ public abstract int getItemResource(); /** * 使用该getItemView方法替换原来的getView方法,需要子类实现 * 查找控件用viewHolder的getView传入id * * @param position * @param convertView * @param parent * @param holder * @return */ public abstract View getItemView(int position, View convertView, ViewHolder holder); @SuppressWarnings("unchecked") @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (null == convertView) { convertView = View.inflate(context, getItemResource(), null); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } return getItemView(position, convertView, holder); } /** * ViewHolder可以获取控件 * @author ... * */ public class ViewHolder { private SparseArray<View> views = new SparseArray<View>(0); private View convertView; public ViewHolder(View convertView) { this.convertView = convertView; } @SuppressWarnings({ "unchecked", "hiding" }) public <T extends View> T getView(int resId) { View v = views.get(resId); if (null == v) { v = convertView.findViewById(resId); views.put(resId, v); } return (T) v; } } /** * 添加在头部 * * @param elem */ public void addFirst(List<T> elem) { data.addAll(0, elem); notifyDataSetChanged(); } /** * 添加在尾部 * * @param elem */ public void addLast(List<T> elem) { data.addAll(elem); notifyDataSetChanged(); } /** * 按位置添加 * * @param position * @param elem */ public void addByPosition(int position, List<T> elem) { data.addAll(position, elem); notifyDataSetChanged(); } /** * 根据元素移除数据 * * @param elem */ public void remove(T elem) { data.remove(elem); notifyDataSetChanged(); } /** * 根据位置移除数据 * * @param index */ public void remove(int index) { data.remove(index); notifyDataSetChanged(); } /** * 替换所有数据 * * @param elem */ public void replaceAll(List<T> elem) { data.clear(); data.addAll(elem); notifyDataSetChanged(); } }</span>使用也很简单:
<span style="font-family:Microsoft YaHei;font-size:12px;">import java.util.List; import android.content.Context; import android.view.View; import android.widget.TextView; public class MyAdapter extends SimpleBaseAdapter<String> { public MyAdapter(Context context, List<String> data) { super(context, data); } @Override public int getItemResource() { return android.R.layout.simple_list_item_1; // return R.layout.list_cell_item; } @Override public View getItemView(int position, View convertView, SimpleBaseAdapter<String>.ViewHolder holder) { // TextView t = holder.getView(R.id.tv); TextView t = holder.getView(android.R.id.text1); t.setText(getItem(position).toString()); return convertView; } }</span>这段代码应该很好懂的.