Android – recyclerview universal adapter using databinding

I used databinding to create a general adapter for recyclerview. This is a small code fragment

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 the complete code in GitHub repo: recyclerview generic adapter

The problem I face is that after the loading time increases using the universal adapter recyclerview, it will display the design time layout and load the original data

resolvent:

The one you lack is binding. Executependingbindings() in binddata:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>