在开发中我们经常会用到适配器,当一个项目中有很多地方使用了listView展示数据,而且数据展示都比较复杂的时候这时我们就需要写很多的适配器来展示数据,为了listview性能的提高减少了很多不必要的View的创建,我们会用ViewHolder来优化.由于适配器很多每个适配器中都要写一个ViewHolder类,这样就会显得有很多重复的代码.为了不要写很多的重复代码下面我们就自定义一个适配器
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public abstract class ComAdapter<T> extends BaseAdapter {
protected Context mContext;
protected List<T> mDatas;
private int mLayoutId;
/**
* 构造方法
*/
public ComAdapter(Context context, List<T> datas, int layoutId) {
this.mContext = context;
this.mDatas = datas;
this.mLayoutId = layoutId;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public T getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ComViewHolder holder = ComViewHolder.getViewHolder(mContext,
convertView, parent, mLayoutId, position);
convert(holder, getItem(position), position);
return holder.getConvertVeiw();
}
/**
* 子类需要实现的方法
*/
public abstract void convert(ComViewHolder holder, T t, int position);
}
公共的ViewHolder代码如下:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
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
*
* @Description 此类需要不断添加设置方法去完善,如果没有需要的方法,可以自行添加
*/
public class ComViewHolder {
/** 用于保存view的集合 */
private SparseArray<View> mViews;
/** 当前的位置 */
private int mPosition;
/** 复用view */
private View mConvertView;
/**
*
* 构造方法
*/
public ComViewHolder(Context context, ViewGroup parent, int layoutId,
int position) {
this.mPosition = position;
this.mViews = new SparseArray<View>();
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
false);
mConvertView.setTag(this);
}
/**
* 静态获取对象方法
*/
public static ComViewHolder getViewHolder(Context context,
View convertView, ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new ComViewHolder(context, parent, layoutId, position);
} else {
ComViewHolder holder = (ComViewHolder) convertView.getTag();
holder.mPosition = position;
return holder;
}
}
/**
* 通过id获取view
*/
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
/**
* 获取convertView
*/
public View getConvertVeiw() {
return mConvertView;
}
/**
* 设置textview的文本
*/
public ComViewHolder setText(int viewId, String text) {
TextView tv = getView(viewId);
if (!TextUtils.isEmpty(text)) {
tv.setText(text);
}
return this;
}
/**
* 设置imageview的图片
*/
public ComViewHolder setImageResource(int viewId, int resId) {
ImageView img = getView(viewId);
img.setImageResource(resId);
return this;
}
/**
* 设置imageview的图片
*/
public ComViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
ImageView img = getView(viewId);
img.setImageBitmap(bitmap);
return this;
}
/**
* 设置imageview的图片
*/
public ComViewHolder setImageDrawable(int viewId, Drawable drawable) {
ImageView img = getView(viewId);
img.setImageDrawable(drawable);
return this;
}
/**
* 设置imageview的图片(背景图片)
*/
public ComViewHolder setBackgroundResource(int viewId, int resId) {
ImageView img = getView(viewId);
img.setBackgroundResource(resId);
return this;
}
/**
* 设置imageview的颜色(背景颜色)
*/
public ComViewHolder setBackgroundColor(int viewId, int color) {
ImageView img = getView(viewId);
img.setBackgroundColor(color);
return this;
}
}
然后只要我们自定义适配器继承这个适配器就好了,代码如下:
public class MoveAppAdapter extends ComAdapter<AppInfo> {
private Context context;
private List<AppInfo> datas;
private ListView listView;
public void setData(List<AppInfo> datas) {
this.datas = datas;
}
public MoveAppAdapter(Context context, List<AppInfo> datas,
ListView listView) {
//加载item布局R.layout.item_move_app
super(context, datas, R.layout.item_move_app);
this.context = context;
this.datas = datas;
this.listView = listView;
}
//实现父类convert方法
@Override
public void convert(ComViewHolder holder, final AppInfo app,
final int position) {
//viewholder方法调用
holder.setImageDrawable(R.id.iv_app_icon, app.getIcon());
holder.setText(R.id.txt_app_name, app.getFilename());
holder.setText(R.id.txt_app_version, "版本号:" + app.getVersionName());
holder.setText(R.id.txt_app_size, "大 小:" + getSize(app.getSize())
+ "M");
LinearLayout uninstall = holder.getView(R.id.linean_app_uninstall);
LinearLayout operateLayout = holder.getView(R.id.llItemOperate);
RelativeLayout appItem = holder.getView(R.id.app_item);
Button open = holder.getView(R.id.ivRun);
Button manager = holder.getView(R.id.ivAppInfo);
}
}
这样比较多的适配器就可以不用写太多简单繁琐的代码了,希望对大家有所帮助.