RecyclerView与ViewBinding结合简单封装
作者:互联网
RecyclerView与ViewBinding结合简单封装
先进行BaseRecyclerViewAdapter抽象封装
//简单的封装 单布局 多布局 复用ViewHolder
public abstract class BaseRecyclerViewAdapter<V> extends RecyclerView.Adapter<BaseRecyclerViewAdapter.ViewHolder> {
protected ArrayList<V> list;
protected Context context;
protected boolean isSizeCrrect;
public BaseRecyclerViewAdapter(ArrayList<V> list, Context context, boolean isSizeCrrect) {
this.list = list;
this.context = context;
this.isSizeCrrect = isSizeCrrect;
}
@Override
public int getItemViewType(int position) {
return getViewType(position);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(getViewBinding(viewType, LayoutInflater.from(context), parent));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
setData(holder.v, list.get(position));
}
@Override
public int getItemCount() {
if (isSizeCrrect) {
//如果 list.size() 为正确
return list.size();
} else {
//如果 list.size() 为错误
return getListSize(list.size());
}
}
static class ViewHolder extends RecyclerView.ViewHolder {
ViewBinding v;
ViewHolder(ViewBinding viewBinding) {
super(viewBinding.getRoot());
v= viewBinding;
}
}
/* 这里返回的是 实际大小 不等于 list.size()
*
* 比如: 列表的末尾要显示一个添加图片的按钮布局 就要用到这个抽象方法 来给适配器实际的列表大小
* */
protected abstract int getListSize(int size);
/*
* 这个多布局判断抽象方法 需实现类 实现操作
* */
protected abstract int getViewType(int position);
/*
* 获取 ViewBinding 实现类
* */
protected abstract ViewBinding getViewBinding(int viewType, LayoutInflater from, ViewGroup parent);
/*
* 给ViewBinding 布局设置数据 点击事件等操作
* */
protected abstract void setData(ViewBinding viewBinding, V entity);
}
再进行UtilsRecyclerViewAdapter抽象封装
// 列表工具类 可以自行添加
public abstract class UtilsRecyclerViewAdapter <V>extends BaseRecyclerViewAdapter<V> {
public UtilsRecyclerViewAdapter(ArrayList<V> list, Context context, boolean isSizeCrrect) {
super(list, context, isSizeCrrect);
}
//------------------------------------------------------------------
protected void setText(TextView view, String o){
view.setText(o);
}
protected void setText(EditText view, String o){
view.setText(o);
}
protected void setOnClick(View view, View.OnClickListener v){
view.setOnClickListener(v);
}
//---------------------------------------------------------------------------------------------------------------------------
/*
* 在指定位置 插入 单个 数据
*
* */
protected void setInsertItem(V item,int position){
this.list.add(position,item);
notifyItemRangeInserted(position,1);
notifyItemRangeChanged(position,getItemCount());
}
/*
* 从指定位置 插入 多个 数据
*
* */
protected void setInsertList(ArrayList<V> list,int position){
this.list.addAll(position,list);
notifyItemRangeInserted(position,list.size());
notifyItemRangeChanged(position,getItemCount());
}
/*
* 插入 多个 数据
*
* */
protected void setInsertList(ArrayList<V> list){
this.list.addAll(list);
notifyItemRangeInserted(0,this.list.size());
notifyItemRangeChanged(0,getItemCount());
}
/*
* 从指定位置 删除 多个 数据
*
* */
protected void setDeleteItem(V item,int position){
if(item == null ){
this.list.remove(position);
}else {
this.list.remove(item);
}
notifyItemRemoved(position);
notifyItemRangeChanged(position,getItemCount());
}
/*
* 删除 多个 数据
* */
protected void setDeleteList(ArrayList<V> list){
this.list.removeAll(list);
notifyDataSetChanged();
}
/*
* 清空数据
* */
protected void setClear() {
if(this.list.size() > 0){
this.list.clear();
}
}
/*
* 清空数据 再 添加列表数据
* */
protected void setClearAddList(ArrayList<V> list){
setClear();
setDeleteList(list);
}
}
注意:
UtilsRecyclerViewAdapter 只是一个工具抽象类,
如果不需要可以只使用BaseRecyclerViewAdapter 就可以
HomeAdapter 抽象实现类
public class HomeAdapter extends UtilsRecyclerViewAdapter<entity> {
public HomeAdapter(ArrayList<entity> list, Context context, boolean isSizeCrrect) {
super(list, context, isSizeCrrect);
}
@Override
protected int getListSize(int size) {
return list.size();
}
@Override
protected int getViewType(int position) {
if(position % 2 == 0){
return 0;
}else {
return 1;
}
}
@Override
protected ViewBinding getViewBinding(int viewType, LayoutInflater from, ViewGroup parent) {
if(viewType == 0){
return ItemLayoutBinding.inflate(from,parent,false);
}else {
return Item2LayoutBinding.inflate(from,parent,false);
}
}
@Override
protected void setData(ViewBinding viewHolder, entity entity) {
if(viewHolder instanceof ItemLayoutBinding){
((ItemLayoutBinding) viewHolder).text.setText("1111111111");
((ItemLayoutBinding) viewHolder).text.setOnClickListener(v -> {
Toast toast = Toast.makeText(context, null, Toast.LENGTH_SHORT);
toast.setText(((ItemLayoutBinding) viewHolder).text.getText().toString());
toast.show();
});
return;
}
if(viewHolder instanceof Item2LayoutBinding){
((Item2LayoutBinding) viewHolder).textView.setText("2222222222");
((Item2LayoutBinding) viewHolder).textView.setOnClickListener(v -> {
Toast toast = Toast.makeText(context, null, Toast.LENGTH_SHORT);
toast.setText(((Item2LayoutBinding) viewHolder).textView.getText().toString());
toast.show();
});
}
}
}
感谢大家的观看,如觉写的不好请见谅,谢谢!
标签:封装,int,ViewBinding,void,list,protected,context,position,RecyclerView 来源: https://blog.csdn.net/weixin_43508425/article/details/106555292