一种简洁的Adapter模式实现

鄙人觉得做重复的事情就是浪费生命啊(Don’t Repeat Yourself原则), 在Android中ListView的Adapter中有大量重复工作可以被取消掉.

首先看看一种已经实现的调用方式:

1
2
3
4
5
6
7
8
9
10
11
12
adapter =
new QuickAdapter<GoodsModel>(R.layout.adapter_goods) {
@Override
public void bindView(Context context, AutoViewHolder holder, int position, GoodsDetailModel model) {
//商品缩略图
Picasso.with(NativeActivity.this).load(model.getPicThumb()).into(holder.getImageView(R.id.goods_item_thumb));
//商品的标题
holder.getTextView(R.id.goods_item_title).setText(model.getTitle());
//商品的价格
holder.getTextView(R.id.goods_item_now_price).setText(model.getPrice());
}
};

说明: GoodsModel是商品的数据模型, R.layout.adapter_goods是相应的布局文件, 然后在bindView()方法中将相应的数据和视图绑定.

以下是鄙人的实现:

QuickAdapter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.qbeenslee.base;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;
public class QuickAdapter<T> extends BaseAdapter {
private final int layoutId;
protected List<T> mData;
public QuickAdapter(int layoutId) {
this.layoutId = layoutId;
}
public QuickAdapter(int layoutId, List<T> data) {
this.layoutId = layoutId;
this.mData = data;
}
public void addData(List<T> data) {
if (data != null) {
if (this.mData == null) {
this.mData = new ArrayList<>(data.size());
}
this.mData.addAll(data);
notifyDataSetChanged();
}
}
public List<T> getData() {
return mData;
}
public void setData(List<T> data) {
if (data != null) {
if (this.mData == null) {
this.mData = data;
} else {
this.mData.clear();
this.mData.addAll(data);
}
notifyDataSetChanged();
}
}
public View getLayoutView(ViewGroup parent, int layoutRes) {
View view = null;
if (parent != null) {
Context context = parent.getContext();
if (context != null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (li != null) {
view = li.inflate(layoutRes, parent, false);
}
}
}
return view;
}
@Override
public int getCount() {
return mData == null ? 0 : mData.size();
}
@Override
public T getItem(int position) {
return mData != null && mData.size() > position && position >= 0 ? mData.get(position) : null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public String toString() {
return mData == null ? "CommonAdapter:Data Is Null" : "CommonAdapter{" + "mData=" + mData + '}';
}
public int getLayoutId() {
return layoutId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutView(parent, layoutId);
}
bindView(parent.getContext(), AutoViewHolder.getViewHolder(convertView), position, getItem(position));
return convertView;
}
public void bindView(Context context, AutoViewHolder holder, int position, T model) {
}
}

AutoViewHolder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.qbeenslee.base;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
* ViewHolder模式
*/
public class AutoViewHolder {
private final SparseArray<View> viewHolder;
private final View view;
private AutoViewHolder(View view) {
this.view = view;
viewHolder = new SparseArray<>();
view.setTag(viewHolder);
}
public static AutoViewHolder getViewHolder(View view) {
AutoViewHolder viewHolder = (AutoViewHolder) view.getTag();
if (viewHolder == null) {
viewHolder = new AutoViewHolder(view);
view.setTag(viewHolder);
}
return viewHolder;
}
public <T extends View> T get(int id) {
View childView = viewHolder.get(id);
if (childView == null) {
childView = view.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}
public View getConvertView() {
return view;
}
public TextView getTextView(int id) {
return get(id);
}
public Button getButton(int id) {
return get(id);
}
public ImageView getImageView(int id) {
return get(id);
}
public void setTextView(int id, CharSequence charSequence) {
getTextView(id).setText(charSequence);
}
}

在github上搜了一下发现基于RecyclerView的实现方案, 大家也可以参考一下QuickAdapter , SuperAdapter, CommonAdapter .

本文为原创,转载请遵守本站版权声明

更新于:2016年6月3日 21:06