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());
}
}
相关推荐
Jerry3 小时前
Compose 中的绘制功能简介
android
我科绝伦(Huanhuan Zhou)4 小时前
【脚本升级】银河麒麟V10一键安装MySQL9.3.0
android·adb
消失的旧时光-19434 小时前
Android回退按钮处理方法总结
android·开发语言·kotlin
叫我龙翔5 小时前
【MySQL】从零开始了解数据库开发 --- 数据表的约束
android·c++·mysql·数据库开发
2501_916013745 小时前
iOS 上架 App 全流程实战,应用打包、ipa 上传、App Store 审核与工具组合最佳实践
android·ios·小程序·https·uni-app·iphone·webview
2501_915106325 小时前
iOS 26 能耗监测全景,Adaptive Power、新电池视图
android·macos·ios·小程序·uni-app·cocoa·iphone
用户2018792831676 小时前
浅谈Android PID与UID原理
android
TimeFine6 小时前
Android AWS KVS WebRTC 通话声道切换到媒体音乐声道
android
用户2018792831678 小时前
Android文件下载完整性保证:快递员小明的故事
android