Android: Databinding and RecyclerView

If you want to do databinding in a RecyclerView:

  1. Ensure the ViewHolder has a variable for the binding.
  2. Change onCreateViewHolder to inflate your binding and pass that to ViewHolder to return
  3. Setup the bindings in onBindViewHolder

For example:

public class MyRecycler extends RecyclerView.Adapter<MyRecycler.ViewHolder>{

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ListitemBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.listitem, parent, false);
        return new ViewHolder(viewDataBinding);

    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.binding.setYoyo("Position: " + position);

    public int getItemCount() {
        return 40;

    public class ViewHolder extends RecyclerView.ViewHolder{
        private ListitemBinding binding;
        public ViewHolder(ListitemBinding itemView) {
            binding = itemView;
android android-databinding android-recyclerview

Edit on github
comments powered by Disqus
Click me