android 基于MVVM架构的超简便易用RecyclerView适配器Adapter封装(databingding方式)

本文介绍了如何基于MVVM架构,利用Databinding方式封装一个超简便的RecyclerView适配器基类。提供了适配器基类的使用方法,包括布局文件和数据绑定的注意事项,强调了数据绑定的特定字段名要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上git地址https://github.com/mazwu110/libhttpapiMvvmDemo

1. 适配器基类

/**
 * Created by mzw on 2019/7/25.
 */

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BindingViewHolder>
        implements BaseViewHolder.OnNotifyChangeListener {
    public static Context mContext;
    public LayoutInflater mLayoutInflater;
    public List<T> mRecordList = new ArrayList();
    public static final int TYPE_CONTENT = 1;

    public BaseRecyclerAdapter(Context context) {
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        return setViewHolder(viewGroup);
    }

    @Override
    public void onBindViewHolder(BindingViewHolder holder, int position) {
        bindData(holder, position);
    }

    @Override
    public int getItemCount() {
        return mRecordList.size();
    }

    //空布局类型返回1,正常布局类型返回0
    @Override
    public int getItemViewType(int position) {
        return TYPE_CONTENT;
    }

    @Override
    public void onNotify() {
        //提供给BaseViewHolder方便刷新视图
        notifyDataSetChanged();
    }

    //添加第一页数据
    public void addRecordList(List<T> datas) {
        mRecordList.clear();
        if (null != datas) {
            mRecordList.addAll(datas);
        }

        notifyDataSetChanged();
    }

    // 添加更多数据
    public void addMoreRecordList(List<T> datas) {
        if (null != datas) {
            mRecordList.addAll(datas);
        }
        notifyDataSetChanged();
    }

    public void clearDatas() {
        mRecordList.clear();
        notifyDataSetChanged();
    }


    public T getItem(int position) {
        if (mRecordList != null && position < mRecordList.size()) {
            return mRecordList.get(position);
        } else {
            return null;
        }
    }

    //删除单条数据
    public void deletItem(T data) {
        mRecordList.remove(data);
        notifyDataSetChanged();
    }

    /**
     * 子类重写实现自定义ViewHolder
     */
    public abstract BindingViewHolder setViewHolder(ViewGroup parent);

    //用给定的 data 对 holder 的 view 进行赋值,交给子类自己实现
    public abstract void bindData(BindingViewHolder bindingViewHolder, int positon);


    // 图片加载
    @BindingAdapter({"app:imageUrl"})
    public static void loadImageFromUrl(ImageView imageView, String url) {
        if (url.startsWith("http") || url.startsWith("HTTP")) { // 网络图片
            QApp.mImageLoader.displayNetImage(imageView, url);
        } else { // base64图片
            //QApp.mImageLoader.addBase64Image(url, imageView);
        }
    }

    // 解决为图片赋值本地资源时 运行效果不见图片的问题
    @BindingAdapter("android:src")
    public static void setSrc(ImageView view, int resId) {
        view.setImageResource(resId);
    }

    @BindingAdapter("android:textColor")
    public static void setTextColor(TextView view, int resId) {
        view.setTextColor(mContext.getResources().getColor(resId));
    }
}

 

BindingViewHolder类
public class BindingViewHolder<T extends ViewDataBinding> extends RecyclerView.ViewHolder {
    private T mBinding;
    public BindingViewHolder(@NonNull T binding) {
        super(binding.getRoot());
        mBinding = binding;
    }
    public T getBinding() {
        return mBinding;
    }
}

 

超级基类,所谓超级基类即使用的时候其他适配器直接继承他即可

//SuperBaseAdapter 所有适配器基类
// add by mzw 2019-12-22
public abstract class SuperBaseAdapter<T> extends BaseRecyclerAdapter<T> {
    private int layoutId;

    public SuperBaseAdapter(Context context, int layoutId) {
        super (context);
        this.layoutId = layoutId;
    }

    @Override
    public BindingViewHolder setViewHolder(ViewGroup viewGroup) {
        ViewDataBinding binding = DataBindingUtil.inflate (mLayoutInflater, layoutId, viewGroup, false);
        return new BindingViewHolder (binding);
    }

    @Override
    public void bindData(BindingViewHolder bindingViewHolder, int positon) {
        T item = mRecordList.get (positon);
        ViewDataBinding binding = bindingViewHolder.getBinding ();
        binding.setVariable (BR.item, item); // item布局中名称要和这个设置的一致,否则这个基类不能用,或者自己改item为布局中的名称,以下类似
        binding.setVariable (
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值