How to create RecyclerView with multiple view types

To create a RecyclerView with multiple view types in Android, you will need to use a RecyclerView.Adapter that supports multiple view types.

To do this, you can define a class that extends RecyclerView.Adapter, and overrides the getItemViewType() method to return a unique integer value for each view type.

Here is an example of how you can create a RecyclerView.Adapter with two view types:

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private static final int VIEW_TYPE_ITEM_1 = 1;
    private static final int VIEW_TYPE_ITEM_2 = 2;

    private String[] mData;

    public MyAdapter(String[] data) {
        mData = data;

    public int getItemViewType(int position) {
        // Return a unique integer value for each view type
        if (position % 2 == 0) {
            return VIEW_TYPE_ITEM_1;
        } else {
            return VIEW_TYPE_ITEM_2;

    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;
        if (viewType == VIEW_TYPE_ITEM_1) {
            view = inflater.inflate(R.layout.view_type_1, parent, false);