当前位置:网站首页>DataBinding下的RecycleView适配器Adapter基类
DataBinding下的RecycleView适配器Adapter基类
2022-08-04 00:01:00 【建人尹口】
public abstract class BaseAdapter<T, B extends ViewDataBinding>
extends RecyclerView.Adapter<BaseViewHolder> {
private final ObservableList<T> mData;
public BaseAdapter() {
this(new ObservableArrayList<>());
}
public BaseAdapter(@NonNull ObservableList<T> dataList) {
mData = dataList;
mData.addOnListChangedCallback(new ObservableList.OnListChangedCallback<ObservableList<T>>() {
@Override
public void onChanged(ObservableList<T> sender) {
notifyDataSetChanged();
}
@Override
public void onItemRangeChanged(ObservableList<T> sender, int positionStart, int itemCount) {
notifyItemRangeChanged(positionStart, itemCount);
}
@Override
public void onItemRangeInserted(ObservableList<T> sender, int positionStart, int itemCount) {
notifyItemRangeInserted(positionStart, itemCount);
}
@Override
public void onItemRangeMoved(ObservableList<T> sender, int fromPosition, int toPosition, int itemCount) {
notifyItemMoved(fromPosition, toPosition);
}
@Override
public void onItemRangeRemoved(ObservableList<T> sender, int positionStart, int itemCount) {
notifyItemRangeRemoved(positionStart, itemCount);
}
});
}
public void setData(List<T> data) {
mData.clear();
addData(data);
}
public void addData(List<T> data) {
if (data != null && data.size() > 0) {
mData.addAll(data);
}
}
public @NonNull List<T> getData() {
int size = getItemCount();
List<T> resList = new ArrayList<>(size);
if (size > 0) {
resList.addAll(mData);
}
return resList;
}
public void updateItem(T item) {
int index = mData.indexOf(item);
if (index > -1) {
mData.set(index, item);
}
}
@NonNull
@Override
public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
B binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
getItemLayoutId(), parent, false);
return new BaseViewHolder(binding.getRoot());
}
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
B binding = DataBindingUtil.getBinding(holder.itemView);
onBindItem(mData.get(position), binding);
}
@Override
public int getItemCount() {
return mData.size();
}
/**
* 获取item项布局资源id
* @return 布局资源id
*/
public abstract @LayoutRes int getItemLayoutId();
/**
* 绑定数据
* @param item 数据项
* @param binding 绑定
*/
public abstract void onBindItem(T item, B binding);
}
其中
public final class BaseViewHolder extends RecyclerView.ViewHolder {
public BaseViewHolder(@NonNull View itemView) {
super(itemView);
}
}
边栏推荐
猜你喜欢
栈的压入、弹出序列
2021年数据泄露成本报告解读
Shell编程之循环语句与函数
详谈RDMA技术原理和三种实现方式
The world's first mass production, with the most fixed points!How does this AVP Tier1 lead?
corn表达式 具体详解与案例
Using matlab to solve the linear optimization problem based on matlab dynamic model of learning notes _11 】 【
ping数据包中的进程号
查看CUDA、pytorch等的版本号
LeetCode 19:删除链表的倒数第 N 个结点
随机推荐
LeetCode 0155. 最小栈
全面讲解 Handler机制原理解析 (小白必看)
栈的压入、弹出序列
C语言实验十五 文件
建木DevOps流程的快速运用
XSLT – 服务器端概述
超级完美版布局有快捷键,有背景置换(解决opencv 中文路径问题)
The problem of disorganized data output by mnn model
Flutter教程之为什么 Flutter 是创业的最佳选择?
Free自由协议系统开发
用队列模拟实现栈
关于mnn模型输出的数据杂乱无章问题
(PC+WAP)织梦模板不锈钢类网站
2021年数据泄露成本报告解读
孙宇晨:Web3.0和元宇宙将协助人类更加全面地进入网络世界
七夕?new一个对象
图论-虚拟节点分层建图
【OpenCV图像处理】 图像拼接技术
Justin Sun was invited to attend the 36氪 Yuan Universe Summit and delivered a keynote speech
一文搞定 SQL Server 执行计划