public class FirstpageGridAdapter extends BaseAdapter {
/* gridview数据 */
private String[] choices = new String[] { "新房", "二手房", "租房", "资讯", "打折优惠",
"最新开盘", "房贷计算", "更多" };
private int[] images = new int[] { R.drawable.selector_xinfang,
R.drawable.selector_ershou, R.drawable.selector_zufang,
R.drawable.selector_zixun, R.drawable.selector_youhui,
R.drawable.selector_kaipan, R.drawable.selector_calculator,
R.drawable.selector_more };
private LayoutInflater layoutInflater;
private onClickChildIbListener listener;
public FirstpageGridAdapter(Context context, onClickChildIbListener listener) {
super();
this.layoutInflater = LayoutInflater.from(context);
this.listener = listener;
}
public interface onClickChildIbListener {
public void clickChild(int pos,View view);
}
public int getCount() {
return choices.length;
}
public Object getItem(int position) {
return choices[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.item_main_grid,
parent, false);
holder.ib = (ImageButton) convertView
.findViewById(R.id.item_main_grid_ib);
holder.tv = (TextView) convertView
.findViewById(R.id.item_main_grid_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ib.setBackgroundResource(images[position]);
holder.tv.setText(choices[position]);
final int pos = position;
holder.ib.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listener.clickChild(pos,v);
}
});
return convertView;
}
private class ViewHolder {
ImageButton ib;
TextView tv;
}
}
private SparseArray cacheViews;
@Override
public View getView(int resId) {
View v = cacheViews.get(resId);
if (v == null) {
v = itemView.findViewById(resId);
if (v != null) {
cacheViews.put(resId, v);
}
}
return v;
}
/**
*
* @author chendong
* 用来实现复用加载的单类型ViewHolder
*
*/
public static class SingleViewHolder{
/**
* 使用SparseArray
*/
private SparseArray cacheViews;
private View itemView;
public SingleViewHolder(View itemView, int viewCount) {
super();
this.itemView = itemView;
cacheViews = new SparseArray(viewCount);
}
@Override
public View getView(int resId) {
View v = cacheViews.get(resId);
if (v == null) {
v = itemView.findViewById(resId);
if (v != null) {
cacheViews.put(resId, v);
}
}
return v;
}
}
/**
1. 抽象适配器,使用了模板方法模式,将设置item显示内容的部分抽象到了类外 这是单类型的抽象适配
2.
3. @author chendong
4.
5. @param
6. 泛型
*/
public abstract class SingleEasyAdapter<T> extends BaseAdapter {
private LayoutInflater layoutInflater;
private int resId;
private List datas;
private int viewCount = 5;
/**
* @param context
* 上下文对象,建议使用getApplicationContext();
* @param resId
* item布局id
* @param datas
* 数据集
* @param viewCount
* item中的view个数,用来优化SparseArray
*/
public SingleEasyAdapter(Context context, int resId, List datas,
int viewCount) {
super();
this.layoutInflater = LayoutInflater.from(context);
this.resId = resId;
this.datas = datas;
this.viewCount = viewCount;
}
public int getCount() {
return datas.size();
}
public Object getItem(int position) {
return datas.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = layoutInflater.inflate(resId, parent, false);
holder = new ViewHolder(convertView, viewCount);
convertView.setTag(holder);
//在这里绑定监听,避免重复绑定。
bindListener4View(holder, datas.get(position), position);
} else {
holder = (ViewHolder) convertView.getTag();
}
bindData4View(holder, datas.get(position), position);
return convertView;
}
/**
* 绑定数据
*
* @param holder
* @param data
*/
public abstract void bindData4View(ViewHolder holder, T data, int pos);
/**
* 绑定监听
*
* @param holder
* @param pos
*/
public abstract void bindListener4View(ViewHolder holder, T data, int pos);
}
1.父类的getView()方法中使用了一个抽象方法,使用了一个设计模式模板方法模式,将方法的实现推迟到了子类中
2.泛型,传递的数据类型是不确定的,使用泛型可以解决这个问题,泛型的使用不了解的建议搜一下,后面深入的介绍会使用更复杂的泛型
/**
* 抽象适配器,使用了模板方法模式,将设置item显示内容的部分抽象到了类外 这是单类型的抽象适配
*
* @param 泛型
* @author chendong
*/
public abstract class SingleEasyAdapter<T> extends BaseAdapter {
private LayoutInflater layoutInflater;
private int resId;
private List datas;
private int viewCount = 5;
private Context context;
/**
* @param context 上下文对象,建议使用getApplicationContext();
* @param resId item布局id
* @param datas 数据集
* @param viewCount item中的view个数,用来优化SparseArray
*/
public SingleEasyAdapter(Context context, int resId, List datas,
int viewCount) {
super();
this.layoutInflater = LayoutInflater.from(context);
this.resId = resId;
this.datas = datas;
this.viewCount = viewCount;
this.context = context;
}
public Context getContext() {
return this.context;
}
public List getData() {
return datas;
}
public void swapData(List datas) {
this.datas = datas;
notifyDataSetChanged();
}
public int getCount() {
return datas.size();
}
public Object getItem(int position) {
return datas.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = layoutInflater.inflate(resId, parent, false);
holder = new ViewHolder(convertView, viewCount);
convertView.setTag(holder);
//在这里绑定监听
bindListener4View(holder, datas.get(position), position);
} else {
holder = (ViewHolder) convertView.getTag();
}
bindData4View(holder, datas.get(position), position);
return convertView;
}
/**
* 绑定数据
*
* @param holder
* @param data
*/
public abstract void bindData4View(ViewHolder holder, T data, int pos);
/**
* 绑定监听
*
* @param holder
* @param pos
*/
public abstract void bindListener4View(ViewHolder holder, T data, int pos);
}
listview = (ListView) findViewById(R.id.listview);
list = new ArrayList();
for (int i = 0; i < 20; i++) {
if (i % 5 == 0)
list.add(new Student("name" + i, "age" + i, "sex", 3));
else
list.add(new Student("name" + i, "age" + i, "sex",
i % 2 == 0 ? 1 : 2));
}
listview.setAdapter(new SingleEasyAdapter(
getApplicationContext(), R.layout.item_type1, list, 4) {
@Override
public void bindData4View(SingleViewHolder holder, Student data,int pos) {
((TextView)holder.getView(R.id.tv_name)).setText(data.getName());
//此处省略若干代码
}
@Override
public void bindListener4View(SingleViewHolder holder, Student data,int pos) {
//此处省略若干代码
}
});
可以看到我们只用了很少的代码就完成了功能,而且很清晰,简单地适配不需要创建适配器类了,使用匿名的也很快就可以实现。演示有点丑,基本都实现了。