自带头部,底部,和空布局的recycleView.Adapter基类封装相关说明:
1.适用于多种类型的条目类型(目前支持头部,底部,和空布局,其他条目布局需要自己扩展)
2.支持空页面展示和自定义空布局
3.支持头部和底部的添加和自定义头部和底部布局
下面对关键代码进行分析:
重写getItemViewType(int position) 方法,根据不同的条目返回对应的布局类型在这里我定义了四种布局类型:默认条目类型,头部,底部,和空布局
@Override
public int getItemViewType(int position) {
int size = 0;
boolean isEmpty = null == data || data.isEmpty();
if (isShowHeader) ++size;
if (isShowFooter) ++size;
if (isShowHeader && !isEmpty && position == 0) return TYPE_HEADER;
if (isShowFooter && !isEmpty && (data.size()-1+size == position)) return TYPE_FOOTER;
if (isShowEmpty && isEmpty) return TYPE_EMPTY;
return super.getItemViewType(position);
}
重写 getItemCount()方法,计算出需要的 item 数量,这里我在这里我通过判断data数据源的数据来判断当数据为空时不显示头部和底部,有需要显示的可以修改这部分代码
@Override
public int getItemCount() {
int size = 0;
boolean isEmpty = null == data || data.isEmpty();
if (isShowHeader && !isEmpty) ++size;
if (isShowFooter && !isEmpty) ++size;
if (isShowEmpty && isEmpty) return 1;
return data.size()+size;
}
重写onCreateViewHolder(@NonNull ViewGroup parent, int viewType) , 根据viewType来判断创建不同的ViewHolder类型,这里头部,底部和空页面都通过了方法返回的方式来创建View,目的是为了方便自定义布局的使用(注意:头部和底部要放到空页面的前面判断,后面会有说明)
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_HEADER:
return new ViewHolderHeader<>(parent,layoutResIdHeader());
case TYPE_FOOTER:
return new ViewHolderFooter<>(parent,layoutResIdFooter());
case TYPE_EMPTY:
return new ViewHolderEmpty<>(parent,layoutResIdEmpty());
default:
return new ViewHolder<D>(parent,layoutResId());
}
}
重写onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) 方法,在该方法中判断布局类型,onBindView 是通用默认的类型,onBindViews适用于头部,底部和空页面布局,大家可以根据自己的需要去改。当然如果有头部布局显示,不要忘记position要减去1。头部,底部的viewHolder 要在空布局的viewHolder之前判断,下面我会贴出原因:
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder) {
int len = isShowHeader?position-1:position;
onBindViewHolders(((ViewHolder<D>)holder).binding,len);
checkedListener(((ViewHolder<D>)holder).binding,len);
} else if (holder instanceof ViewHolderHeader) {
onBindView(((ViewHolderHeader) holder).binding);
} else if (holder instanceof ViewHolderFooter) {
onBindView(((ViewHolderFooter) holder).binding);
} else if (holder instanceof ViewHolderEmpty){
onBindView(((ViewHolderEmpty) holder).binding);
}
// holder.binding.executePendingBindings();
}
//原因:头部和尾部都继承了空布局的ViewHodler
public static class ViewHolderEmpty<E extends ViewDataBinding> extends RecyclerView.ViewHolder{
public E binding;
public ViewHolderEmpty(ViewGroup parent,@LayoutRes int resId) {
super(DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),resId,parent,false).getRoot());
binding = DataBindingUtil.getBinding(this.itemView);
}
}
public static class ViewHolderHeader<E extends ViewDataBinding> extends ViewHolderEmpty<E> {
public ViewHolderHeader(ViewGroup parent, int resId) {
super(parent, resId);
}
}
public static class ViewHolderFooter<E extends ViewDataBinding> extends ViewHolderEmpty<E> {
public ViewHolderFooter(ViewGroup parent, int resId) {
super(parent, resId);
}
}
针对实现GridView效果的头部、尾部和空布局的适配问题,例如使用了两列,代码如下:
GridLayoutManager layoutManager = new GridLayoutManager(getApplication(),2, LinearLayoutManager.VERTICAL,false);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int type = adapter.getItemViewType(position);
if (type == BaseRecycleAdapter.TYPE_HEADER
|| type == BaseRecycleAdapter.TYPE_FOOTER
|| type == BaseRecycleAdapter.TYPE_EMPTY) return 2;
return 1;
}
});
最后附上完整的代码,希望对大家有帮助,如果有不足的地方欢迎大家指正,谢谢。
public abstract class BaseRecycleAdapter<T,D extends ViewDataBinding> extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
protected List<T> data;
protected OnItemClickListener onItemClickListener;
private boolean isUseDefaultListener = true;
private boolean isShowHeader = false;
private boolean isShowFooter = false;
private boolean isShowEmpty = true;
public BaseRecycleAdapter(List<T> data, boolean isUseDefaultListener,boolean isShowHeader ,boolean isShowFooter,boolean isShowEmpty) {
this(data,isShowHeader,isShowFooter,isShowEmpty);
this.isUseDefaultListener = isUseDefaultListener;
}
public BaseRecycleAdapter(List<T> data ,boolean isShowHeader ,boolean isShowFooter,boolean isShowEmpty) {
this(data,true);
this.isShowHeader = isShowHeader;
this.isShowFooter = isShowFooter;
this.isShowEmpty = isShowEmpty;
}
public BaseRecycleAdapter(List<T> data, boolean isUseDefaultListener) {
this(data);
this.isUseDefaultListener = isUseDefaultListener;
}
public BaseRecycleAdapter(List<T> data) {
this.data = data;
}
private static final int TYPE_EMPTY = 1;
private static final int TYPE_HEADER = 2;
private static final int TYPE_FOOTER = 3;
//通用条目
public abstract @LayoutRes
int layoutResId();
//空布局条目
public @LayoutRes int layoutResIdEmpty(){
return R.layout.view_base_empty;
}
public @LayoutRes int layoutResIdHeader(){
return layoutResIdEmpty();
}
public @LayoutRes int layoutResIdFooter(){
return layoutResIdEmpty();
}
/**
* 通用item
* @param binding 布局
* @param position 条目索引
*/
public abstract void onBindViewHolders(D binding, int position);
/**
* 头部尾部和空布局
* @param binding 布局
*/
public void onBindView(ViewDataBinding binding) {}
public void addOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_HEADER:
return new ViewHolderHeader<>(parent,layoutResIdHeader());
case TYPE_FOOTER:
return new ViewHolderFooter<>(parent,layoutResIdFooter());
case TYPE_EMPTY:
return new ViewHolderEmpty<>(parent,layoutResIdEmpty());
default:
return new ViewHolder<D>(parent,layoutResId());
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder) {
int len = isShowHeader?position-1:position;
onBindViewHolders(((ViewHolder<D>)holder).binding,len);
checkedListener(((ViewHolder<D>)holder).binding,len);
} else if (holder instanceof ViewHolderHeader) {
onBindView(((ViewHolderHeader) holder).binding);
} else if (holder instanceof ViewHolderFooter) {
onBindView(((ViewHolderFooter) holder).binding);
} else if (holder instanceof ViewHolderEmpty){
onBindView(((ViewHolderEmpty) holder).binding);
}
// holder.binding.executePendingBindings();
}
protected void checkedListener(D binding,int position){
if (isUseDefaultListener && null != data && data.size() > 0 && onItemClickListener != null) {
binding.getRoot().setOnClickListener(view -> onItemClickListener.onClickListener(view,position));
}
}
@Override
public int getItemCount() {
int size = 0;
boolean isEmpty = null == data || data.isEmpty();
if (isShowHeader && !isEmpty) ++size;
if (isShowFooter && !isEmpty) ++size;
if (isShowEmpty && isEmpty) return 1;
return data.size()+size;
}
@Override
public int getItemViewType(int position) {
int size = 0;
boolean isEmpty = null == data || data.isEmpty();
if (isShowHeader) ++size;
if (isShowFooter) ++size;
if (isShowHeader && !isEmpty && position == 0) return TYPE_HEADER;
if (isShowFooter && !isEmpty && (data.size()-1+size == position)) return TYPE_FOOTER;
if (isShowEmpty && isEmpty) return TYPE_EMPTY;
return super.getItemViewType(position);
}
public static class ViewHolder<D extends ViewDataBinding> extends RecyclerView.ViewHolder {
public D binding;
public ViewHolder(ViewGroup parent, @LayoutRes int resId) {
super(DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),resId,parent,false).getRoot());
binding = DataBindingUtil.getBinding(this.itemView);
}
}
public static class ViewHolderEmpty<E extends ViewDataBinding> extends RecyclerView.ViewHolder{
public E binding;
public ViewHolderEmpty(ViewGroup parent,@LayoutRes int resId) {
super(DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),resId,parent,false).getRoot());
binding = DataBindingUtil.getBinding(this.itemView);
}
}
public static class ViewHolderHeader<E extends ViewDataBinding> extends ViewHolderEmpty<E> {
public ViewHolderHeader(ViewGroup parent, int resId) {
super(parent, resId);
}
}
public static class ViewHolderFooter<E extends ViewDataBinding> extends ViewHolderEmpty<E> {
public ViewHolderFooter(ViewGroup parent, int resId) {
super(parent, resId);
}
}
}