上一篇文章 Android:BaseListAdapter——提高开发效率的BaseAdapter,介绍了使用保留ViewHolder的方式实现BaseAdapter,但是显得有些臃肿,接下来介绍摒弃ViewHolder的方式封装BaseAdapter。
1、BaseListAdapter
/**
* Created by HMY on 2016/3/2.
*/
public abstract class BaseListAdapter<T> extends BaseAdapter {
protected Context mContext;
protected List<T> mList;
protected ListView mListView;
protected LayoutInflater inflater;
public BaseListAdapter(Context context) {
this.mContext = context;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return mList == null ? 0 : mList.size();
}
@Override
public T getItem(int position) {
return mList == null ? null : mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("unchecked")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = inflater.inflate(getLayoutResource(), parent, false);
}
onBindView(position, convertView, this.getItem(position));
return convertView;
}
public abstract int getLayoutResource();
public abstract void onBindView(int position, View convertView, T t);
@SuppressWarnings("unchecked")
public <E extends View> E findView(View view, int id) {
SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<>();
view.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
childView = view.findViewById(id);
viewHolder.put(id, childView);
}
return (E) childView;
}
public void setList(List<T> list) {
this.mList = list;
}
public List<T> getList() {
return mList;
}
public void setList(T[] list) {
ArrayList<T> arrayList = new ArrayList<>(list.length);
for (T t : list) {
arrayList.add(t);
}
setList(arrayList);
}
public ListView getListView() {
return mListView;
}
public void setListView(ListView listView) {
mListView = listView;
}
public void add(T t) {
this.mList.add(t);
notifyDataSetChanged();
}
public void addAll(List<T> list) {
this.mList.addAll(list);
notifyDataSetChanged();
}
public void remove(int position) {
this.mList.remove(position);
notifyDataSetChanged();
}
public void clear() {
this.mList.clear();
notifyDataSetChanged();
}
}
这里面去掉了onCreateViewHolder(View convertView) 方法,将onBindViewHolder(int position, VH holder)替换为onBindView(int position, View convertView)。
同时增加了findView(View view, int id)方法,正是这个方法替代了ViewHolder的方式,代码如下:
@SuppressWarnings("unchecked")
public <E extends View> E findView(View view, int id) {
SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<>();
view.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
childView = view.findViewById(id);
viewHolder.put(id, childView);
}
return (E) childView;
}
BaseListTestAdapter2:
/**
* Created by HMY on 2016/3/2.
*/
public class BaseListTestAdapter2 extends BaseListAdapter<BastListAdapterTestModel> {
public BaseListTestAdapter2(Context context) {
super(context);
}
@Override
public int getLayoutResource() {
return R.layout.item_adapter;
}
@Override
public void onBindView(int position, View convertView) {
BastListAdapterTestModel model = mList.get(position);
ImageView iconIv = findView(convertView, R.id.iv_icon);
TextView textTv = findView(convertView, R.id.tv_text);
iconIv.setImageResource(model.resid);
textTv.setText(model.text);
}
可见,对比 上一篇文章,现在的代码量更少。两者的差异除了代码量与结构,就是当前这个的findViewById需要在bindView里面写,而我习惯将两者分开
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
xml文件和activity实现也请看上一篇文章,没有任何变化。