RecyclerView Custom Adapter with Multiple View Types in Java – Android

As we all know that RecyclerView is a UI component to show lists in Android. In this tutorial, you will learn, how to create a Custom RecyclerView Adapter with Multiple View Types in Java Android. Following is the general RecyclerView Adapter with multiple view types. But if you are looking for a use case with all steps take a look at another article, Multiple View Types in RecyclerView with Chat App Example.

Following is the Java code, here you can also find Kotlin Custom Adapter Adapter to Add Multiple View Types in RecyclerView.

Sample Custom Adapter with Multiple View Types In Android – Java

public class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final Context context;
    ArrayList<String> list; // ArrayList of your Data Model
    final int VIEW_TYPE_ONE = 1;
    final int VIEW_TYPE_TWO = 2;

    public CustomAdapter(Context context, ArrayList<String> list) { // you can pass other parameters in constructor
        this.context = context;
        this.list = list;
    }

    private class ViewHolder1 extends RecyclerView.ViewHolder {

        TextView yourView;
        ViewHolder1(final View itemView) {
            super(itemView);
            yourView = itemView.findViewById(R.id.yourView); // Initialize your All views prensent in list items
        }
        void bind(int position) {
            // This method will be called anytime a list item is created or update its data
            //Do your stuff here
            yourView.setText(list.get(position));
        }
    }
    
    private class ViewHolder2 extends RecyclerView.ViewHolder {

        TextView yourView;
        ViewHolder2(final View itemView) {
            super(itemView);
            yourView = itemView.findViewById(R.id.yourView); // Initialize your All views prensent in list items
        }
        void bind(int position) {
            // This method will be called anytime a list item is created or update its data
            //Do your stuff here
            yourView.setText(list.get(position));
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       if (viewType == VIEW_TYPE_ONE) {
           return new ViewHolder1(LayoutInflater.from(context).inflate(R.layout.your_list_item_1, parent, false));
       }
       //if its not VIEW_TYPE_ONE then its VIEW_TYPE_TWO
       return new ViewHolder2(LayoutInflater.from(context).inflate(R.layout.your_list_item_2, parent, false));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (list.get(position).type == Something) { // put your condition, according to your requirements
            ((ViewHolder1) holder).bind(position);
        } else {
            ((ViewHolder2) holder).bind(position);
        }
        
    }

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

    @Override
    public int getItemViewType(int position) {
        // here you can get decide from your model's ArrayList, which type of view you need to load. Like
        if (list.get(position).type == Something) { // put your condition, according to your requirements
            return VIEW_TYPE_ONE;
        }
        return VIEW_TYPE_TWO;
    }
}

Don’t forget to add the following RecyclerView dependency in your App level build.gradle file

implementation "androidx.recyclerview:recyclerview:1.0.0" 
// or if you are using support library instead of Androidx then 
// implementation 'com.android.support:recyclerview-v7:28.0.0'

In this sample Custom Adapter class, you need to change layout names & conditions as per your requirements. It’s also mentioned in the comments. You can use this sample class in your all projects wherever you need to implement RecyclerView.

If it’s not clear, have a look at our following step by step tutorial.

Multiple View Types in RecyclerView with Chat App Example.

That’s it. Enjoy 🙂

You can also find other useful helping articles, code snippets and tutorials in our Coding Articles & Tutorials Knowledge Base.

Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

3 thoughts on “RecyclerView Custom Adapter with Multiple View Types in Java – Android

  1. I’ve been exploring for a little bit for any high-quality articles or blog posts on this sort of area . Exploring in Yahoo I eventually stumbled upon this site. Reading this information So i’m glad to show that I’ve a very just right uncanny feeling I discovered just what I needed. I so much for sure will make sure to don’t overlook this website and provides it a glance on a constant basis.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.