###############原来写listview / recyclerView 适配器, 都是使用的这种方式:
https://github.com/hongyangAndroid/baseAdapter
###############在butterknife出来之前这种方式堪称完美,但是随着butterknife的出现,原来那种写适配器的方式渐渐的被摒弃了。
原因无外乎以下两点:
1.butterknife实在太好用了,配合android studio插件,一键生成,那种感觉无与伦比的爽。
2.鸿洋的这种方式也有小缺陷, 每次对viewHolder里面的view操作的时候,要么findViewById实例化一个,要么 传递id进去。
.
.
比如设置TextView:
holder.setText(R.id.tv, "文字");
所以针对butterknife,自己对如上的适配器做了不小的改动,由于本人水平有限,如果你觉得哪里不合适,或者觉得哪里可以改进的地方,尽管提意见,大家互相学习。下面贴上代码:
.
.
ListView的adapter:
使用起来也很简单:(其中xxViewHolder 必须继承BaseViewHolder)
adapter = new MBaseAdapter<Bean, xxViewHolder>() {
@Override
public xxViewHolder getHolder(View convertView, int type) {
return new xxViewHolder(convertView);
}
@Override
public void getItemView(xxViewHolder holder, Bean item) {
holder.text.setText(item.text);
holder.image.loadImage(item.getUrl());
}
}
封装的代码:
/**
* listView适配器的基类
* 单种布局的
* @param <T>
* @param <VH>
*/
public abstract class MBaseAdapter<T, VH extends BaseViewHolder> extends BaseAdapter {
protected Context mContext;
protected List<T> list;
protected int layoutId;
public MBaseAdapter(List<T> list, Context context, int layoutId)
{
mContext = context;
this.list = list;
this.layoutId = layoutId;
}
public List<T> getList(){
return list;
}
@Override
public int getCount() {
return null != list ? list.size() : 0;
}
@Override
public T getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int id) {
return id;
}
@Override
@SuppressWarnings("unchecked")
public View getView(int position, View convertView, ViewGroup parent)
{
VH holder;
if (convertView == null) {
convertView = View.inflate(mContext, layoutId, null);;
holder = getHolder(convertView, getItemViewType(position));
convertView.setTag(holder);
} else {
holder = (VH) convertView.getTag();
}
holder.position = position;
getItemView(holder, list.get(position));
return convertView;
}
public abstract VH getHolder(View convertView, int type);
public abstract void getItemView(VH holder, T item);
}
/**
* 支持多种类型的adapter
* @author deadline
* @time 2016/10/17
*/
public abstract class MultiBaseAdapter<T> extends MBaseAdapter<T, BaseViewHolder> implements MultiSupport<T>{
public MultiBaseAdapter(List<T> list, Context context) {
super(list, context, -1);
}
@Override
public int getItemViewType(int position) {
return getItemType(position, list.get(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
BaseViewHolder holder;
int type = getItemViewType(position);
int layoutId = getLayoutId(type);
if (convertView == null) {
convertView = View.inflate(mContext, layoutId, null);
holder = getHolder(convertView, type);
convertView.setTag(holder);
} else {
holder = (BaseViewHolder) convertView.getTag();
if (holder.type != type) {
convertView = View.inflate(mContext, layoutId, null);
holder = getHolder(convertView, type);
convertView.setTag(holder);
}
}
holder.position = position;
getItemView(holder, list.get(position));
return convertView;
}
}
/**
* 多种类型支持接口
* @author deadline
* @time 2016/10/17
*/
public interface MultiSupport<T> {
int getViewTypeCount();
int getLayoutId(int viewType);
int getItemType(int position, T item);
}
BaseViewHolder 自定义的viewHodler都要继承该类
public class BaseViewHolder {
public int type;
public int position;
public BaseViewHolder(View view) {
ButterKnife.bind(this, view);
}
/**
* 多种类型
* @param view
* @param type
*/
public BaseViewHolder(View view, int type) {
this.type = type;
ButterKnife.bind(this, view);
}
}
RecyclerView的适配器
public abstract class MRecyclerBaseAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
protected Context mContext;
protected List<T> list;
public MRecyclerBaseAdapter(Context mContext, List<T> list) {
this.list = list;
this.mContext = mContext;
}
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(getLayoutId(), null);
return getHolder(view);
}
@Override
public void onBindViewHolder(final VH holder, final int position) {
getItemView(holder, list.get(position));
RxView.clicks(holder.itemView).throttleFirst(500, TimeUnit.MILLISECONDS)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
OnItemClick(holder, list.get(position), position);
}
});
RxView.longClicks(holder.itemView).throttleFirst(500, TimeUnit.MILLISECONDS)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
OnItemLongClick(holder, list.get(position), position);
}
});
}
public abstract void getItemView(VH holder, T item);
public abstract int getLayoutId();
public abstract VH getHolder(View convertView);
public void OnItemClick(VH holder, T item, int position) {}
public void OnItemLongClick(VH holder, T item, int position) {}
public List<T> getList() {
return list;
}
@Override
public int getItemCount() {
return list != null ? list.size() : 0;
}
网友评论