Android应用中一般都存在大量的列表展示,几乎全部用到了adapter,在adapter中每次去findviewbyid()找到view总是很长的篇幅,也很麻烦,于是就想有没有一个办法只需提供控件id和我需要做的操作,然后就能完成adapter的适配,在网上查了一些资料后终于完成了,自己总结一下:
1、首先肯定需要一个基类,来完成其所有子类的都需要完成的事情,对于adapter来说无外乎就是创建concerview,viewhodler等,关键在于getview中
public abstract class CommonAdapter<T> extends BaseAdapter {
protected final int mItemLayoutId;
protected LayoutInflater mInflater;
protected Context mContext;
protected List<T> mDatas;
private int lastPosition;
public CommonAdapter(Context context, List<T> mDatas, int itemLayoutId) {
this.mContext = context;
this.mInflater = LayoutInflater.from(mContext);
this.mDatas = mDatas;
this.mItemLayoutId = itemLayoutId;
}
@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) {
//此处创建viewhodler,Viewholder的具体代码在下一段
final CommonViewHolder viewHolder = getViewHolder(position, convertView, parent);
//此方法为抽象方法,等待子类去实现,这也就是实现数据和控件联系起来的方法
convert(viewHolder, getItem(position), position);
View view = viewHolder.getConvertView();
return view;
}
public abstract void convert(CommonViewHolder helper, T item, int position);
//此方法为抽象方法,等待子类去实现,这也就是实现数据和控件联系起来的方法
private CommonViewHolder getViewHolder(int position, View convertView,
ViewGroup parent) {
return CommonViewHolder.get(mContext, convertView, parent, mItemLayoutId,
position);
}
}
2、从上面的comadapter来看,viewholder需要实现的功能为根据layoutID返回concerview,同时再看抽象方法convert()的参数 为viewholder 和 数据源,我们实现的子类去重写这个方法实现数据和控件的匹配,我们能操作的只有viewholder和数据,那么具体如何匹配上去只能在viewholder中去实现,具体如下:
public class CommonViewHolder {
private final SparseArray<View> mViews;
private int mPosition;
private View mConvertView;
private CommonViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
this.mPosition = position;
this.mViews = new SparseArray<View>();
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
// setTag
mConvertView.setTag(this);
}
/**
* 拿到一个ViewHolder对象
*
* @param context
* @param convertView
* @param parent
* @param layoutId
* @param position
* @return
*/
public static CommonViewHolder get(Context context, View convertView,
ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new CommonViewHolder(context, parent, layoutId, position);
}
return (CommonViewHolder) convertView.getTag();
}
public View getConvertView() {
return mConvertView;
}
/**
* 通过控件的Id获取对于的控件,如果没有则加入views
*
* @param viewId
* @return
*/
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;
}
/**
* 为TextView设置字符串
*
* @param viewId
* @param text
* @return
*/
public CommonViewHolder setText(int viewId, String text) {
TextView view = getView(viewId);
view.setText(text);
return this;
}
public CommonViewHolder setText(int viewId, Spanned text) {
TextView view = getView(viewId);
view.setText(text);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param drawableId
* @return
*/
public CommonViewHolder setImageResource(int viewId, int drawableId) {
ImageView view = getView(viewId);
view.setImageResource(drawableId);
return this;
}
public CommonViewHolder setVisibility(int viewId, int visibility) {
View view = getView(viewId);
view.setVisibility(visibility);
return this;
}
public CommonViewHolder setOnClickListener(int viewId, OnClickListener onclicklistener) {
View view = getView(viewId);
view.setOnClickListener(onclicklistener);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param bm
* @return
*/
public CommonViewHolder setImageBitmap(int viewId, Bitmap bm) {
ImageView view = getView(viewId);
view.setImageBitmap(bm);
return this;
}
public int getPosition() {
return mPosition;
}
对于给控件填充数据的方法是可以继续添加的,比如加上网络加在图片等。
3、最后就是activity中实现了:
adapter = new CommonAdapter<Duanzi>(context, datas, R.layout.item_duanzi) {
@Override
public void convert(CommonViewHolder helper, Duanzi item, int position) {
helper.setText(R.id.tv_title, item.getTitle());
helper.setImageBitmap(R.id.tv_praise,bitmap);
........
}
};
listView.setAdapter(adapter);