初级
- 优点:复用convertView
- 缺点:每次调用getview()都会去findview,是耗费资源的操作
public class MyAdapter extends BaseAdapter {
protected List<Bean> mList;
protected Context mContext;
LayoutInflater mInflater;
public MyAdapter(Context context, List<Bean> list) {
this.mList = list;
this.mContext = context;
mInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
/*----------视图操作--------------*/
View itemview = null;
if (convertView == null) {
//初始化布局
itemview = mInflater.inflate(R.layout.item, parent, false);
} else {
//复用convertView
itemview = convertView;
}
/*-------------- 查找控件 --------------*/
TextView title = (TextView) itemview.findViewById(R.id.tv_title);
TextView desc = (TextView) itemview.findViewById(R.id.tv_desc);
/*-------------- 数据绑定 --------------*/
Bean bean = mList.get(position);
title.setText(bean.tilte);
desc.setText(bean.desc);
return itemview;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
/*----------视图操作--------------*/
if (convertView == null) {
//convertView为空,
convertView = mInflater.inflate(R.layout.item, parent, false);
}
/*-------------- 查找控件 --------------*/
TextView title = (TextView) convertView.findViewById(R.id.tv_title);
TextView desc = (TextView) convertView.findViewById(R.id.tv_desc);
/*-------------- 数据绑定 --------------*/
Bean bean = mList.get(position);
title.setText(bean.tilte);
desc.setText(bean.desc);
return convertView;
}
中级
- 优点:利用ViewHolder存储找到的控件,避免多次findViewById
- 缺点:代码量大
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
/*----------视图操作--------------*/
if (convertView == null) {
//解析布局文件
convertView = mInflater.inflate(R.layout.item, parent, false);
//创建对象
holder = new ViewHolder();
//设置标记
convertView.setTag(holder);
/*-----------findview ,并存储到holder--------*/
holder.title = (TextView) convertView.findViewById(R.id.tv_title);
holder.desc = (TextView) convertView.findViewById(R.id.tv_desc);
} else {
//根据标记,取出holder
holder = (ViewHolder) convertView.getTag();
}
/*-------------- 赋值 --------------*/
Bean bean = mList.get(position);
holder.title.setText(bean.tilte);
holder.desc.setText(bean.desc);
return convertView;
}
//存放控件,避免多次findViewById
class ViewHolder{
TextView title;
TextView desc ;
}
高级
public View getView(int position, View convertView, ViewGroup parent) {
HolderView holderView = null;
/*--------------视图操作--------------------*/
if (convertView == null) {
//创建一个holder
holderView = new HolderView(mContext, R.layout.item, position, parent);
} else {
//取出holder
holderView = (HolderView) convertView.getTag();
//更新条目位置
holderView.mPosition=position;
}
/*--------------数据绑定--------------------*/
//数据
Bean bean = mList.get(position);
//设置数据
holderView.setText(R.id.tv_commond, bean.commond)
.setText(R.id.tv_title, bean.tilte)
.setText(R.id.tv_desc, bean.desc)
.setText(R.id.tv_time, bean.time);
return holderView.mItemView;
}
class HolderView {
//条目视图
View mItemView;
//条目位置
int mPosition;
//实质上是存储<Integer,Object>的HashMap,效率高于HashMap
SparseArray<View> mArrays;
public HolderView(Context context,int layoutId,int position,ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(context);
//初始化布局
this.mItemView =inflater.inflate(layoutId, parent, false);
//创建容器
mArrays = new SparseArray<>();
//记录条目位置
mPosition = position;
//标记
this.mItemView.setTag(this);
}
/**
* 获取控件
*/
public <T extends View> T getView(int viewId) {
//根据id从容器取出控件
View view = mArrays.get(viewId);
if (view == null) {
//findview
view = mItemView.findViewById(viewId);
//缓存
mArrays.put(viewId, view);
}
return (T) view;
}
/**
* 设置文本
*/
public HolderView setText(int viewId, String string) {
TextView textView = getView(viewId);
textView.setText(string);
return this;
}
//以后如果有ImageView,Button等空间,可以自行增加方法,进行数据绑定
}
超级
public abstract class CommonAdapter<T> extends BaseAdapter {
protected List<T> mList;
protected Context mContext;
/**
* 布局文件id
*/
protected int mLayoutId;
public CommonAdapter(Context context, int layoutId, List<T> list) {
this.mList = list;
this.mLayoutId = layoutId;
this.mContext = context;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//找到holder
BaseViewHolder holder = BaseViewHolder.get(mContext, mLayoutId, position, convertView, parent);
T bean = mList.get(position);
bindData(bean, holder);
return holder.getItemView();
}
/**
* 数据和控件进行绑定
*
* @param bean
* @param holder
*/
protected abstract void bindData(T bean, BaseViewHolder holder);
}
public class BaseViewHolder {
/**
* 泛型为<Integer,Object>的HashMap,效率比普通的HashMap高一些
* 存储子控件
*/
private SparseArray<View> mArrays;
/**
* 条目位置
*/
private int mPosition;
/**
* 条目视图View
*/
private View mItemView;
/**
* 获取条目位置
*/
public int getPosition() {
return mPosition;
}
/**
* 获取条目视图
*/
public View getItemView() {
return mItemView;
}
//构造方法私有,禁止外部创建对象
//内部创建对象时:进行初始化布局等操作
private BaseViewHolder(Context context, int resId, int position, ViewGroup parent) {
//创建容器
this.mArrays = new SparseArray<>();
//记录item位置
this.mPosition = position;
//初始化布局
this.mItemView = LayoutInflater.from(context).inflate(resId, parent, false);
//标记Holder
this.mItemView.setTag(this);
}
/**
* 获取Holder
*
* @param context 上下文
* @param layoutId 布局id
* @param position 条目的位置
* @param convertView 复用的视图,convert(转换的)
* @param parent 父view
* @return
*/
public static BaseViewHolder get(Context context, int layoutId,
int position, View convertView, ViewGroup parent) {
if (convertView == null) {
//返回一个新创建的Holder
return new BaseViewHolder(context, layoutId, position, parent);
} else {
//返回一个从标记中取出Holder
BaseViewHolder baseHolder = (BaseViewHolder) convertView.getTag();
//更新条目位置
baseHolder.mPosition = position;
return baseHolder;
}
}
/**
* 根据控件Id找到控件
*
* @param viewId 控件Id
* @param <T>
* @return
*/
public <T extends View> T getView(int viewId) {
//从容器中取出控件
View view = mArrays.get(viewId);
//控件为空
if (view == null) {
//findview
view = mItemView.findViewById(viewId);
//缓存
mArrays.put(viewId, view);
}
return (T) view;
}
/**
* 设置文本
*
* @param viewId 控件Id
* @param text 文本内容
* @return
*/
public BaseViewHolder setText(int viewId, String text) {
TextView textView = getView(viewId);
textView.setText(text);
return this;
}
//更多方法,根据条目id和数据自行创建...
}