android 学习中发现经常血药用到ListView,而且还需要自定义View显示,实现ListAdapter interface
于是写了一个通用的CommonAdapter 类,简化ListAdapter 的实现.把Adaapter 需要根据layout 动态实现的部分提取出来.做成接口.静态部分做成通用方法,以后开发就可以只关注layout 部分的绑定.简化开发.
CommonAdapter 类
View Code
public class CommonAdapter extends BaseAdapter {
private int customXmlName;
protected ViewHolderModel Holder = null;
protected final List<Map<String, Object>> list;
private final Context context;
/*
* 构造函数设置Context 和 数据源
*/
public CommonAdapter(ViewHolderModel vModel) {
customXmlName = com.gzh.search.R.layout.list_item;
this.context = vModel.context;
this.list = vModel.list;
this.Holder = vModel;
}
@Override
public int getCount() {
return list.size();
}
public void setLayout_Name(int name) {
if (name <= 0) {
return;
}
customXmlName = name;
}
@Override
public Object getItem(int position) {
if (position < list.size()) {
return list.get(position);
} else {
return null;
}
}
@Override
public long getItemId(int position) {
return position;
}
public void HolderModel(ViewHolderModel vModel) {
this.Holder = vModel;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(customXmlName,
null);
Holder.InitViewHoler(convertView);
convertView.setTag(Holder);
} else {
Holder = (ViewHolderModel) convertView.getTag();
}
Holder.SetViewHolerValues(convertView, position);
return convertView;
}
}
private int customXmlName;
protected ViewHolderModel Holder = null;
protected final List<Map<String, Object>> list;
private final Context context;
/*
* 构造函数设置Context 和 数据源
*/
public CommonAdapter(ViewHolderModel vModel) {
customXmlName = com.gzh.search.R.layout.list_item;
this.context = vModel.context;
this.list = vModel.list;
this.Holder = vModel;
}
@Override
public int getCount() {
return list.size();
}
public void setLayout_Name(int name) {
if (name <= 0) {
return;
}
customXmlName = name;
}
@Override
public Object getItem(int position) {
if (position < list.size()) {
return list.get(position);
} else {
return null;
}
}
@Override
public long getItemId(int position) {
return position;
}
public void HolderModel(ViewHolderModel vModel) {
this.Holder = vModel;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(customXmlName,
null);
Holder.InitViewHoler(convertView);
convertView.setTag(Holder);
} else {
Holder = (ViewHolderModel) convertView.getTag();
}
Holder.SetViewHolerValues(convertView, position);
return convertView;
}
}
customXmlName = com.gzh.search.R.layout.list_item :表示默认layout
ViewHolderModel layout 对应的java Holder 抽象基类
View Code
/*初始化ItemView
*设置View
*/
interface IViewHolderController {
/*
* 初始化
*/
void InitViewHoler(View convertView);
/*
* 设置显示内容
*/
void SetViewHolerValues(View convertView, int position);
}
public abstract class ViewHolderModel implements IViewHolderController {
public ViewHolderModel Holder = null;
protected List<Map<String, Object>> list;
protected Context context;
public ViewHolderModel(Context context, List<Map<String, Object>> list) {
this.context = context;
this.list = list;
}
}
*设置View
*/
interface IViewHolderController {
/*
* 初始化
*/
void InitViewHoler(View convertView);
/*
* 设置显示内容
*/
void SetViewHolerValues(View convertView, int position);
}
public abstract class ViewHolderModel implements IViewHolderController {
public ViewHolderModel Holder = null;
protected List<Map<String, Object>> list;
protected Context context;
public ViewHolderModel(Context context, List<Map<String, Object>> list) {
this.context = context;
this.list = list;
}
}
只需要实现抽象类ViewHolderModel,就完成了自定义Adapter的开发.
应用举例:
实现:ViewHolderModel 基类
public class HolderView extends ViewHolderModel {
private ImageView itemLeftImage;
private TextView itemText;
private ImageView itemRightImage;
private Button itemButton;
public HolderView(Context context, List<Map<String, Object>> list) {
super(context, list);
// TODO Auto-generated constructor stub
}
@Override
public void InitViewHoler(View convertView) {
HolderView viewHolder = (HolderView) Holder;
viewHolder.itemLeftImage = (ImageView) convertView
.findViewById(R.id.item_left);
viewHolder.itemText = (TextView) convertView
.findViewById(R.id.item_text);
viewHolder.itemRightImage = (ImageView) convertView
.findViewById(R.id.item_right);
viewHolder.itemButton = (Button) convertView.findViewById(R.id.btnOK);
}
/*
* (non-Javadoc)
*
* @see
* com.gzh.common.IViewHolderController#SetViewHolerValues(android.view.
* View, int)
*/
@Override
public void SetViewHolerValues(View convertView, int position) {
HolderView viewHolder = (HolderView) Holder;
Map<String, Object> item = list.get(position);
viewHolder.itemLeftImage.setImageResource(Integer.parseInt(item.get(
"left").toString()));
viewHolder.itemText.setText(item.get("Text").toString());
viewHolder.itemRightImage.setImageResource(Integer.parseInt(item.get(
"Right").toString()));
viewHolder.itemButton.setTag(position);
}
}
private ImageView itemLeftImage;
private TextView itemText;
private ImageView itemRightImage;
private Button itemButton;
public HolderView(Context context, List<Map<String, Object>> list) {
super(context, list);
// TODO Auto-generated constructor stub
}
@Override
public void InitViewHoler(View convertView) {
HolderView viewHolder = (HolderView) Holder;
viewHolder.itemLeftImage = (ImageView) convertView
.findViewById(R.id.item_left);
viewHolder.itemText = (TextView) convertView
.findViewById(R.id.item_text);
viewHolder.itemRightImage = (ImageView) convertView
.findViewById(R.id.item_right);
viewHolder.itemButton = (Button) convertView.findViewById(R.id.btnOK);
}
/*
* (non-Javadoc)
*
* @see
* com.gzh.common.IViewHolderController#SetViewHolerValues(android.view.
* View, int)
*/
@Override
public void SetViewHolerValues(View convertView, int position) {
HolderView viewHolder = (HolderView) Holder;
Map<String, Object> item = list.get(position);
viewHolder.itemLeftImage.setImageResource(Integer.parseInt(item.get(
"left").toString()));
viewHolder.itemText.setText(item.get("Text").toString());
viewHolder.itemRightImage.setImageResource(Integer.parseInt(item.get(
"Right").toString()));
viewHolder.itemButton.setTag(position);
}
}