初级
- 优点:复用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 {
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);
holder.title = (TextView) convertView.findViewById(R.id.tv_title);
holder.desc = (TextView) convertView.findViewById(R.id.tv_desc);
} else {
holder = (ViewHolder) convertView.getTag();
}
Bean bean = mList.get(position);
holder.title.setText(bean.tilte);
holder.desc.setText(bean.desc);
return convertView;
}
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;
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) {
View view = mArrays.get(viewId);
if (view == null) {
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;
}
}
超级
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) {
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<>();
this.mPosition = position;
this.mItemView = LayoutInflater.from(context).inflate(resId, parent, false);
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) {
return new BaseViewHolder(context, layoutId, position, parent);
} else {
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) {
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;
}
}