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());
}
}
相关推荐
Lei活在当下3 小时前
【Perfetto从入门到精通】2. 使用 Perfetto 追踪/分析 APP 的 Native/Java 内存
android·性能优化·架构
愤怒的代码4 小时前
🔗 深度解析 SystemUI 进程间通信机制(一)
android·操作系统·app
RainyJiang5 小时前
聊聊协程里的 Semaphore:别让协程挤爆门口
android·kotlin
Dev7z6 小时前
在MySQL里创建数据库
android·数据库·mysql
invicinble7 小时前
mysql建立存数据的表(一)
android·数据库·mysql
似霰8 小时前
传统 Hal 开发笔记1----传统 HAL简介
android·hal
Zender Han8 小时前
Flutter Gradients 全面指南:原理、类型与实战使用
android·flutter·ios
火柴就是我8 小时前
Flutter Path.computeMetrics() 的使用注意点
android·flutter
モンキー・D・小菜鸡儿10 小时前
Android 系统TTS(文字转语音)解析
android·tts
2501_9159090610 小时前
iOS 反编译防护工具全景解析 从底层符号到资源层的多维安全体系
android·安全·ios·小程序·uni-app·iphone·webview