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());
}
}
相关推荐
imuliuliang3 小时前
Laravel6.x核心特性全解析
android·php·laravel
idingzhi4 小时前
A股量化策略日报(2026年05月22日)
android·开发语言·python·kotlin
测试员周周5 小时前
【Appium 系列】第14节-断言与验证 — Validator 的设计
android·人工智能·python·功能测试·ios·单元测试·appium
赏金术士6 小时前
Android 动画对比指南:View 系统 vs Jetpack Compose
android·kotlin·compose
我命由我123456 小时前
C++ - 面向对象 - 析构函数
android·c语言·开发语言·c++·visualstudio·visual studio·android runtime
失眠的咕噜7 小时前
PDA 安卓设备上传多张图片
android·前端·javascript
zb200641207 小时前
Laravel6.x新特性全解析
android
plainGeekDev7 小时前
Kotlin核心:空安全都搞不明白,还敢说熟练Kotlin?
android·面试·kotlin
huaCodeA8 小时前
Android面试-Flow相关
android·面试·职场和发展
繁星星繁8 小时前
Python基础语法(二)
android·服务器·python