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;
}
}
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;
}
}
只需要實現抽象類別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);
}
}