###############原來(lái)寫(xiě)listview / recyclerView 適配器, 都是使用的這種方式:
https://github.com/hongyangAndroid/baseAdapter
###############在butterknife出來(lái)之前這種方式堪稱完美佃却,但是隨著butterknife的出現(xiàn),原來(lái)那種寫(xiě)適配器的方式漸漸的被摒棄了脑慧。
原因無(wú)外乎以下兩點(diǎn):
1.butterknife實(shí)在太好用了,配合android studio插件君纫,一鍵生成,那種感覺(jué)無(wú)與倫比的爽。
2.鴻洋的這種方式也有小缺陷檬某, 每次對(duì)viewHolder里面的view操作的時(shí)候替梨,要么findViewById實(shí)例化一個(gè)钓试,要么 傳遞id進(jìn)去装黑。
.
.
比如設(shè)置TextView:
holder.setText(R.id.tv, "文字");
所以針對(duì)butterknife,自己對(duì)如上的適配器做了不小的改動(dòng)弓熏,由于本人水平有限恋谭,如果你覺(jué)得哪里不合適,或者覺(jué)得哪里可以改進(jìn)的地方挽鞠,盡管提意見(jiàn)疚颊,大家互相學(xué)習(xí)。下面貼上代碼:
.
.
ListView的adapter:
使用起來(lái)也很簡(jiǎn)單:(其中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;
}