<|start_filename|>app/src/main/java/io/dwak/multiitemrecyclerview/FruitViewHolder.java<|end_filename|>
package io.dwak.multiitemrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FruitViewHolder extends RecyclerView.ViewHolder{

    public TextView name;

    public FruitViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.name);
    }

    public static FruitViewHolder create(Context context, ViewGroup parent){
        return new FruitViewHolder(LayoutInflater.from(context).inflate(R.layout.item_fruit_veg, parent, false));
    }

    public static void bind(FruitViewHolder holder, final String name, final FruitVegAdapter.FruitVegListener listener){
        holder.itemView.setBackgroundResource(R.color.fruit_bg);
        holder.name.setText(name);
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onFruitClicked(name);
                }
            }
        });
    }

}


<|start_filename|>app/src/main/java/io/dwak/multiitemrecyclerview/MainActivity.java<|end_filename|>
package io.dwak.multiitemrecyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements FruitVegAdapter.FruitVegListener{
    private RecyclerView recyclerView;
    private boolean listMode = true;
    private LinearLayoutManager linearLayoutManager;
    private GridLayoutManager gridLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        final FruitVegAdapter adapter = new FruitVegAdapter(this, this);

        String[] fruits = getResources().getStringArray(R.array.fruits);
        String[] veggies = getResources().getStringArray(R.array.vegetables);

        for (String veg : veggies) {
            adapter.addVeg(veg);
        }

        for (String fruit : fruits) {
            adapter.addFruit(fruit);
        }

        recyclerView.setAdapter(adapter);

        linearLayoutManager = new LinearLayoutManager(this);
        gridLayoutManager = new GridLayoutManager(this, 3);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(adapter.getItemViewType(position)){
                    case FruitVegItem.FRUIT:
                        return 1;
                    case FruitVegItem.VEGETABLE:
                        return 3;
                }
                return 0;
            }
        });
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.action_toggle){
            listMode = !listMode;
            recyclerView.setLayoutManager(listMode ? linearLayoutManager : gridLayoutManager);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFruitClicked(String message) {
        showToast("Fruit:" + message);
    }

    @Override
    public void onVeggieClicked(String message) {
        showToast("Veggie:" + message);
    }

    void showToast(String message){
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}


<|start_filename|>app/src/main/java/io/dwak/multiitemrecyclerview/FruitVegItem.java<|end_filename|>
package io.dwak.multiitemrecyclerview;

import android.support.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class FruitVegItem<T> {
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({FRUIT, VEGETABLE})
    public @interface ViewType {
    }
    public static final int FRUIT = 0;
    public static final int VEGETABLE = 1;

    T object;
    @ViewType int viewType;

    public FruitVegItem(T object, int viewType) {
        this.object = object;
        this.viewType = viewType;
    }
}


<|start_filename|>app/src/main/java/io/dwak/multiitemrecyclerview/VegViewHolder.java<|end_filename|>
package io.dwak.multiitemrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class VegViewHolder extends RecyclerView.ViewHolder {

    public TextView name;

    public VegViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.name);
    }

    public static VegViewHolder create(Context context, ViewGroup parent) {
        return new VegViewHolder(LayoutInflater.from(context).inflate(R.layout.item_fruit_veg, parent, false));
    }

    public static void bind(VegViewHolder holder, final String name, final FruitVegAdapter.FruitVegListener listener) {
        holder.itemView.setBackgroundResource(R.color.veg_bg);
        holder.name.setText(name);
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onVeggieClicked(name);
                }
            }
        });
    }

}


<|start_filename|>app/src/main/java/io/dwak/multiitemrecyclerview/FruitVegAdapter.java<|end_filename|>
package io.dwak.multiitemrecyclerview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class FruitVegAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    @Nullable private FruitVegListener listener;
    private final List<FruitVegItem> list;
    private final Context context;

    public FruitVegAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        listener = null;
    }

    public FruitVegAdapter(Context context, FruitVegListener listener){
        this.context = context;
        this.listener = listener;
        list = new ArrayList<>();
    }

    public void addFruit(String fruitName){
        list.add(new FruitVegItem<>(fruitName, FruitVegItem.FRUIT));
        notifyItemInserted(list.size());
    }

    public void addVeg(String vegName){
        list.add(new FruitVegItem<>(vegName, FruitVegItem.VEGETABLE));
        notifyItemInserted(list.size());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        switch (viewType){
            case FruitVegItem.FRUIT:
                return FruitViewHolder.create(context, viewGroup);
            case FruitVegItem.VEGETABLE:
                return VegViewHolder.create(context, viewGroup);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        switch (getItemViewType(position)){
            case FruitVegItem.FRUIT:
                FruitViewHolder.bind((FruitViewHolder) viewHolder,
                                     (String) list.get(position).object,
                                     listener);
                break;
            case FruitVegItem.VEGETABLE:
                VegViewHolder.bind((VegViewHolder) viewHolder,
                                   (String) list.get(position).object,
                                   listener);
                break;

        }
    }

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

    @Override
    public int getItemViewType(int position) {
        return list.get(position).viewType;
    }

    public void setListener(FruitVegListener listener) {
        this.listener = listener;
    }

    public interface FruitVegListener {
        void onFruitClicked(String message);
        void onVeggieClicked(String message);
    }
}
