RecyclerView适配器的封装

RecyclerView适配器基类的封装

RecyclerView.Adapter封装类

复制代码
public abstract class AutoRollAdapter<T> extends RecyclerView.Adapter<ViewHolder> {

protected List<T> ts = new ArrayList<>();

public List<T> getData() {
    return ts;
}
public int size() {
    return null == ts ? 0 : ts.size();
}

public void add(T t) {
    ts.add(t);
    notifyDataSetChanged();
}

protected void remove(int index) {
    ts.remove(index);
    notifyDataSetChanged();
}

public void addAll(List<T> ts) {
    this.ts.addAll(ts);
    notifyDataSetChanged();
}

public void clear() {
    this.ts.clear();
    notifyDataSetChanged();
}

@LayoutRes
public abstract int getLayoutResId();

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new ViewHolder(parent, getLayoutResId());
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    if (size() > 0) {
        onConvert(holder, getItem(position));
      
       // onConvert(holder, getItem(position), position);
    }
}

protected abstract void onConvert(ViewHolder holder, T bean);

 //返回条目索引,根据自己需求设置返回参数
protected abstract void onConvert(ViewHolder holder, T bean, int position);

public T getItem(int position) {
    return ts.get(position % size());
}
}

ViewHolder封装

复制代码
public class ViewHolder extends RecyclerView.ViewHolder {
private SparseArray<View> viewArray;

public ViewHolder(ViewGroup parent, @LayoutRes int resId) {
    super(LayoutInflater.from(parent.getContext()).inflate(resId, parent, false));
    viewArray = new SparseArray<>();
}

public <T extends View> T findViewById(@IdRes int resId) {
    View view = viewArray.get(resId);
    if(view == null) {
        view = itemView.findViewById(resId);
        viewArray.put(resId, view);
    }
    return (T) view;
}

public ViewHolder setText(@IdRes int resId, CharSequence text) {
    TextView tv = findViewById(resId);
    if(tv != null) {
        tv.setText(text);
    }
    return this;
}

public ViewHolder setTextColor(@IdRes int resId, int color) {
    TextView tv = findViewById(resId);
    if(tv != null) {
        tv.setTextColor(color);
    }
    return this;
}
public ViewHolder setOnClickListener(int viewId, View.OnClickListener onClickListener) {
    View view = findViewById(viewId);
    view.setOnClickListener(onClickListener);
    return this;
}
}

ViewHolder进行了封装目前之封装了setText、setTextColor以及setOnClickListener,可以根据自己的需求进行添加

使用封装的基类

复制代码
public class CallAdapter extends AutoRollAdapter<CallBean> {

//设置布局-
@Override
public int getLayoutResId() {
    return R.layout.listview_item_call;
}

//设置控件属性
@Override
protected void onConvert(ViewHolder holder, CallBean bean) {
    holder.setText(R.id.tv_window, bean.getWindowName())
            .setTextColor(R.id.tv_window, bean.getStateColor());
}
}
相关推荐
selt79116 小时前
Redisson之RedissonLock源码完全解析
android·java·javascript
Yao_YongChao17 小时前
Android MVI处理副作用(Side Effect)
android·mvi·mvi副作用
非凡ghost18 小时前
JRiver Media Center(媒体管理软件)
android·学习·智能手机·媒体·软件需求
席卷全城18 小时前
Android 推箱子实现(引流文章)
android
齊家治國平天下18 小时前
Android 14 系统中 Tombstone 深度分析与解决指南
android·crash·系统服务·tombstone·android 14
maycho12320 小时前
MATLAB环境下基于双向长短时记忆网络的时间序列预测探索
android
思成不止于此21 小时前
【MySQL 零基础入门】MySQL 函数精讲(二):日期函数与流程控制函数篇
android·数据库·笔记·sql·学习·mysql
brave_zhao21 小时前
达梦数据库(DM8)支持全文索引功能,但并不直接兼容 MySQL 的 FULLTEXT 索引语法
android·adb
sheji341621 小时前
【开题答辩全过程】以 基于Android的网上订餐系统为例,包含答辩的问题和答案
android
easyboot1 天前
C#使用SqlSugar操作mysql数据库
android·sqlsugar