1056

From Create dynamic lists with RecyclerView:

When we create a RecyclerView.Adapter we have to specify ViewHolder that will bind with the adapter.

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

    private String[] mDataset;

    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.some_layout, parent, false);

        //findViewById...

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset[position]);
    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

Is it possible to create RecyclerView with multiple view types?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pongpat
  • 13,248
  • 9
  • 38
  • 51
  • 6
    On top of Anton's answer, see my answer here: http://stackoverflow.com/questions/25914003/recyclerview-and-handling-different-type-of-row-inflation/29362643#29362643 – ticofab Mar 31 '15 at 07:10
  • 1
    Check these link which can also useful for you http://stackoverflow.com/a/39972276/3946958 – Ravindra Kushwaha Oct 19 '16 at 13:39
  • 2
    Good tutorial here: https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView – Gene Bo Nov 17 '16 at 00:42
  • 1
    Check these link it is workable http://stackoverflow.com/questions/39971350/recycle-view-inflating-different-row-getting-exception-while-binding-the-data/39972276#39972276 If there is issue than please let me know – Ravindra Kushwaha Jan 23 '17 at 06:24
  • 1
    The Great library to implement it https://github.com/vivchar/RendererRecyclerViewAdapter – Vitaly Jan 28 '18 at 10:50
  • Check this blog https://techtibet.com/blog/android/android-recyclerview-with-multiple-view-type/ its well and clear – Kunchok Tashi Mar 29 '20 at 12:18
  • Check this video tutorial: https://youtu.be/UZwiKdrm768 I felt it helpful. Hope that helps you too. – Chandu Jul 16 '20 at 05:00
  • 1
    Well, it's 2021 and I thought this answer needs an update. AndroidX now has something called as ConcatAdapter which beginners may find easier to use. check this for a simple tutorial: https://blog.mindorks.com/implementing-merge-adapter-in-android-tutorial – Shikhar Deep Jan 19 '21 at 13:53

23 Answers23

1510

Yes, it's possible. Just implement getItemViewType(), and take care of the viewType parameter in onCreateViewHolder().

So you do something like:

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    class ViewHolder0 extends RecyclerView.ViewHolder {
        ...
        public ViewHolder0(View itemView){
        ...
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        ...
        public ViewHolder2(View itemView){
        ...
    }

    @Override
    public int getItemViewType(int position) {
        // Just as an example, return 0 or 2 depending on position
        // Note that unlike in ListView adapters, types don't have to be contiguous
        return position % 2 * 2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case 0: return new ViewHolder0(...);
             case 2: return new ViewHolder2(...);
             ...
         }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        switch (holder.getItemViewType()) {
            case 0:
                ViewHolder0 viewHolder0 = (ViewHolder0)holder;
                ...
                break;

            case 2:
                ViewHolder2 viewHolder2 = (ViewHolder2)holder;
                ...
                break;
        }
    }
}
Anton Savin
  • 40,838
  • 8
  • 54
  • 90
  • What is the code in viewholder will look like? Is one viewholder suppose to hold one view type? – Pongpat Oct 07 '14 at 22:45
  • 4
    @GZ95 generally yes, because different view types can correspond to completely different view layouts. ViewHolder is a common design pattern in android described [here](http://developer.android.com/training/improving-layouts/smooth-scrolling.html). Before it was optional to use, now RecyclerView enforces its usage. – Anton Savin Oct 07 '14 at 22:53
  • 4
    That's my point since only one ViewHolder is available in one RecyclerView.Adapter how you going to add multiple to it? – Pongpat Oct 07 '14 at 22:56
  • 1
    @GZ95 you can create a subclass of `MyAdapter.ViewHolder` for each view type. – Anton Savin Oct 07 '14 at 23:33
  • 52
    Then you have to cast viewholder type in onBindViewHolder() method which I think it defeat the purpose of generic type. By the way, thank you for your answer. – Pongpat Oct 08 '14 at 04:53
  • 1
    Well, using `ListView` you would also perform a cast (moreover, you do it even if you have only one view type, so here there is some improvement). To my knowledge, Java doesn't have variadic generics, so they have to use what they have. – Anton Savin Oct 08 '14 at 07:12
  • 37
    You can create a BaseHolder and extend it for all required types. Then add an abstract setData, which would be overriden (overrode?) in implementation holders. This way you let the language handle type differences. Though it only works if you have a single set of data that all list items can interpret. – DariusL Dec 05 '14 at 14:02
  • 3
    What about different layout file? I want to change layout on `optionMenuItem`. How its possible? @AntonSavin – Pratik Butani Feb 23 '15 at 06:59
  • 1
    @PratikButani-AndroidButs when you create particular `ViewHolder` you initialize it with some `View` which you can create in whatever way you prefer - in particular, you can inflate it from whatever XML layout resource you want. – Anton Savin Feb 28 '15 at 19:50
  • 1
    http://stackoverflow.com/q/28581712/1318946 @AntonSavin I am just stuck with this problem – Pratik Butani Mar 02 '15 at 04:32
  • 1
    see my answer here: http://stackoverflow.com/questions/25914003/recyclerview-and-handling-different-type-of-row-inflation/29362643#29362643 – ticofab Mar 31 '15 at 07:10
  • 2
    this works fine adding multiple viewTypes but the problem is with the binding data .while scrolling the data changes randomly. – Hemant Shori Apr 28 '15 at 06:45
  • 1
    @Hemant probably you implemented it wrong. If you have a specific problem, it's better to ask a new question. – Anton Savin Apr 28 '15 at 08:25
  • 1
    Enums were nice to model the switch statement – JakeWilson801 Dec 22 '15 at 01:27
  • 1
    @AntonSavin: I am facing a related problem ... could you please help me out with my question [here](http://stackoverflow.com/q/34431734/3287204) ? – Yash Sampat Dec 27 '15 at 10:24
  • 7
    Your ViewHolders should be static if they reside in your RecyclerView Adapter – Samer Feb 01 '16 at 09:53
  • 1
    have you example about 3 or more ? –  Apr 15 '16 at 12:28
  • 2
    @delive What's a problem about 3 or more? Create appropriate number of view holders and that's it. – Anton Savin Apr 15 '16 at 17:13
  • 1
    @Samer, can you explain why ViewHolders should be static? – AJW Jul 03 '16 at 18:34
  • 1
    What is the difference between getItemViewType and getViewType? getItemViewType does not seem to be a default method in RecyclerAdapter. – Martin Erlic Jul 06 '16 at 08:37
  • 1
    @bluemunch I don't understand your question, AFAIK there's no such thing as `getViewType()` in Android. – Anton Savin Jul 07 '16 at 01:01
  • 1
    Check another solution in these link which can also useful for you http://stackoverflow.com/a/39972276/3946958 – Ravindra Kushwaha Oct 19 '16 at 13:40
  • 1
    How about position of onBindViewHolder. For example, view1 has 8 items and view2 has 23 items. – drojokef Feb 12 '17 at 23:55
  • 1
    Thank you @AntonSavin! I don't understand how can I specify what view has to use a particolar `ViewHolder`. I mean, I create the adapter passing the layout for the `inflate()`, I don't understand what the `getItemViewType() ` should get as parameter. How can I say 'if I use the adapter for the recyclerView with id _R.id.r1_ use `ViewHolder0`, while if I use recyclerView with id _R.id.r2_ use `ViewHolder1`'? – A. Wolf Apr 24 '17 at 16:21
  • 1
    @A.Wolf `getItemViewType()`'s parameter is item's index in the list. It's up to your internal logic to decide what type that item should have. If you have two different instances of `RecyclerView` you probably will create two different adaptor classes for them, so you implement different logic. – Anton Savin Apr 24 '17 at 17:10
  • 1
    My idea is to have only one Adapter for recyclerview which has one image and one textview and i infilate a different layout from the adapter constructor. I want to change behaviour based on that value. I have to do a switch in that method based on resource layout id? – A. Wolf Apr 24 '17 at 17:20
  • 1
    A better way is to get the actual item from your list and then decide what you want done with it. `itemsList.get(position)` – Vahid Amiri Sep 02 '17 at 17:07
  • The 0 or 2 really confused me at first reading this answer. I suggest simplifying it to returning 0 or 1, use constants, and remove a bunch of the code to make this answer really dead simple to grok at first glance. – Dave Thomas Nov 20 '17 at 23:29
  • This works fine adding multiple viewTypes but the problem is with the binding data. How can i fix the random changing data?? I've got 3 viewTypes already defined. – Fyruz Apr 01 '19 at 16:48
  • @GianmarcoFerruzzi Better ask this as a separate question – Anton Savin Apr 01 '19 at 22:17
  • Also have a look at my question https://stackoverflow.com/q/58693741/3496570 – Zar E Ahmer Nov 04 '19 at 12:49
  • You are heavily relying on position. What if it's not position relevant ? – RonTLV Nov 21 '19 at 11:59
  • @RonTLV in general you are supposed to have a dataset that you want to visualize. Get an item based on position and determine how to render it based on that. – Anton Savin Nov 22 '19 at 17:26
  • This way is better if you have completely different view types... – DragonFire Aug 25 '21 at 02:15
97

If the layouts for view types are only a few and binding logics are simple, follow Anton's solution. But the code will be messy if you need to manage the complex layouts and binding logics.

I believe the following solution will be useful for someone who need to handle complex view types.

Base DataBinder class

abstract public class DataBinder<T extends RecyclerView.ViewHolder> {

    private DataBindAdapter mDataBindAdapter;

    public DataBinder(DataBindAdapter dataBindAdapter) {
        mDataBindAdapter = dataBindAdapter;
    }

    abstract public T newViewHolder(ViewGroup parent);

    abstract public void bindViewHolder(T holder, int position);

    abstract public int getItemCount();

......

}

The functions needed to define in this class are pretty much same as the adapter class when creating the single view type.

For each view type, create the class by extending this DataBinder.

Sample DataBinder class

public class Sample1Binder extends DataBinder<Sample1Binder.ViewHolder> {

    private List<String> mDataSet = new ArrayList();

    public Sample1Binder(DataBindAdapter dataBindAdapter) {
        super(dataBindAdapter);
    }

    @Override
    public ViewHolder newViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.layout_sample1, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void bindViewHolder(ViewHolder holder, int position) {
        String title = mDataSet.get(position);
        holder.mTitleText.setText(title);
    }

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

    public void setDataSet(List<String> dataSet) {
        mDataSet.addAll(dataSet);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView mTitleText;

        public ViewHolder(View view) {
            super(view);
            mTitleText = (TextView) view.findViewById(R.id.title_type1);
        }
    }
}

In order to manage DataBinder classes, create an adapter class.

Base DataBindAdapter class

abstract public class DataBindAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return getDataBinder(viewType).newViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        int binderPosition = getBinderPosition(position);
        getDataBinder(viewHolder.getItemViewType()).bindViewHolder(viewHolder, binderPosition);
    }

    @Override
    public abstract int getItemCount();

    @Override
    public abstract int getItemViewType(int position);

    public abstract <T extends DataBinder> T getDataBinder(int viewType);

    public abstract int getPosition(DataBinder binder, int binderPosition);

    public abstract int getBinderPosition(int position);

......

}

Create the class by extending this base class, and then instantiate DataBinder classes and override abstract methods

  1. getItemCount
    Return the total item count of DataBinders

  2. getItemViewType
    Define the mapping logic between the adapter position and view type.

  3. getDataBinder
    Return the DataBinder instance based on the view type

  4. getPosition
    Define convert logic to the adapter position from the position in the specified DataBinder

  5. getBinderPosition
    Define convert logic to the position in the DataBinder from the adapter position

I left a more detailed solution and samples on GitHub, so please refer to RecyclerView-MultipleViewTypeAdapter if you need.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yqritc
  • 2,021
  • 12
  • 4
  • 4
    I'm slightly confused by your code, maybe you can help me, I would not like my views to be defined by the positions in the list but by their viewtypes. It appears as if the views in your code are determined based on their positions ie. so if im on position 1, view 1 is displayed, position 3, view 3 is displayed and everything else displays position 2's view. I don't want to base my views on positions but on viewtypes - so if i specify that the view type is images, it should display the image. How can i do that? – Simon Apr 25 '15 at 18:19
  • Sorry, I could not fully understand your question..., but you need to write the logic somewhere in order to bind the position and viewtype. – yqritc Apr 27 '15 at 03:48
  • 2
    this code is not confused , this is a RecyclerView Adapter pattern, and this should be excepted like correct answer of the question. Follow the link of @yqritc, spend a little time to discovered and you will have perfect pattern for RecyclerView with different type of layouts. – Stoycho Andreev Apr 27 '15 at 05:02
  • newbe here, `public class DataBinder` can someone tell me what `` is called, so I can google if I get the term . Also when I say `abstract public class DataBinder` does that mean that this class is of type `ViewHolder`, so in result every class that extends this class will be of type`viewHolder` is that the idea? – rgv Jul 16 '15 at 19:09
  • @yqritc I have used the library you have mentioned here , I am getting problem with multiple DataBinders , I have posted a question on Stackoverflow .Here is the link http://stackoverflow.com/questions/33894351/manage-multiple-view-types-recyclerview . Help me plz. – Shishupal Shakya Nov 24 '15 at 13:28
  • Very clean solution. It works smooth and elegantly. @Simon I understood your question. You should create some kind of logic in your adapter class. For instance; you can you `position %3 == 0` instead of `position == 0`. But this might not be enough depending of the size of your dataset for that type. – Erdi İzgi Dec 01 '16 at 14:58
  • 1
    @cesards you made me to go refresh my knowledge again on polymorphism lol.... Java isn't bad – Paul Okeke Sep 06 '18 at 20:31
  • @rgv it's generic type .. search for Generics in Java – Deeptimay Routray Jun 28 '21 at 18:47
  • For future readers, this is b*llcrap – Farid Feb 14 '22 at 19:57
  • @StoychoAndreev This should never be an accepted answer under any circumstance. The guy, probably, wrote this when he was new to `RecyclerView`. Just read review it carefully. There is no way someone in their right mind would call this a solution – Farid Feb 14 '22 at 20:06
  • @Simon You were totally right about the code being confusing. This code makes no sense at all in any way. Just check the repository in the library (probably you did back then). Senseless bag of code supposed to make it easy but turned into sh*t then posted as an "answer" – Farid Feb 14 '22 at 20:10
48

The below is not pseudocode. I have tested it and it has worked for me.

I wanted to create a headerview in my recyclerview and then display a list of pictures below the header which the user can click on.

I used a few switches in my code and don't know if that is the most efficient way to do this, so feel free to give your comments:

   public class ViewHolder extends RecyclerView.ViewHolder{

        //These are the general elements in the RecyclerView
        public TextView place;
        public ImageView pics;

        //This is the Header on the Recycler (viewType = 0)
        public TextView name, description;

        //This constructor would switch what to findViewBy according to the type of viewType
        public ViewHolder(View v, int viewType) {
            super(v);
            if (viewType == 0) {
                name = (TextView) v.findViewById(R.id.name);
                decsription = (TextView) v.findViewById(R.id.description);
            } else if (viewType == 1) {
                place = (TextView) v.findViewById(R.id.place);
                pics = (ImageView) v.findViewById(R.id.pics);
            }
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,
                                         int viewType)
    {
        View v;
        ViewHolder vh;
        // create a new view
        switch (viewType) {
            case 0: //This would be the header view in my Recycler
                v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.recyclerview_welcome, parent, false);
                vh = new ViewHolder(v,viewType);
                return  vh;
            default: //This would be the normal list with the pictures of the places in the world
                v = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.recyclerview_picture, parent, false);
                vh = new ViewHolder(v, viewType);
                v.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, nextActivity.class);
                        intent.putExtra("ListNo",mRecyclerView.getChildPosition(v));
                        mContext.startActivity(intent);
                    }
                });
                return vh;
        }
    }

    //Overridden so that I can display custom rows in the recyclerview
    @Override
    public int getItemViewType(int position) {
        int viewType = 1; //Default is 1
        if (position == 0) viewType = 0; //If zero, it will be a header view
        return viewType;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //position == 0 means it's the info header view on the Recycler
        if (position == 0) {
            holder.name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext,"name clicked", Toast.LENGTH_SHORT).show();
                }
            });
            holder.description.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext,"description clicked", Toast.LENGTH_SHORT).show();
                }
            });
            //This means it is beyond the headerview now as it is no longer 0. For testing purposes, I'm alternating between two pics for now
        } else if (position > 0) {
           holder.place.setText(mDataset[position]);
            if (position % 2 == 0) {
               holder.pics.setImageDrawable(mContext.getResources().getDrawable(R.drawable.pic1));
            }
            if (position % 2 == 1) {
                holder.pics.setImageDrawable(mContext.getResources().getDrawable(R.drawable.pic2));
            }
        }
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simon
  • 19,658
  • 27
  • 149
  • 217
  • 1
    This is a nice, what if I wanted multiple headers at dynamic positions? Say, a list of items with headers defining categories. You solution would seem to require the special headers to be at predetermined int positions. – Bassinator Aug 04 '17 at 18:32
40

Here is a complete sample to show a RecyclerView with two types, the view type decided by the object.

Class model

open class RecyclerViewItem
class SectionItem(val title: String) : RecyclerViewItem()
class ContentItem(val name: String, val number: Int) : RecyclerViewItem()

Adapter code

const val VIEW_TYPE_SECTION = 1
const val VIEW_TYPE_ITEM = 2

class UserAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var data = listOf<RecyclerViewItem>()

    override fun getItemViewType(position: Int): Int {
        if (data[position] is SectionItem) {
            return VIEW_TYPE_SECTION
        }
        return VIEW_TYPE_ITEM
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == VIEW_TYPE_SECTION) {
            return SectionViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.item_user_section, parent, false)
            )
        }
        return ContentViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_user_content, parent, false)
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = data[position]
        if (holder is SectionViewHolder && item is SectionItem) {
            holder.bind(item)
        }
        if (holder is ContentViewHolder && item is ContentItem) {
            holder.bind(item)
        }
    }

    internal inner class SectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(item: SectionItem) {
            itemView.text_section.text = item.title
        }
    }

    internal inner class ContentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(item: ContentItem) {
            itemView.text_name.text = item.name
            itemView.text_number.text = item.number.toString()
        }
    }
}

item_user_section.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text_section"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#eee"
    android:padding="16dp" />

item_user_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="32dp">

    <TextView
        android:id="@+id/text_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Name" />

    <TextView
        android:id="@+id/text_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Example using

val dataSet = arrayListOf<RecyclerViewItem>(
    SectionItem("A1"),
    ContentItem("11", 11),
    ContentItem("12", 12),
    ContentItem("13", 13),

    SectionItem("A2"),
    ContentItem("21", 21),
    ContentItem("22", 22),

    SectionItem("A3"),
    ContentItem("31", 31),
    ContentItem("32", 32),
    ContentItem("33", 33),
    ContentItem("33", 34),
)

recyclerAdapter.data = dataSet
recyclerAdapter.notifyDataSetChanged()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Linh
  • 57,942
  • 23
  • 262
  • 279
31

Create a different ViewHolder for different layouts

Enter image description here

RecyclerView can have any number of viewholders you want, but for better readability let’s see how to create one with two ViewHolders.

It can be done in three simple steps

  1. Override public int getItemViewType(int position)
  2. Return different ViewHolders based on the ViewType in onCreateViewHolder() method
  3. Populate View based on the itemViewType in onBindViewHolder() method

Here is a small code snippet:

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

   private static final int LAYOUT_ONE = 0;
   private static final int LAYOUT_TWO = 1;

   @Override
   public int getItemViewType(int position)
   {
      if(position==0)
        return LAYOUT_ONE;
      else
        return LAYOUT_TWO;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

      View view = null;
      RecyclerView.ViewHolder viewHolder = null;

      if(viewType==LAYOUT_ONE)
      {
          view = LayoutInflater.from(parent.getContext()).inflate(R.layout.one,parent,false);
          viewHolder = new ViewHolderOne(view);
      }
      else
      {
          view = LayoutInflater.from(parent.getContext()).inflate(R.layout.two,parent,false);
          viewHolder= new ViewHolderTwo(view);
      }

      return viewHolder;
   }

   @Override
   public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

      if(holder.getItemViewType() == LAYOUT_ONE)
      {
            // Typecast Viewholder
            // Set Viewholder properties
            // Add any click listener if any
      }
      else {
        ViewHolderOne vaultItemHolder = (ViewHolderOne) holder;
        vaultItemHolder.name.setText(displayText);
        vaultItemHolder.name.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
            .......
           }
         });
       }
   }

  //****************  VIEW HOLDER 1 ******************//

   public class ViewHolderOne extends RecyclerView.ViewHolder {

      public TextView name;

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


   //****************  VIEW HOLDER 2 ******************//

   public class ViewHolderTwo extends RecyclerView.ViewHolder {

      public ViewHolderTwo(View itemView) {
         super(itemView);

        ..... Do something
      }
   }
}

getItemViewType(int position) is the key.

In my opinion, the starting point to create this kind of recyclerView is the knowledge of this method. Since this method is optional to override, it is not visible in RecylerView class by default which in turn makes many developers (including me) wonder where to begin.

Once you know that this method exists, creating such RecyclerView would be a cakewalk.

Let's see one example to prove my point. If you want to show two layout at alternate positions do this

@Override
public int getItemViewType(int position)
{
   if(position%2==0)       // Even position
     return LAYOUT_ONE;
   else                   // Odd position
     return LAYOUT_TWO;
}

Relevant Links:

Check out the project where I have implemented this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
26

Yes, it is possible.

Write a generic view holder:

    public abstract class GenericViewHolder extends RecyclerView.ViewHolder
{
    public GenericViewHolder(View itemView) {
        super(itemView);
    }

    public abstract  void setDataOnView(int position);
}

then create your view holders and make them extend the GenericViewHolder. For example, this one:

     public class SectionViewHolder extends GenericViewHolder{
    public final View mView;
    public final TextView dividerTxtV;

    public SectionViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        dividerTxtV = (TextView) mView.findViewById(R.id.dividerTxtV);
    }

    @Override
    public void setDataOnView(int position) {
        try {
            String title= sections.get(position);
            if(title!= null)
                this.dividerTxtV.setText(title);
        }catch (Exception e){
            new CustomError("Error!"+e.getMessage(), null, false, null, e);
        }
    }
}

then the RecyclerView.Adapter class will look like this one:

public class MyClassRecyclerViewAdapter extends RecyclerView.Adapter<MyClassRecyclerViewAdapter.GenericViewHolder> {

@Override
public int getItemViewType(int position) {
     // depends on your problem
     switch (position) {
         case : return VIEW_TYPE1;
         case : return VIEW_TYPE2;
         ...
     }
}

    @Override
   public GenericViewHolder onCreateViewHolder(ViewGroup parent, int viewType)  {
    View view;
    if(viewType == VIEW_TYPE1){
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout1, parent, false);
        return new SectionViewHolder(view);
    }else if( viewType == VIEW_TYPE2){
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout2, parent, false);
        return new OtherViewHolder(view);
    }
    // Cont. other view holders ...
    return null;
   }

@Override
public void onBindViewHolder(GenericViewHolder holder, int position) {
    holder.setDataOnView(position);
}
Islam Assi
  • 991
  • 1
  • 13
  • 18
  • 1
    How to use then in an activity? Should the type be passed throught the method? – skm Aug 12 '18 at 04:29
  • 1
    How to use this Adapter in Activity? And how does it recognize which Type is in the list – skm Aug 12 '18 at 07:14
18

Yes, it is possible.

In your adapter getItemViewType Layout like this ....

public class MultiViewTypeAdapter extends RecyclerView.Adapter {

    private ArrayList<Model>dataSet;
    Context mContext;
    int total_types;
    MediaPlayer mPlayer;
    private boolean fabStateVolume = false;

    public static class TextTypeViewHolder extends RecyclerView.ViewHolder {

        TextView txtType;
        CardView cardView;

        public TextTypeViewHolder(View itemView) {
            super(itemView);

            this.txtType = (TextView) itemView.findViewById(R.id.type);
            this.cardView = (CardView) itemView.findViewById(R.id.card_view);
        }
    }

    public static class ImageTypeViewHolder extends RecyclerView.ViewHolder {

        TextView txtType;
        ImageView image;

        public ImageTypeViewHolder(View itemView) {
            super(itemView);

            this.txtType = (TextView) itemView.findViewById(R.id.type);
            this.image = (ImageView) itemView.findViewById(R.id.background);
        }
    }

    public static class AudioTypeViewHolder extends RecyclerView.ViewHolder {

        TextView txtType;
        FloatingActionButton fab;

        public AudioTypeViewHolder(View itemView) {
            super(itemView);

            this.txtType = (TextView) itemView.findViewById(R.id.type);
            this.fab = (FloatingActionButton) itemView.findViewById(R.id.fab);
        }
    }

    public MultiViewTypeAdapter(ArrayList<Model>data, Context context) {
        this.dataSet = data;
        this.mContext = context;
        total_types = dataSet.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view;
        switch (viewType) {
            case Model.TEXT_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_type, parent, false);
                return new TextTypeViewHolder(view);
            case Model.IMAGE_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_type, parent, false);
                return new ImageTypeViewHolder(view);
            case Model.AUDIO_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_type, parent, false);
                return new AudioTypeViewHolder(view);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {

        switch (dataSet.get(position).type) {
            case 0:
                return Model.TEXT_TYPE;
            case 1:
                return Model.IMAGE_TYPE;
            case 2:
                return Model.AUDIO_TYPE;
            default:
                return -1;
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

        Model object = dataSet.get(listPosition);
        if (object != null) {
            switch (object.type) {
                case Model.TEXT_TYPE:
                    ((TextTypeViewHolder) holder).txtType.setText(object.text);

                    break;
                case Model.IMAGE_TYPE:
                    ((ImageTypeViewHolder) holder).txtType.setText(object.text);
                    ((ImageTypeViewHolder) holder).image.setImageResource(object.data);
                    break;
                case Model.AUDIO_TYPE:

                    ((AudioTypeViewHolder) holder).txtType.setText(object.text);

            }
        }
    }

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

For the reference link: Android RecyclerView Example – Multiple ViewTypes

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sayan Manna
  • 584
  • 7
  • 13
  • I reformatted my code to mimic this snippet and it now works perfectly. The problem I was having was that on swipe beyond the current page it would crash. Crash no More ! Excellent model,. Thank you . Well done. – user462990 Jan 04 '19 at 16:03
  • Couldn't find anything helpful for days until I saw this one, Thank you! – Itay Braha May 04 '19 at 13:09
15

Simpler than ever, forget about ViewTypes. It is not recommended to use multiple viewtypes inside one adapter. It will mess the code and break the single responsibility principle since now the adapter needs to handle logic to know which view to inflate.

Now imagine working in large teams where each team has to work in one of those viewtypes features. It will be a mess to touch the same adapter by all the teams that work in the different viewtypes. This is solved using ConcatAdapter where you isolate the adapters. Code them one by one and then just merge them inside one view.

From recyclerview:1.2.0-alpha04 you now can use ConcatAdapter.

If you need a view with different viewTypes, you can just write the Adapters for each section and just use ConcatAdapter to merge all of them inside one recyclerview.

ConcatAdapter

This image shows three different viewtypes that one recyclerview has, header, content and footer.

Enter image description here

You only create one adapter for each section, and then just use ConcatAdapter to merge them inside one recyclerview:

val firstAdapter: FirstAdapter = …
val secondAdapter: SecondAdapter = …
val thirdAdapter: ThirdAdapter = …
val concatAdapter = ConcatAdapter(firstAdapter, secondAdapter,
                                  thirdAdapter)
recyclerView.adapter = concatAdapter

Enter image description here

That's all you need to know. If you want to handle loading state, for example remove the last adapter after some loading happened, you can use LoadState.

For more in depth information follow Florina Muntenescu post here https://medium.com/androiddevelopers/merge-adapters-sequentially-with-mergeadapter-294d2942127a

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • Thank you so much.This answer is very helpful. It is necessary to add `implementation "androidx.recyclerview:recyclerview:1.2.0-alpha04" ` in the gradle. – jujuf1 Jan 02 '21 at 19:40
  • 1.2.1 stable version is available: `implementation "androidx.recyclerview:recyclerview:1.2.1"`. It worked for me perfectly. – Atakan Yildirim Jun 18 '21 at 14:21
  • 2
    Great solution, but how to solve problems when types are mixed? For example, list looks like this: SecondAdapter, FirstAdapter, FirstAdapter, ThirdAdapter, FirstAdapter,SecondAdapter,ThirdAdapter,FirsrtAdapter, SecondAdapter,ThirdAdapter... ? – Dragan Stojanov Oct 04 '21 at 11:49
  • 1
    there is no a problem, you can repeat the adapters, because is a list of adapters and then they are displayed, you can try adding the adapters and then set the recyclerview with the concatadapter – Gastón Saillén Oct 05 '21 at 14:52
9

Although the selected answer is correct, I just want to further elaborate it. I found a useful Custom Adapter for multiple View Types in RecyclerView. Its Kotlin version is here.

The custom adapter is the following:

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;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
8

Following Anton's solution, I came up with this ViewHolder which holds/handles/delegates different type of layouts.

But I am not sure if the replacing new layout would work when the recycling view's ViewHolder is not the type of the data roll in.

So basically, onCreateViewHolder(ViewGroup parent, int viewType) is only called when new view layout is needed;

getItemViewType(int position) will be called for the viewType;

onBindViewHolder(ViewHolder holder, int position) is always called when recycling the view (new data is brought in and try to display with that ViewHolder).

So when onBindViewHolder is called it needs to be put in the right view layout and update the ViewHolder.

public int getItemViewType(int position) {
    TypedData data = mDataSource.get(position);
    return data.type;
}

public ViewHolder onCreateViewHolder(ViewGroup parent,
    int viewType) {
    return ViewHolder.makeViewHolder(parent, viewType);
}

public void onBindViewHolder(ViewHolder holder,
    int position) {
    TypedData data = mDataSource.get(position);
    holder.updateData(data);
}

///
public static class ViewHolder extends
    RecyclerView.ViewHolder {

    ViewGroup mParentViewGroup;
    View mCurrentViewThisViewHolderIsFor;
    int mDataType;

    public TypeOneViewHolder mTypeOneViewHolder;
    public TypeTwoViewHolder mTypeTwoViewHolder;

    static ViewHolder makeViewHolder(ViewGroup vwGrp,
        int dataType) {
        View v = getLayoutView(vwGrp, dataType);
        return new ViewHolder(vwGrp, v, viewType);
    }

    static View getLayoutView(ViewGroup vwGrp,
        int dataType) {
        int layoutId = getLayoutId(dataType);
        return LayoutInflater.from(vwGrp.getContext())
                             .inflate(layoutId, null);
    }

    static int getLayoutId(int dataType) {
        if (dataType == TYPE_ONE) {
            return R.layout.type_one_layout;
        } else if (dataType == TYPE_TWO) {
            return R.layout.type_two_layout;
        }
    }

    public ViewHolder(ViewGroup vwGrp, View v,
        int dataType) {
        super(v);
        mDataType = dataType;
        mParentViewGroup = vwGrp;
        mCurrentViewThisViewHolderIsFor = v;

        if (data.type == TYPE_ONE) {
            mTypeOneViewHolder = new TypeOneViewHolder(v);
        } else if (data.type == TYPE_TWO) {
            mTypeTwoViewHolder = new TypeTwoViewHolder(v);
        }
    }

    public void updateData(TypeData data) {
        mDataType = data.type;
        if (data.type == TYPE_ONE) {
            mTypeTwoViewHolder = null;
            if (mTypeOneViewHolder == null) {
                View newView = getLayoutView(mParentViewGroup,
                               data.type);

                /**
                 *  How can I replace a new view with
                    the view in the parent
                    view container?
                 */
                replaceView(mCurrentViewThisViewHolderIsFor,
                            newView);
                mCurrentViewThisViewHolderIsFor = newView;

                mTypeOneViewHolder =
                    new TypeOneViewHolder(newView);
            }
            mTypeOneViewHolder.updateDataTypeOne(data);

        } else if (data.type == TYPE_TWO){
            mTypeOneViewHolder = null;
            if (mTypeTwoViewHolder == null) {
                View newView = getLayoutView(mParentViewGroup,
                               data.type);

                /**
                 *  How can I replace a new view with
                    the view in the parent view
                    container?
                 */
                replaceView(mCurrentViewThisViewHolderIsFor,
                            newView);
                mCurrentViewThisViewHolderIsFor = newView;

                mTypeTwoViewHolder =
                    new TypeTwoViewHolder(newView);
            }
            mTypeTwoViewHolder.updateDataTypeOne(data);
        }
    }
}

public static void replaceView(View currentView,
    View newView) {
    ViewGroup parent = (ViewGroup)currentView.getParent();
    if(parent == null) {
        return;
    }
    final int index = parent.indexOfChild(currentView);
    parent.removeView(currentView);
    parent.addView(newView, index);
}

ViewHolder has member mItemViewType to hold the view.

It looks like in onBindViewHolder(ViewHolder holder, int position) the ViewHolder passed in has been picked up (or created) by looked at getItemViewType(int position) to make sure it is a match, so it may not need to worry there that ViewHolder's type does not match the data[position]'s type.

It looks like The recycle ViewHolder is picked by type, so no warrior there.

Building a RecyclerView LayoutManager – Part 1 answers this question.

It gets the recycle ViewHolder like:

holder = getRecycledViewPool().getRecycledView(mAdapter.getItemViewType(offsetPosition));

Or create a new one if not find recycle ViewHolder of the right type.

public ViewHolder getRecycledView(int viewType) {
        final ArrayList<ViewHolder> scrapHeap = mScrap.get(viewType);
        if (scrapHeap != null && !scrapHeap.isEmpty()) {
            final int index = scrapHeap.size() - 1;
            final ViewHolder scrap = scrapHeap.get(index);
            scrapHeap.remove(index);
            return scrap;
        }
        return null;
    }

View getViewForPosition(int position, boolean dryRun) {
    ......

    if (holder == null) {
            final int offsetPosition = mAdapterHelper.findPositionOffset(position);
            if (offsetPosition < 0 || offsetPosition >= mAdapter.getItemCount()) {
                throw new IndexOutOfBoundsException("Inconsistency detected. Invalid item "
                        + "position " + position + "(offset:" + offsetPosition + ")."
                        + "state:" + mState.getItemCount());
            }

            final int type = mAdapter.getItemViewType(offsetPosition);
            // 2) Find from scrap via stable ids, if exists
            if (mAdapter.hasStableIds()) {
                holder = getScrapViewForId(mAdapter.getItemId(offsetPosition), type, dryRun);
                if (holder != null) {
                    // update position
                    holder.mPosition = offsetPosition;
                    fromScrap = true;
                }
            }
            if (holder == null && mViewCacheExtension != null) {
                // We are NOT sending the offsetPosition because LayoutManager does not
                // know it.
                final View view = mViewCacheExtension
                        .getViewForPositionAndType(this, position, type);
                if (view != null) {
                    holder = getChildViewHolder(view);
                    if (holder == null) {
                        throw new IllegalArgumentException("getViewForPositionAndType returned"
                                + " a view which does not have a ViewHolder");
                    } else if (holder.shouldIgnore()) {
                        throw new IllegalArgumentException("getViewForPositionAndType returned"
                                + " a view that is ignored. You must call stopIgnoring before"
                                + " returning this view.");
                    }
                }
            }
            if (holder == null) { // fallback to recycler
                // try recycler.
                // Head to the shared pool.
                if (DEBUG) {
                    Log.d(TAG, "getViewForPosition(" + position + ") fetching from shared "
                            + "pool");
                }
                holder = getRecycledViewPool()
                        .getRecycledView(mAdapter.getItemViewType(offsetPosition));
                if (holder != null) {
                    holder.resetInternal();
                    if (FORCE_INVALIDATE_DISPLAY_LIST) {
                        invalidateDisplayListInt(holder);
                    }
                }
            }
            if (holder == null) {
                holder = mAdapter.createViewHolder(RecyclerView.this,
                        mAdapter.getItemViewType(offsetPosition));
                if (DEBUG) {
                    Log.d(TAG, "getViewForPosition created new ViewHolder");
                }
            }
        }
        boolean bound = false;
        if (mState.isPreLayout() && holder.isBound()) {
            // do not update unless we absolutely have to.
            holder.mPreLayoutPosition = position;
        } else if (!holder.isBound() || holder.needsUpdate() || holder.isInvalid()) {
            if (DEBUG && holder.isRemoved()) {
                throw new IllegalStateException("Removed holder should be bound and it should"
                        + " come here only in pre-layout. Holder: " + holder);
            }
            final int offsetPosition = mAdapterHelper.findPositionOffset(position);
            mAdapter.bindViewHolder(holder, offsetPosition);
            attachAccessibilityDelegate(holder.itemView);
            bound = true;
            if (mState.isPreLayout()) {
                holder.mPreLayoutPosition = position;
            }
        }

        final ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        final LayoutParams rvLayoutParams;
        if (lp == null) {
            rvLayoutParams = (LayoutParams) generateDefaultLayoutParams();
            holder.itemView.setLayoutParams(rvLayoutParams);
        } else if (!checkLayoutParams(lp)) {
            rvLayoutParams = (LayoutParams) generateLayoutParams(lp);
            holder.itemView.setLayoutParams(rvLayoutParams);
        } else {
            rvLayoutParams = (LayoutParams) lp;
        }
        rvLayoutParams.mViewHolder = holder;
        rvLayoutParams.mPendingInvalidate = fromScrap && bound;
        return holder.itemView;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lannyf
  • 9,865
  • 12
  • 70
  • 152
7

I have a better solution which allows to create multiple view types in a declarative and type safe way. It’s written in Kotlin which, by the way, is really nice.

Simple view holders for all required view types

class ViewHolderMedium(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val icon: ImageView = itemView.findViewById(R.id.icon) as ImageView
    val label: TextView = itemView.findViewById(R.id.label) as TextView
}

There is an abstraction of adapter data item. Note that a view type is represented by a hashCode of particular view holder class (KClass in Kotlin)

trait AdapterItem {
   val viewType: Int
   fun bindViewHolder(viewHolder: RecyclerView.ViewHolder)
}

abstract class AdapterItemBase<T>(val viewHolderClass: KClass<T>) : AdapterItem {
   override val viewType: Int = viewHolderClass.hashCode()
   abstract fun bindViewHolder(viewHolder: T)
   override fun bindViewHolder(viewHolder: RecyclerView.ViewHolder) {
       bindViewHolder(viewHolder as T)
   }
}

Only bindViewHolder needs to be overridden in concrete adapter item classes (type safe way).

class AdapterItemMedium(val icon: Drawable, val label: String, val onClick: () -> Unit) : AdapterItemBase<ViewHolderMedium>(ViewHolderMedium::class) {
    override fun bindViewHolder(viewHolder: ViewHolderMedium) {
        viewHolder.icon.setImageDrawable(icon)
        viewHolder.label.setText(label)
        viewHolder.itemView.setOnClickListener { onClick() }
    }
}

List of such AdapterItemMedium objects is a data source for the adapter which actually accepts List<AdapterItem>. See below.

The important part of this solution is a view holder factory which will provide fresh instances of a specific ViewHolder:

class ViewHolderProvider {
    private val viewHolderFactories = hashMapOf<Int, Pair<Int, Any>>()

    fun provideViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val (layoutId: Int, f: Any) = viewHolderFactories.get(viewType)
        val viewHolderFactory = f as (View) -> RecyclerView.ViewHolder
        val view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutId, viewGroup, false)
        return viewHolderFactory(view)
    }

    fun registerViewHolderFactory<T>(key: KClass<T>, layoutId: Int, viewHolderFactory: (View) -> T) {
        viewHolderFactories.put(key.hashCode(), Pair(layoutId, viewHolderFactory))
    }
}

And the simple adapter class looks like this:

public class MultitypeAdapter(val items: List<AdapterItem>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

   val viewHolderProvider = ViewHolderProvider() // inject ex Dagger2

   init {
        viewHolderProvider!!.registerViewHolderFactory(ViewHolderMedium::class, R.layout.item_medium, { itemView ->
            ViewHolderMedium(itemView)
        })
   }

   override fun getItemViewType(position: Int): Int {
        return items[position].viewType
    }

    override fun getItemCount(): Int {
        return items.size()
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder? {
        return viewHolderProvider!!.provideViewHolder(viewGroup, viewType)
    }

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
        items[position].bindViewHolder(viewHolder)
    }
}

There are only three steps to create a new view type:

  1. create a view holder class
  2. create an adapter item class (extending from AdapterItemBase)
  3. register the view holder class in ViewHolderProvider

Here is an example of this concept: android-drawer-template.

It goes even further - a view type which acts as a spinner component, with selectable adapter items.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michal Faber
  • 131
  • 1
  • 1
7

It is very simple and straightforward.

Just override the getItemViewType() method in your adapter. On the basis of data return different itemViewType values. E.g., consider an object of type Person with a member isMale, if isMale is true, return 1 and isMale is false, return 2 in the getItemViewType() method.

Now coming to the createViewHolder (ViewGroup parent, int viewType), on the basis of different viewType yon can inflate the different layout file. Like the following:

 if (viewType == 1){
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.male, parent, false);
    return new AdapterMaleViewHolder(view);
}
else{
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.female, parent, false);
    return new AdapterFemaleViewHolder(view);
}

in onBindViewHolder (VH holder,int position) check where holder is an instance of AdapterFemaleViewHolder or AdapterMaleViewHolder by instanceof and accordingly assign the values.

ViewHolder may be like this

    class AdapterMaleViewHolder extends RecyclerView.ViewHolder {
            ...
            public AdapterMaleViewHolder(View itemView){
            ...
            }
        }

    class AdapterFemaleViewHolder extends RecyclerView.ViewHolder {
         ...
         public AdapterFemaleViewHolder(View itemView){
            ...
         }
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tulsi
  • 719
  • 7
  • 15
5

I recommend this library from Hannes Dorfmann. It encapsulates all the logic related to a particular view type in a separate object called "AdapterDelegate".

https://github.com/sockeqwe/AdapterDelegates

public class CatAdapterDelegate extends AdapterDelegate<List<Animal>> {

  private LayoutInflater inflater;

  public CatAdapterDelegate(Activity activity) {
    inflater = activity.getLayoutInflater();
  }

  @Override public boolean isForViewType(@NonNull List<Animal> items, int position) {
    return items.get(position) instanceof Cat;
  }

  @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
    return new CatViewHolder(inflater.inflate(R.layout.item_cat, parent, false));
  }

  @Override public void onBindViewHolder(@NonNull List<Animal> items, int position,
      @NonNull RecyclerView.ViewHolder holder, @Nullable List<Object> payloads) {

    CatViewHolder vh = (CatViewHolder) holder;
    Cat cat = (Cat) items.get(position);

    vh.name.setText(cat.getName());
  }

  static class CatViewHolder extends RecyclerView.ViewHolder {

    public TextView name;

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

public class AnimalAdapter extends ListDelegationAdapter<List<Animal>> {

  public AnimalAdapter(Activity activity, List<Animal> items) {

    // DelegatesManager is a protected Field in ListDelegationAdapter
    delegatesManager.addDelegate(new CatAdapterDelegate(activity))
                    .addDelegate(new DogAdapterDelegate(activity))
                    .addDelegate(new GeckoAdapterDelegate(activity))
                    .addDelegate(23, new SnakeAdapterDelegate(activity));

    // Set the items from super class.
    setItems(items);
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dmitrii Bychkov
  • 829
  • 8
  • 3
4

I firstly recommend you to read Hannes Dorfmann's great article about this topic.

When a new view type comes, you have to edit your adapter and you have to handle so many messy things. Your adapter should be open for extension, but closed for modification.

You may check this two project, they can give the idea about how to handle different ViewTypes in Adapter:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ibrahimyilmaz
  • 18,331
  • 13
  • 61
  • 80
3

If anyone is interested to see the super simple solution written in Kotlin, check the blogpost I just created. The example in the blogpost is based on creating Sectioned RecyclerView:

https://brona.blog/2020/06/sectioned-recyclerview-in-three-steps/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mariusz Brona
  • 1,549
  • 10
  • 12
2

You can use the library: https://github.com/vivchar/RendererRecyclerViewAdapter

mRecyclerViewAdapter = new RendererRecyclerViewAdapter(); /* Included from library */
mRecyclerViewAdapter.registerRenderer(new SomeViewRenderer(SomeModel.TYPE, this));
mRecyclerViewAdapter.registerRenderer(...); /* You can use several types of cells */

For each item, you should to implement a ViewRenderer, ViewHolder, SomeModel:

ViewHolder - it is a simple view holder of recycler view.

SomeModel - it is your model with ItemModel interface

public class SomeViewRenderer extends ViewRenderer<SomeModel, SomeViewHolder> {

    public SomeViewRenderer(final int type, final Context context) {
        super(type, context);
    }

    @Override
    public void bindView(@NonNull final SomeModel model, @NonNull final SomeViewHolder holder) {
        holder.mTitle.setText(model.getTitle());
    }

    @NonNull
    @Override
    public SomeViewHolder createViewHolder(@Nullable final ViewGroup parent) {
        return new SomeViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.some_item, parent, false));
    }
}

For more details you can look at the documentation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vitaly
  • 549
  • 1
  • 6
  • 14
2

View types implementation becomes easier with Kotlin. Here is a sample with this light library https://github.com/Link184/KidAdapter

recyclerView.setUp {
    withViewType {
        withLayoutResId(R.layout.item_int)
        withItems(mutableListOf(1, 2, 3, 4, 5, 6))
        bind<Int> { // this - is adapter view hoder itemView, it - current item
            intName.text = it.toString()
        }
    }


    withViewType("SECOND_STRING_TAG") {
        withLayoutResId(R.layout.item_text)
        withItems(mutableListOf("eight", "nine", "ten", "eleven", "twelve"))
        bind<String> {
            stringName.text = it
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Link182
  • 733
  • 6
  • 15
2

You can deal with multipleViewTypes RecyclerAdapter by making getItemViewType() return the expected viewType value for that position.

I prepared an MultipleViewTypeAdapter for constructing an MCQ list for examinations which may throw a question that may have two or more valid answers (checkbox options) and a single answer questions (radiobutton options).

For this I get the type of question from the API response and I used that for deciding which view I have to show for that question.

public class MultiViewTypeAdapter extends RecyclerView.Adapter {

    Context mContext;
    ArrayList<Question> dataSet;
    ArrayList<String> questions;
    private Object radiobuttontype1;


    //Viewholder to display Questions with checkboxes
    public static class Checkboxtype2 extends RecyclerView.ViewHolder {
        ImageView imgclockcheck;
        CheckBox checkbox;

        public Checkboxtype2(@NonNull View itemView) {
            super(itemView);
            imgclockcheck = (ImageView) itemView.findViewById(R.id.clockout_cbox_image);
            checkbox = (CheckBox) itemView.findViewById(R.id.clockout_cbox);
        }
    }

    //Viewholder to display Questions with radiobuttons

    public static class Radiobuttontype1 extends RecyclerView.ViewHolder {
        ImageView clockout_imageradiobutton;
        RadioButton clockout_radiobutton;
        TextView sample;

        public radiobuttontype1(View itemView) {
            super(itemView);
            clockout_imageradiobutton = (ImageView) itemView.findViewById(R.id.clockout_imageradiobutton);
            clockout_radiobutton = (RadioButton) itemView.findViewById(R.id.clockout_radiobutton);
            sample = (TextView) itemView.findViewById(R.id.sample);
        }
    }

    public MultiViewTypeAdapter(ArrayList<QueDatum> data, Context context) {
        this.dataSet = data;
        this.mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType.equalsIgnoreCase("1")) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.clockout_radio_list_row, viewGroup, false);
            return new radiobuttontype1(view);

        } else if (viewType.equalsIgnoreCase("2")) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.clockout_cbox_list_row, viewGroup, false);
            view.setHorizontalFadingEdgeEnabled(true);
            return new Checkboxtype2(view);

        } else if (viewType.equalsIgnoreCase("3")) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.clockout_radio_list_row, viewGroup, false);
            return new Radiobuttontype1(view);

        } else if (viewType.equalsIgnoreCase("4")) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.clockout_radio_list_row, viewGroup, false);
            return new Radiobuttontype1(view);

        } else if (viewType.equalsIgnoreCase("5")) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.clockout_radio_list_row, viewGroup, false);
            return new Radiobuttontype1(view);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int viewType) {
        if (viewType.equalsIgnoreCase("1")) {
            options =  dataSet.get(i).getOptions();
            question = dataSet.get(i).getQuestion();
            image = options.get(i).getValue();
            ((radiobuttontype1) viewHolder).clockout_radiobutton.setChecked(false);
            ((radiobuttontype1) viewHolder).sample.setText(question);
            //Loading image bitmap in the ViewHolder's View
            Picasso.with(mContext)
                    .load(image)
                    .into(((radiobuttontype1) viewHolder).clockout_imageradiobutton);

        } else if (viewType.equalsIgnoreCase("2")) {
            options = (ArrayList<Clockout_questions_Option>) dataSet.get(i).getOptions();
            question = dataSet.get(i).getQuestion();
            image = options.get(i).getValue();
            //Loading image bitmap in the ViewHolder's View
            Picasso.with(mContext)
                    .load(image)
                    .into(((Checkboxtype2) viewHolder).imgclockcheck);

        } else if (viewType.equalsIgnoreCase("3")) {
            //Fit data to viewHolder for ViewType 3
        } else if (viewType.equalsIgnoreCase("4")) {
            //Fit data to viewHolder for ViewType 4
        } else if (viewType.equalsIgnoreCase("5")) {
            //Fit data to viewHolder for ViewType 5
        }
    }

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

    /**
     * Returns viewType for that position by picking the viewType value from the
     *     dataset
     */
    @Override
    public int getItemViewType(int position) {
        return dataSet.get(position).getViewType();
    }
}

You can avoid multiple conditionals based viewHolder data fillings in onBindViewHolder() by assigning same ids for the similar views across viewHolders which differ in their positioning.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Avinash Ch
  • 76
  • 5
1

Actually, I'd like to improve on Anton's answer.

Since getItemViewType(int position) returns an integer value, you can return the layout resource ID you'd need to inflate. That way you'd save some logic in onCreateViewHolder(ViewGroup parent, int viewType) method.

Also, I wouldn't suggest doing intensive calculations in getItemCount() as that particular function is called at least 5 times while rendering the list, as well as while rendering each item beyond the visible items. Sadly since notifyDatasetChanged() method is final, you can't really override it, but you can call it from another function within the adapter.

Community
  • 1
  • 1
Dragas
  • 1,140
  • 13
  • 29
  • 3
    Yes, that might work, but will make confusing for other developers. Also from Documentation `Note: Integers must be in the range 0 to getViewTypeCount() - 1. IGNORE_ITEM_VIEW_TYPE can also be returned.` So it's just better to write bit more code and don't use hacks. – Ioane Sharvadze Oct 09 '17 at 13:05
  • 1
    I agree. Back then i had missed that particular clause. – Dragas Oct 10 '17 at 13:42
  • 1
    Thats funny because the RecyclerView.Adapter:getItemViewType() docs here https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemViewType(int) suggest what Dragas had posted that you should "Consider using id resources to uniquely identify item view types." apparently oblivious to the requirement for getViewTypeCount() – Deemoe Jan 11 '18 at 18:14
1

If you want to use it in conjunction with Android Data Binding look into the https://github.com/evant/binding-collection-adapter - it is by far the best solution for the multiple view types RecyclerView I have even seen.

You may use it like

var items: AsyncDiffPagedObservableList<BaseListItem> =
        AsyncDiffPagedObservableList(GenericDiff)

    val onItemBind: OnItemBind<BaseListItem> =
        OnItemBind { itemBinding, _, item -> itemBinding.set(BR.item, item.layoutRes) }

And then in the layout where the list is:

 <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                app:enableAnimations="@{false}"
                app:scrollToPosition="@{viewModel.scrollPosition}"

                app:itemBinding="@{viewModel.onItemBind}"
                app:items="@{viewModel.items}"

                app:reverseLayoutManager="@{true}"/>

Your list items must implement the BaseListItem interface which looks like this:

interface BaseListItem {
    val layoutRes: Int
}

And the item view should look something like this:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
                name="item"
                type="...presentation.somescreen.list.YourListItem"/>
    </data>

   ...

</layout>

Where YourListItem implements BaseListItem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pavlo Ostasha
  • 14,527
  • 11
  • 35
1

First you must create two layout XML files. After that inside recyclerview adapter TYPE_CALL and TYPE_EMAIL are two static values with 1 and 2 respectively in the adapter class.

Now define two static values ​​at the Recycler view Adapter class level, for example: private static int TYPE_CALL = 1; private static int TYPE_EMAIL = 2;

Now create the view holder with multiple views like this:

class CallViewHolder extends RecyclerView.ViewHolder {

    private TextView txtName;
    private TextView txtAddress;

    CallViewHolder(@NonNull View itemView) {
        super(itemView);
        txtName = itemView.findViewById(R.id.txtName);
        txtAddress = itemView.findViewById(R.id.txtAddress);
    }
}

class EmailViewHolder extends RecyclerView.ViewHolder {

    private TextView txtName;
    private TextView txtAddress;

    EmailViewHolder(@NonNull View itemView) {
        super(itemView);
        txtName = itemView.findViewById(R.id.txtName);
        txtAddress = itemView.findViewById(R.id.txtAddress);
    }
}

Now code as below in onCreateViewHolder and onBindViewHolder method in the recyclerview adapter:

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
    View view;
    if (viewType == TYPE_CALL) { // for call layout
        view = LayoutInflater.from(context).inflate(R.layout.item_call, viewGroup, false);
        return new CallViewHolder(view);

    } else { // for email layout
        view = LayoutInflater.from(context).inflate(R.layout.item_email, viewGroup, false);
        return new EmailViewHolder(view);
    }
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
    if (getItemViewType(position) == TYPE_CALL) {
        ((CallViewHolder) viewHolder).setCallDetails(employees.get(position));
    } else {
        ((EmailViewHolder) viewHolder).setEmailDetails(employees.get(position));
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Genius8020
  • 135
  • 1
  • 4
1

I did something like this. I passed "fragmentType" and created two ViewHolders and on basis of this, I classified my Layouts accordingly in a single adapter that can have different Layouts and LayoutManagers

private Context mContext;
protected IOnLoyaltyCardCategoriesItemClicked mListener;
private String fragmentType;
private View view;

public LoyaltyCardsCategoriesRecyclerViewAdapter(Context context, IOnLoyaltyCardCategoriesItemClicked itemListener, String fragmentType) {
    this.mContext = context;
    this.mListener = itemListener;
    this.fragmentType = fragmentType;
}

public class LoyaltyCardCategoriesFragmentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private ImageView lc_categories_iv;
    private TextView lc_categories_name_tv;
    private int pos;

    public LoyaltyCardCategoriesFragmentViewHolder(View v) {
        super(v);
        view.setOnClickListener(this);
        lc_categories_iv = (ImageView) v.findViewById(R.id.lc_categories_iv);
        lc_categories_name_tv = (TextView) v.findViewById(R.id.lc_categories_name_tv);
    }

    public void setData(int pos) {
        this.pos = pos;
        lc_categories_iv.setImageResource(R.mipmap.ic_launcher);
        lc_categories_name_tv.setText("Loyalty Card Categories");
    }

    @Override
    public void onClick(View view) {
        if (mListener != null) {
            mListener.onLoyaltyCardCategoriesItemClicked(pos);
        }
    }
}

public class MyLoyaltyCardsFragmentTagViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public ImageButton lc_categories_btn;
    private int pos;

    public MyLoyaltyCardsFragmentTagViewHolder(View v) {
        super(v);
        lc_categories_btn = (ImageButton) v.findViewById(R.id.lc_categories_btn);
        lc_categories_btn.setOnClickListener(this);
    }

    public void setData(int pos) {
        this.pos = pos;
        lc_categories_btn.setImageResource(R.mipmap.ic_launcher);
    }

    @Override
    public void onClick(View view) {
        if (mListener != null) {
            mListener.onLoyaltyCardCategoriesItemClicked(pos);
        }
    }
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (fragmentType.equalsIgnoreCase(Constants.LoyaltyCardCategoriesFragmentTag)) {
        view = LayoutInflater.from(mContext).inflate(R.layout.loyalty_cards_categories_frag_item, parent, false);
        return new LoyaltyCardCategoriesFragmentViewHolder(view);
    } else if (fragmentType.equalsIgnoreCase(Constants.MyLoyaltyCardsFragmentTag)) {
        view = LayoutInflater.from(mContext).inflate(R.layout.my_loyalty_cards_categories_frag_item, parent, false);
        return new MyLoyaltyCardsFragmentTagViewHolder(view);
    } else {
        return null;
    }
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    if (fragmentType.equalsIgnoreCase(Constants.LoyaltyCardCategoriesFragmentTag)) {
        ((LoyaltyCardCategoriesFragmentViewHolder) holder).setData(position);
    } else if (fragmentType.equalsIgnoreCase(Constants.MyLoyaltyCardsFragmentTag)) {
        ((MyLoyaltyCardsFragmentTagViewHolder) holder).setData(position);
    }
}

@Override
public int getItemCount() {
    return 7;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Syed Arsalan Kazmi
  • 949
  • 1
  • 11
  • 17
1

I see there are a lot of great answers, incredibly detailed and extensive. In my case, I always understand things better if I follow along the reasoning from almost scratch, step by step. I would recommend you check this link out and whenever you have similar questions, search for any codelabs that address the issue.

Android Kotlin Fundamentals: Headers in RecyclerView

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Miguel Lasa
  • 470
  • 1
  • 7
  • 19