Activity,Fragment,RecyclerView(ViewBinding)封装 基于ViewBinding,反射简单封装 Activity(ViewBinding,反射进行封装), Fragment(ViewBinding,反射进行封装), RecyclerView(多布局复用单个ViewHolder节省代码逻辑更清晰) 先进行BaseRecyclerViewAdapter抽象封装 再进行UtilsRecyclerViewAdapter抽象封装 HomeAdapter 抽象实现类 感谢大家的观看,如觉写的不好请见谅,谢谢!
RecyclerView与ViewBinding结合简单封装
//简单的封装 单布局 多布局 复用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); }
// 列表工具类 可以自行添加 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 就可以
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(); }); } } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算