抽取baseAdapter基类
- 构造方法
public BaseAdapter(Context context, @LayoutRes int layout , List<T> List){
mContext=context;
mList = List;
mLayout = layout;
notifyDataSetChanged();
}
layout xml视图 list后台接口数据,
- 成员变量
private Context mContext;
private List<T> mList;
private ViewHolder viewHolder;
private @LayoutRes int mLayout;
- getView
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (view == null) {
view = View.inflate(mContext, mLayout, null);
viewHolder = getViewHolder();
viewHolder.ViewHolder(view);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
initData(mContext,i, viewGroup, mList, viewHolder);
return view;
}
- 子类处理页面数据方法initData
public abstract void initData(Context context, int i, ViewGroup viewGroup, List<T> mList, ViewHolder viewHolder);
- getViewHolder需要子类重写
public abstract ViewHolder getViewHolder();
- ViewHolder基类 主要做绑定ButterKnife
public abstract class ViewHolder {
public void ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
- 子类只需要继承BaseAdapter 跟ViewHolder 定义好数据泛型即可用
列如:
public class AddWalletCreateAdapter extends BaseAdapter<CoinBean.ContentBean> {
@Override
public void initData(Context context, int i, ViewGroup viewGroup, List<CoinBean.ContentBean> mList, ViewHolder viewHolder) {
final AddWalletCreateHolder addWalletHolder= (AddWalletCreateHolder) viewHolder;
Glide.with(context).load(mList.get(i).getIcon()).into(addWalletHolder.iv_icon);
addWalletHolder.tv_name.setText(mList.get(i).getCoinName());
addWalletHolder.tv_full_name.setText(mList.get(i).getFullName());
}
@Override
public ViewHolder getViewHolder() {
return new AddWalletCreateHolder();
}
class AddWalletCreateHolder extends ViewHolder{
@BindView(R.id.iv_icon)
ImageView iv_icon;
@BindView(R.id.tv_name)
TextView tv_name;
@BindView(R.id.tv_full_name)
TextView tv_full_name;
}
}
使用 :
new AddWalletCreateAdapter().initView(this, R.layout.list_wallet_create, mContentList);
至此简单的基类完成了. 可以根据业务不同抽取更多二级基类.
贴上基类完整代码
public abstract class BaseAdapter<T> extends android.widget.BaseAdapter {
private Context mContext;
private List<T> mList;
private ViewHolder viewHolder;
private @LayoutRes int mLayout;
@Override
public int getCount() {
if(mList==null){
return 0;
}
return mList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
public BaseAdapter(Context context, @LayoutRes int layout , List<T> List){
mContext=context;
mList = List;
mLayout = layout;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (view == null) {
view = View.inflate(mContext, mLayout, null);
viewHolder = getViewHolder();
viewHolder.ViewHolder(view);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
initData(mContext,i, viewGroup, mList, viewHolder);
return view;
}
public abstract void initData(Context context, int i, ViewGroup viewGroup, List<T> mList, ViewHolder viewHolder);
public abstract ViewHolder getViewHolder();
public abstract class ViewHolder {
public void ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
来源:oschina
链接:https://my.oschina.net/u/4410289/blog/4650824