今天的例子是关于在Android中实现一个万能的适配器,以减少重复的代码编写和提高代码效率。我将直接贴出代码供大家参考。
1.实现自定义的ViewHolder类:
package com.example.test.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
/**
* ViewHolder
* @author 宋小亮
*
*/
public class ViewHolder {
private SparseArray<View> mViews;//存放view
private int mPosition;//Item的position
private View mConvertview;//加载的视图View
/**
* 构造方法中对数据进行初始化并加载视图View,以及将ViewHolder实例设置成convertView的tag
* @param context
* @param parent
* @param layoutId
* @param position
* @param mInflater
*/
public ViewHolder(Context context,ViewGroup parent,int layoutId,int position,LayoutInflater mInflater){
this.mViews = new SparseArray<View>();
this.mPosition = position;
this.mConvertview = mInflater.inflate(layoutId, parent,false);
mConvertview.setTag(this);
}
/**
* 获取ViewHolder实例
* @param context
* @param convertview
* @param parent
* @param layoutId
* @param position
* @param mInflater
* @return
*/
public static ViewHolder get(Context context,View convertview,ViewGroup parent,int layoutId,int position,LayoutInflater mInflater){
if(convertview == null){
return new ViewHolder(context,parent,layoutId,position,mInflater);
}else{
ViewHolder holder = (ViewHolder)convertview.getTag();
holder.mPosition = position;
return holder;
}
}
/**
* 通过viewId获取控件
* @param resId
* @return
*/
public <T extends View> T getView(int resId){
View view = mViews.get(resId);
if(view == null){
view = mConvertview.findViewById(resId);
mViews.put(resId, view);
}
return (T) view;
}
public View getmConvertview() {
return mConvertview;
}
/**
* 设置TextView显示文本
* @param resId
* @param str
* @return
*/
public ViewHolder setText(int resId,String str){
TextView tv = (TextView)this.getView(resId);
tv.setText(str);
return this;
}
/**
* 设置ImageView显示
*/
public ViewHolder setImageResources(int resId,int rid){
ImageView iv = (ImageView)this.getView(resId);
iv.setImageResource(rid);
return this;
}
/**
* 设置ImageView显示成Bitmap
* @param resId
* @param bm
* @return
*/
public ViewHolder setImageBitmap(int resId,Bitmap bm){
ImageView iv = (ImageView)this.getView(resId);
iv.setImageBitmap(bm);
return this;
}
}
2.实现自定义的公共的Adapter父类
package com.example.test.utils;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
/**
* Adapter公共类
* @author 宋小亮
*
* @param <T>
*/
public abstract class CommonAdapter<T> extends BaseAdapter{
protected List<T> mList;
private int layoutId;
protected Context mContext;
protected LayoutInflater mInflater;
public CommonAdapter(Context context,List<T> list,int layoutId){
this.mContext = context;
this.mList = list;
this.layoutId = layoutId;
this.mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent,layoutId, position,mInflater);
//交给子类去重写该方法,实现自定义
commonView(viewHolder,mList.get(position));
return viewHolder.getmConvertview();
}
public abstract void commonView(ViewHolder viewHolder,T obj);
}
3.在我们自己的Adapter类中,我们只需要实现commonView方法:
package com.example.test;
import java.util.List;
import com.example.test.utils.CommonAdapter;
import com.example.test.utils.ViewHolder;
import android.content.Context;
/**
* 自定义Adapyer
* @author 宋小亮
*
*/
public class ListViewAdapter extends CommonAdapter<Bean>{
public ListViewAdapter(Context context, List<Bean> list) {
super(context, list,R.layout.list_content);
}
/**
* 实现该方法,在该方法中调用设置方法进行数据的显示即可
*/
@Override
public void commonView(ViewHolder viewHolder, Bean obj) {
viewHolder.setText(R.id.tv_first, obj.name).
setText(R.id.tv_second, obj.age);
}
}
至此,一个万能的适配器就大功告成!