初級
- 優(yōu)點:復(fù)用convertView
- 缺點:每次調(diào)用getview()都會去findview,是耗費(fèi)資源的操作
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 {
//復(fù)用convertView
itemview = convertView;
}
/*-------------- 查找控件 --------------*/
TextView title = (TextView) itemview.findViewById(R.id.tv_title);
TextView desc = (TextView) itemview.findViewById(R.id.tv_desc);
/*-------------- 數(shù)據(jù)綁定 --------------*/
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);
/*-------------- 數(shù)據(jù)綁定 --------------*/
Bean bean = mList.get(position);
title.setText(bean.tilte);
desc.setText(bean.desc);
return convertView;
}
中級
- 優(yōu)點:利用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);
//創(chuàng)建對象
holder = new ViewHolder();
//設(shè)置標(biāo)記
convertView.setTag(holder);
/*-----------findview ,并存儲到holder--------*/
holder.title = (TextView) convertView.findViewById(R.id.tv_title);
holder.desc = (TextView) convertView.findViewById(R.id.tv_desc);
} else {
//根據(jù)標(biāo)記,取出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) {
//創(chuàng)建一個holder
holderView = new HolderView(mContext, R.layout.item, position, parent);
} else {
//取出holder
holderView = (HolderView) convertView.getTag();
//更新條目位置
holderView.mPosition=position;
}
/*--------------數(shù)據(jù)綁定--------------------*/
//數(shù)據(jù)
Bean bean = mList.get(position);
//設(shè)置數(shù)據(jù)
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;
//實質(zhì)上是存儲<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);
//創(chuàng)建容器
mArrays = new SparseArray<>();
//記錄條目位置
mPosition = position;
//標(biāo)記
this.mItemView.setTag(this);
}
/**
* 獲取控件
*/
public <T extends View> T getView(int viewId) {
//根據(jù)id從容器取出控件
View view = mArrays.get(viewId);
if (view == null) {
//findview
view = mItemView.findViewById(viewId);
//緩存
mArrays.put(viewId, view);
}
return (T) view;
}
/**
* 設(shè)置文本
*/
public HolderView setText(int viewId, String string) {
TextView textView = getView(viewId);
textView.setText(string);
return this;
}
//以后如果有ImageView,Button等空間,可以自行增加方法,進(jìn)行數(shù)據(jù)綁定
}
超級
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();
}
/**
* 數(shù)據(jù)和控件進(jìn)行綁定
*
* @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;
}
//構(gòu)造方法私有,禁止外部創(chuàng)建對象
//內(nèi)部創(chuàng)建對象時:進(jìn)行初始化布局等操作
private BaseViewHolder(Context context, int resId, int position, ViewGroup parent) {
//創(chuàng)建容器
this.mArrays = new SparseArray<>();
//記錄item位置
this.mPosition = position;
//初始化布局
this.mItemView = LayoutInflater.from(context).inflate(resId, parent, false);
//標(biāo)記Holder
this.mItemView.setTag(this);
}
/**
* 獲取Holder
*
* @param context 上下文
* @param layoutId 布局id
* @param position 條目的位置
* @param convertView 復(fù)用的視圖,convert(轉(zhuǎn)換的)
* @param parent 父view
* @return
*/
public static BaseViewHolder get(Context context, int layoutId,
int position, View convertView, ViewGroup parent) {
if (convertView == null) {
//返回一個新創(chuàng)建的Holder
return new BaseViewHolder(context, layoutId, position, parent);
} else {
//返回一個從標(biāo)記中取出Holder
BaseViewHolder baseHolder = (BaseViewHolder) convertView.getTag();
//更新條目位置
baseHolder.mPosition = position;
return baseHolder;
}
}
/**
* 根據(jù)控件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;
}
/**
* 設(shè)置文本
*
* @param viewId 控件Id
* @param text 文本內(nèi)容
* @return
*/
public BaseViewHolder setText(int viewId, String text) {
TextView textView = getView(viewId);
textView.setText(text);
return this;
}
//更多方法,根據(jù)條目id和數(shù)據(jù)自行創(chuàng)建...
}