package com.ziang.driver.base; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import androidx.annotation.LayoutRes; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.List; /** * Created by ts_xiaoA on 2019/9/24 13:01 * E-Mail Address:443502578@qq.com * Desc: recyclerView baseAdapter 基于 DataBinding */ public abstract class BaseRvAdapter extends RecyclerView.Adapter { public Context context; protected List data; protected OnItemClickListener onItemClickListener; protected final String TAG = String.valueOf(this); private int headViewLayoutId = 0; private int footViewLayoutId = 0; private static final int TYPE_NORMAL = 7895; private static final int TYPE_HEADER = 7896; private static final int TYPE_FOOTER = 7897; public BaseRvAdapter() { this(null, null); } public BaseRvAdapter(Context context) { this(context, null); } public BaseRvAdapter(List data) { this(null, data); } public BaseRvAdapter(Context context, List items) { this.context = context; this.data = items; if (data == null) { data = new ArrayList<>(); } } public void toast(String content){ Toast.makeText(context, content, Toast.LENGTH_SHORT).show(); } @Override public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); if (this.context == null) { this.context = recyclerView.getContext(); } } @NonNull @Override public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view; switch (viewType) { case TYPE_HEADER: view = LayoutInflater.from(context).inflate(headViewLayoutId,parent,false); if (onHeaderViewCreator != null) { onHeaderViewCreator.onCreateView(view); } break; case TYPE_FOOTER: view = LayoutInflater.from(context).inflate(footViewLayoutId,parent); break; case TYPE_NORMAL: default: view = LayoutInflater.from(context).inflate(this.getLayoutResId(viewType),parent,false); } return new BaseViewHolder(view); } @Override public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) { View view =holder.itemView; switch (getItemViewType(position)) { case TYPE_HEADER: if (onHeadViewBinder != null) { onHeadViewBinder.onBindHeadView(holder, view); } break; case TYPE_FOOTER: break; case TYPE_NORMAL: default: int dataPosition = headViewLayoutId == 0 ? holder.getLayoutPosition() : holder.getLayoutPosition() - 1; if (onItemClickListener != null) { holder.itemView.setOnClickListener(v -> onItemClickListener.onItemClick(v, dataPosition)); } this.onBindItem(holder, view, data.size() > dataPosition ? data.get(dataPosition) : null); break; } } @Override public void onBindViewHolder(@NonNull BaseViewHolder holder, int position, @NonNull List payloads) { if (payloads.isEmpty()) { super.onBindViewHolder(holder, position, payloads); } else { View view = holder.itemView; int dataPosition = headViewLayoutId == 0 ? holder.getLayoutPosition() : holder.getLayoutPosition() - 1; onBindChangedItem(holder, view, data.size() > dataPosition ? data.get(dataPosition) : null); } } //局部刷新更新item中的某个指定控件数据或状态 public void onBindChangedItem(BaseViewHolder holder, View view, DataEntity item) { } //局部刷新 public void notifyItemChangedByPayLoads(int position) { notifyItemChanged(position, 0); } @LayoutRes protected abstract int getLayoutResId(int viewType); @Override public final int getItemViewType(int position) { if (isHeader(position)) { return TYPE_HEADER; } else if (isFooter(position)) { return TYPE_FOOTER; } int type = getDataItemViewType(headViewLayoutId == 0 ? position : position - 1); if (type == TYPE_HEADER || type == TYPE_FOOTER) { throw new IllegalArgumentException("Item type cannot equal " + TYPE_HEADER + " or " + TYPE_FOOTER); } return type; } protected int getDataItemViewType(int position) { return TYPE_NORMAL; } public boolean isHeader(int position) { return headViewLayoutId != 0 && position == 0; } public boolean isFooter(int position) { return footViewLayoutId != 0 && position == getItemCount() - 1; } //绑定数据 protected abstract void onBindItem(BaseViewHolder holder, View view, DataEntity item); public List getData() { if (data == null) { data = new ArrayList<>(); } return data; } @Override public final int getItemCount() { int itemCount = 0; if (headViewLayoutId != 0) { itemCount++; } if (footViewLayoutId != 0) { itemCount++; } itemCount += getDataItemCount(); return itemCount; } public int getDataItemCount() { return data.size(); } public int getDataItemPosition(int position) { if (headViewLayoutId != 0) { return position - 1; } else { return position; } } public void setHeadView(@LayoutRes int layoutId) { if (layoutId == 0) { return; } if (this.headViewLayoutId != 0) { notifyItemRemoved(0); } this.headViewLayoutId = layoutId; notifyItemInserted(0); } public interface OnItemClickListener { void onItemClick(View view, int position); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } private OnHeadViewBinder onHeadViewBinder; public interface OnHeadViewBinder { void onBindHeadView(BaseViewHolder holder, View binding); } public void setOnHeadViewBinder(OnHeadViewBinder onHeadViewBinder) { this.onHeadViewBinder = onHeadViewBinder; } private OnHeaderViewCreator onHeaderViewCreator; public interface OnHeaderViewCreator { void onCreateView(View view); } public void setOnHeaderViewCreator(OnHeaderViewCreator onHeaderViewCreator) { this.onHeaderViewCreator = onHeaderViewCreator; } public void removeDataItem(int position) { if (headViewLayoutId != 0) { position += 1; } notifyItemRemoved(position); } public void notifyDateItemRangeChanged(int startPosition, int count) { if (headViewLayoutId != 0) { startPosition += 1; } notifyItemRangeChanged(startPosition, count); } public void removeDataItemAndRefresh(int position) { getData().remove(position); removeDataItem(position); notifyDateItemRangeChanged(position, getDataItemCount()); } }