29

Here is the code I used in my RecycleView adapter class. I don't know this is the right way or not to use View Binding. If you have a better solution answer me. Thank you.

@Override
public CategoryAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.common_circle_image, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull CategoryAdapter.MyViewHolder holder, final int position) {
    holder.binding.img.setBackgroundResource(addAdapterData.get(position).getItemUrl());
    holder.binding.txt.setText(addAdapterData.get(position).getItemName());
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {

    CommonCircleImageBinding binding;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        binding = CommonCircleImageBinding.bind(itemView);
        binding.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                commonItemClick.onItemClick(getAdapterPosition(),"");
            }
        });
    }
}

Also, I want to know is it right to use R.layout.layout_name and ViewBinding in the same class.

Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49
Vijay Villiers
  • 423
  • 1
  • 4
  • 12
  • 2
    Hi I have written a blog post completely explaining with patterns and anti-patterns regarding the same do checkout [Androidbites|ViewBinding](https://chetangupta.net/viewbinding/) – Chetan Gupta Dec 06 '20 at 10:44

5 Answers5

40

What you need to do is pass the generated binding class object to the holder class constructor. In your example, You have common_circle_image XML file for RecyclerView item and the generated class is CommonCircleImageBinding so like this you use the onCreateViewHolder to pass the generated binding class to the ViewHolder class

@NonNull
@Override
public CategoryAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    CommonCircleImageBinding itemBinding = CommonCircleImageBinding .inflate(LayoutInflater.from(parent.getContext()), parent, false);
    return new MyViewHolder(itemBinding);
}

and use the holder class like this so you can use these fields in onBindViewHolder

static class MyViewHolder extends RecyclerView.ViewHolder {
    private TextView txt;
    private ImageView img; 

    MyViewHolder(CommonCircleImageBinding itemBinding) {
        super(itemBinding.getRoot());
        img = itemBinding.img ;
        txt = itemBinding.txt ;
    }
}
Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49
28

Here is full view binding recycler view code in java, you can do as like:

package com.jbws.myviewbindingdemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jbws.myviewbindingdemo.databinding.RowXmlViewBinding;
import com.jbws.myviewbindingdemo.pojo.ModelObject;

import java.util.ArrayList;

public class RecyclerViewListAdapter extends RecyclerView.Adapter<RecyclerViewListAdapter.ViewHolder> {
    public ArrayList<ModelObject> modelObjectArrayList;

    public RecyclerViewListAdapter(ArrayList<ModelObject> modelObjectArrayList) {
        this.modelObjectArrayList = modelObjectArrayList;
    }

    @NonNull
    @Override
    public RecyclerViewListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(RowXmlViewBinding.inflate(LayoutInflater.from(parent.getContext()),
                parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewListAdapter.ViewHolder holder, final int position) {
        ModelObject modelObject = modelObjectArrayList.get(position);
        holder.rowXmlViewBinding.txtObjectName.setText(modelObject.getFullName());
        holder.rowXmlViewBinding.btnUpdateName.setOnClickListener(view -> {
         Log.i("LOG_TAG", "Full Name: " + modelObject.getFullName);
        });
    }

    @Override
    public int getItemCount() {
        return modelObjectArrayList == null ? 0 :
                modelObjectArrayList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private RowXmlViewBinding rowXmlViewBinding;

        public ViewHolder(RowXmlViewBinding rowXmlViewBinding) {
            super(rowXmlViewBinding.getRoot());
            this.rowXmlViewBinding = rowXmlViewBinding;
        }
    }
}
Mayur Misal
  • 806
  • 10
  • 13
  • 1
    I don't know why OP suddenly decided to un-accept my answer and accepted your answer after 2.5 months. This answer doesn't add any extra info on top of my old answer. Are you and the @Vijay Villiers friends? – Somesh Kumar Jul 27 '20 at 09:09
  • Mr.Somesh Kumar, I just wrote complete answer. Also in your answer, you can check internal ViewHolder class code, in that you again bind views by using binding class. I think there is no need for this code. Finally, me and Vijay Villiers(Question owner) are not friends. – Mayur Misal Jul 28 '20 at 08:30
  • 1. My answer is also "complete". 2. What I am doing and what you are doing is similar in a sense. The only difference is that you are re-assigning the whole binding class whereas I am re-assigning only fields required. – Somesh Kumar Aug 04 '20 at 10:00
  • 1
    Somesh, your answer is more informative and explaining. thanks – Umair Mustafa Jun 21 '21 at 08:27
8

For the folks looking for a solution in Kotlin, here it is:

It's a minimal example, where the adapter gets an array of Strings and displays each of the them in a layout called recyclerview_item in a TextView called itemTextView.

It's based on @SomeshKumar's answer and answers @Vijay Villiers question on how to get rid of the private TextView txt;

Edit: New Version: I noticed the generated ...Binding has a .bind() function, so let's use it. (I guess it might be less resource-heavy?)

class SampleAdapter(private val context: Context, private val content: Array<String>) :
        RecyclerView.Adapter<SampleAdapter.CustomViewHolder>()
{
    class CustomViewHolder(view: View) : RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int) =
            CustomViewHolder(
                    // Alternatively inflate like usual, if you don't need binding
                    RecyclerviewItemBinding
                            .inflate(LayoutInflater.from(context), viewGroup, false)
                            .root
            )

    override fun getItemCount() = content.size

    override fun onBindViewHolder(viewHolder: CustomViewHolder, position: Int)
    {
        RecyclerviewItemBinding.bind(viewHolder.itemView).apply{
            itemTextView.text = content[position]
            
        }
    }
} 

Edit: Old Version:

class SampleAdapter(private val context: Context, private val content: Array<String>) :
        RecyclerView.Adapter<SampleAdapter.CustomViewHolder>()
{
    class CustomViewHolder(var viewBinding: RecyclerviewItemBinding) :
            RecyclerView.ViewHolder(viewBinding.root)

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int) =
            CustomViewHolder(
                    RecyclerviewItemBinding
                            .inflate(LayoutInflater.from(context), viewGroup, false)
            )

    override fun getItemCount() = content.size

    override fun onBindViewHolder(viewHolder: CustomViewHolder, position: Int)
    {
        viewHolder.viewBinding.apply {
            itemTextView.text = content[position]
        }
    }
}
m.reiter
  • 1,796
  • 2
  • 11
  • 31
  • Thank you for help here. How do I manage element/item click on this adapter ? @m.reiter – Arpit Patel May 13 '22 at 21:16
  • @ArpitPatel just add a ClickListener in onBindViewHolder like `RecyclerviewItemBinding.bind(viewHolder.itemView).apply{ itemTextView.text = content[position] root.setOnClickListener { /* launch the detail scren from here */) } }` – m.reiter May 15 '22 at 19:16
2

Here is full recycler view adapter class in java :

public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyViewHolder> {

    private List<Note> notes;
    private ItemNotesBinding notesBinding;

    public NotesAdapter(List<Note> notes) {
        this.notes = notes;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
        notesBinding = ItemNotesBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(notesBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        Note note = notes.get(position);
        notesBinding.tvTitle.setText(note.getNote());
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemNotesBinding notesBinding;

        public MyViewHolder(@NonNull ItemNotesBinding binding) {
            super(binding.getRoot());
            notesBinding = binding;
        }
    }
}
Chirag Prajapati
  • 337
  • 6
  • 14
0

You can create CommonCircleImageBinding directly in onCreateViewHolder by CommonCircleImageBinding.inflate(LayoutInflater.from(parent.getContext()))

Then pass it to MyViewHolder

Nam Đỗ
  • 146
  • 5