具有DataBinding的RecyclerView通用适配器 [英] RecyclerView generic adapter with DataBinding

查看:784
本文介绍了具有DataBinding的RecyclerView通用适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用DataBinding为RecyclerView创建了通用适配器。这里是小代码片段

  public class RecyclerAdapter< T,VM extends ViewDataBinding>扩展RecyclerView.Adapter< RecyclerAdapter.RecyclerViewHolder> {
private final Context context;
private ArrayList< T>项目;
private int layoutId;
private RecyclerCallback< VM,T> bindingInterface;

public RecyclerAdapter(Context context,ArrayList< T> items,int layoutId,RecyclerCallback< VM,T> bindingInterface){
this.items = items;
this.context = context;
this.layoutId = layoutId;
this.bindingInterface = bindingInterface;
}

公共类RecyclerViewHolder扩展了RecyclerView.ViewHolder {

虚拟机绑定;

public RecyclerViewHolder(View view){
super(view);
binding = DataBindingUtil.bind(view);
}

public void bindData(T model){
bindingInterface.bindData(binding,model);

$ b}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent,
int viewType){
View v = LayoutInflater.from(parent.getContext())
.inflate(layoutId,parent,false);
返回新的RecyclerViewHolder(v);

$ b @Override
public void onBindViewHolder(RecyclerAdapter.RecyclerViewHolder holder,int position){
T item = items.get(position);
holder.bindData(item);
}

@Override
public int getItemCount(){
if(items == null){
return 0;
}
return items.size();


$ / code $ / pre

你可以在Github repo中找到完整的代码: Recyclerview-Generic-Adapter



我面临的问题是在使用通用适配器 RecyclerView 加载时间增加之后,它显示了设计时间布局并加载了原始数据。 / p>

解决方案

您缺少的部分是 binding.executePendingBindings() bindData

  public void bindData(T model){
bindingInterface.bindData(binding,model);
binding.executePendingBindings();
}


I have created generic adapter for RecyclerView by using DataBinding. Here is small code snippet

public class RecyclerAdapter<T, VM extends ViewDataBinding> extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
private final Context context;
private ArrayList<T> items;
private int layoutId;
private RecyclerCallback<VM, T> bindingInterface;

public RecyclerAdapter(Context context, ArrayList<T> items, int layoutId, RecyclerCallback<VM, T> bindingInterface) {
    this.items = items;
    this.context = context;
    this.layoutId = layoutId;
    this.bindingInterface = bindingInterface;
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder {

    VM binding;

    public RecyclerViewHolder(View view) {
        super(view);
        binding = DataBindingUtil.bind(view);
    }

    public void bindData(T model) {
        bindingInterface.bindData(binding, model);
    }

}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent,
                                             int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(layoutId, parent, false);
    return new RecyclerViewHolder(v);
}

@Override
public void onBindViewHolder(RecyclerAdapter.RecyclerViewHolder holder, int position) {
    T item = items.get(position);
    holder.bindData(item);
}

@Override
public int getItemCount() {
    if (items == null) {
        return 0;
    }
    return items.size();
}
}

You can find full code in Github repo : Recyclerview-Generic-Adapter

The problem i am facing is after using generic adapter RecyclerView loading time increase and for a second it shows design time layout and than loads original data.

解决方案

The piece you're missing is the binding.executePendingBindings() in bindData:

public void bindData(T model) {
    bindingInterface.bindData(binding, model);
    binding.executePendingBindings();
}

这篇关于具有DataBinding的RecyclerView通用适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆