前言:
我们在做多布局的时候需要在adapter中各种判断什么的(当初我刚开始学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)
那么就从这儿开始入手开始写,看完了源码就不用一点点分析了,网上有很多博文都有讲如何去看源码的
新增一个类定义为BaseRecyclerAdapter继承Recycleview.Adapter()
/***
* @since 对RecycleView适配器进行封装
* @author liu
* @param <T>
*/
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerHouder> {
//条目ID不一样只能通过参数传递
private int mLayoutId;
//通过泛型传递
private List<T> mDatas;
//实例化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;
}
//创建View
View itemView = mInlater.inflate(mLayoutId, parent, false);
return new RecyclerHouder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerHouder holder, final int position) {
//对Viewhouder的优化
convert(holder, mDatas.get(position), position);
//条目点击事件
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);
}
/***
* 把必须参数数据传递进去
* @param houder
* @param item 当前数据
* @param position 当前索引值
*/
protected abstract void convert(RecyclerHouder houder, T item, int position);
@Override
public int getItemCount() {
return mDatas.size();
}
/***
* 设置点击事件
*/
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进行缓存
View view=mViews.get(viewId);
//使用缓存的方法减少findViewByID的次数
if(view==null){
view=itemView.findViewById(viewId);
mViews.put(viewId,view);
}
return (T) view;
}
/***
* 对通用功能进行封装
* @param viewId
* @param text
* @return
*/
public RecyclerHouder setText(int viewId,CharSequence text){
TextView textView=getView(viewId);
textView.setText(text);
return this;
}
/***
* 设置图片资源
* @param viewId
* @param resourceId
* @return
*/
public RecyclerHouder setImageResource(int viewId,int resourceId){
ImageView imageView=getView(viewId);
imageView.setImageResource(resourceId);
return this;
}
}
在Activit与Fragment中使用时实现implements MulitiTypeSupport就可以了,然后根据你实体类中类型去显示不同布局