前言:
我們在做多布局的時(shí)候需要在adapter中各種判斷什么的(當(dāng)初我剛開始學(xué)Android是這樣)很麻煩而且代碼也比較臃腫邓馒,看著頭疼巷波,既然是在adapter中處理多布局的問題那么為何我不去改變一下原始的BaseAdapter呢霎迫,RecycleView的adapter是繼承Recycleview.Adapter减江,而ListView是BaseAdapter誊册,我還是說Recycleview吧
Recycleview的適配器是泛型的波岛,我們可以去看他的源碼
![image.png](https://upload-images.jianshu.io/upload_images/6624077-c1fa9909c1847c06.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
那么就從這兒開始入手開始寫芭梯,看完了源碼就不用一點(diǎn)點(diǎn)分析了险耀,網(wǎng)上有很多博文都有講如何去看源碼的
新增一個(gè)類定義為BaseRecyclerAdapter繼承Recycleview.Adapter()
/***
* @since 對RecycleView適配器進(jìn)行封裝
* @author liu
* @param <T>
*/
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerHouder> {
//條目ID不一樣只能通過參數(shù)傳遞
private int mLayoutId;
//通過泛型傳遞
private List<T> mDatas;
//實(shí)例化LayoutInflate
private LayoutInflater mInlater;
//多布局接口
private MulitiTypeSupport<T> muTypeSuport;
public BaseRecyclerAdapter(Context context, List<T> mDatas, int mLayoutId) {
this.mInlater = LayoutInflater.from(context);
this.mLayoutId = mLayoutId;
this.mDatas = mDatas;
}
public BaseRecyclerAdapter(Context context, List<T> mDatas, MulitiTypeSupport<T> typeSuport) {
this(context, mDatas, -1);
this.muTypeSuport = typeSuport;
}
@NonNull
@Override
public RecyclerHouder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (muTypeSuport != null) {
//使用多布局
mLayoutId = viewType;
}
//創(chuàng)建View
View itemView = mInlater.inflate(mLayoutId, parent, false);
return new RecyclerHouder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerHouder holder, final int position) {
//對Viewhouder的優(yōu)化
convert(holder, mDatas.get(position), position);
//條目點(diǎn)擊事件
if (listtenner != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listtenner.onItemClick(view, position);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return listtenner.onItemLongClick(view, position);
}
});
}
}
@Override
public int getItemViewType(int position) {
if (muTypeSuport != null) {
return muTypeSuport.getLayoutId(mDatas.get(position));
}
return super.getItemViewType(position);
}
/***
* 把必須參數(shù)數(shù)據(jù)傳遞進(jìn)去
* @param houder
* @param item 當(dāng)前數(shù)據(jù)
* @param position 當(dāng)前索引值
*/
protected abstract void convert(RecyclerHouder houder, T item, int position);
@Override
public int getItemCount() {
return mDatas.size();
}
/***
* 設(shè)置點(diǎn)擊事件
*/
private ItemClickListtenner listtenner;
public void setItemClickListenner(ItemClickListtenner itemClickListenner) {
this.listtenner = itemClickListenner;
}
懶得寫了,上面有注釋
接口 #ItemClickListtenner
public interface ItemClickListtenner {
void onItemClick(View view, int position);
boolean onItemLongClick(View view, int position);
}
多布局的接口 #
/****
* @deprecated 多布局類型支持接口
*/
public interface MulitiTypeSupport<T> {
int getLayoutId(T item);
}
使用方法
public class MainRecycleAdapter extends BaseRecyclerAdapter<String>{
public MainRecycleAdapter(Context context, List<String> mDatas, int mLayoutId) {
super(context, mDatas, mLayoutId);
}
@Override
protected void convert(RecyclerHouder houder, String item, int position) {
TextView text=getView(R.id.text);
text.setText("我是馬云");
//還有一種用法是
houder.setText(R.id.text,"我是馬化騰");
}
}
我們看看Houder,同樣是繼承Recycleview.ViewHouder
public class RecyclerHouder extends RecyclerView.ViewHolder{
//用于緩存界面
private SparseArray<View> mViews=new SparseArray<>();
public RecyclerHouder(View itemView) {
super(itemView);
}
/***
* 通過viewId獲取控件
* @param viewId
* @param <T>
* @return
*/
public < T extends View> T getView(int viewId){
//多次findviewByID玖喘,對已有的view進(jìn)行緩存
View view=mViews.get(viewId);
//使用緩存的方法減少findViewByID的次數(shù)
if(view==null){
view=itemView.findViewById(viewId);
mViews.put(viewId,view);
}
return (T) view;
}
/***
* 對通用功能進(jìn)行封裝
* @param viewId
* @param text
* @return
*/
public RecyclerHouder setText(int viewId,CharSequence text){
TextView textView=getView(viewId);
textView.setText(text);
return this;
}
/***
* 設(shè)置圖片資源
* @param viewId
* @param resourceId
* @return
*/
public RecyclerHouder setImageResource(int viewId,int resourceId){
ImageView imageView=getView(viewId);
imageView.setImageResource(resourceId);
return this;
}
}
在Activit與Fragment中使用時(shí)實(shí)現(xiàn)implements MulitiTypeSupport就可以了甩牺,然后根據(jù)你實(shí)體類中類型去顯示不同布局