自定义adapter比较常用,很多人还在使用extends BaseAdapter,然后写一大堆重复的代码,这里是提供一个封装的工具类,把重复的代码都省略掉,让adapter变的简洁一些。
给控件设置adapter的调用代码大概是这样
mGridView.setAdapter(new CommonAdapter<GridBean>(mContext, list, R.layout.item_main_shouye_grid) {
@Override
public void convert(ViewHolder helper, GridBean item) {
helper.setText(R.id.tv_item, item.name);
helper.setImageResource(R.id.iv_item, item.icon);
ImageView imageView = helper.getView(R.id.image);
//DO your buisness with imageview
}
});
指明该adapter填充的数据类型,context,数据集data,和单项的布局文件,然后就是给各控件set值了。这样写adapter就比较简洁了。
CommonAdapter代码
package com.ab.hongyang;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* Created by wuwf on 2015-04-07.
*/
public abstract class CommonAdapter<T> extends BaseAdapter {
protected LayoutInflater mInflater;
protected Context mContext;
protected List<T> mDatas;
protected final int mItemLayoutId;
public CommonAdapter(Context context, List<T> mDatas, int itemLayoutId) {
mInflater = LayoutInflater.from(context);
this.mContext = context;
this.mDatas = mDatas;
this.mItemLayoutId = itemLayoutId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder = getViewHolder(position, convertView,
parent);
convert(viewHolder, getItem(position));
return viewHolder.getConvertView();
}
public abstract void convert(ViewHolder helper, T item);
private ViewHolder getViewHolder(int position, View convertView,
ViewGroup parent) {
return ViewHolder.get(mContext, convertView, parent, mItemLayoutId,
position);
}
@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;
}
}
ViewHolder.java
package com.ab.hongyang;
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;
import com.ab.R;
import com.ab.hongyang.imageloader.ImageLoader;
public class ViewHolder {
private final SparseArray<View> mViews;
private int mPosition;
private View mConvertView;
private Context mContext;
private ViewHolder(Context context, ViewGroup parent, int layoutId,
int position) {
mContext = context;
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 ViewHolder get(Context context, View convertView,
ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new ViewHolder(context, parent, layoutId, position);
}
return (ViewHolder) convertView.getTag();
}
/**
* 通过控件的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;
}
public View getConvertView() {
return mConvertView;
}
/**
* 为TextView设置字符串
*
* @param viewId
* @param text
* @return
*/
public ViewHolder setText(int viewId, String text) {
TextView view = getView(viewId);
view.setText(text);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param drawableId
* @return
*/
public ViewHolder setImageResource(int viewId, int drawableId) {
ImageView view = getView(viewId);
view.setImageResource(drawableId);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @return
*/
public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
ImageView view = getView(viewId);
view.setImageBitmap(bm);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param url
* @param url 是否是网络图片
* @return
*/
public ViewHolder setImageByUrl(int viewId, String url, boolean isNet) {
// AbImageLoader.getInstance(mContext).display((ImageView) getView(viewId), url);
ImageView imageview = getView(viewId);
imageview.setImageResource(R.drawable.pictures_no);
ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(url, imageview
, isNet);
return this;
}
public int getPosition() {
return mPosition;
}
}
这个类提供了一些给textview和imageview填充值的方法,setImageByUrl是展示网络图片的,这个方法里替换为你自己的加载图片框架即可。推荐使用glide。
相信CommonAdapter会给自定义adapter带来极大的方便