When getItemCount and getItemViewType method get called in Recycler Adapter

Ganesh picture Ganesh · Nov 14, 2015 · Viewed 13.7k times · Source

I saw similar questions in stackoverflow but they doesn't give clear answers to my question. Don't mark it as duplicate before reading my full questions. I saw this link , this , and this too. Thanks for spending your time to read this.

I gave my three questions below the Source code, kindly have a look at it.

I'll make it simple. I am trying to use two ViewHolder in Recycler Adapter which i am going to use in ViewPager for TabLayout. Both View Holder having different Xml and different elements (ie textview , imageview etc..) But got struck with several confusions inside it.

I implemented my RecyclerView adapter class as follows

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


    public class MainViewHolder extends  RecyclerView.ViewHolder {
       public MainViewHolder(View v) {
        super(v);
       }


    class ViewHolder0 extends MainViewHolder { 
        ... 
    } 

    class ViewHolder2 extends MainViewHolder { 
        ... 
    } 

    @Override 
    public int getItemViewType(int position) {
        /**** I don't know where and when this method will be called and what will be the value present in the variable "position" ****/
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case 0: return new ViewHolder0(...); 
             case 2: return new ViewHolder2(...); 
             ... 
         } 
    } 
      public int getItemCount() {
             /**** I don't know where and when this method will be called and   what will be the value present in the variable "position" ****/ 
     }
} 

And my Questions are,

Q1. When and where getViewType is called and what will be in "position" variable and what do we need to return

Q2. When and where getItemCount is called and how can i return correctly (because i am using two view holders and each will have different count )

Q3. I created seperate Recyclerview Adapter class but it gave an error that RecyclerViewAdapter class clashes with the another one. (Since i am using them in same activity for TabLayout, i thought that error was thrown, am I correct? or is there any way to create seperate Adapter class)

If you can explain the full process of RecyclerViewAdapter, that would be awesome :) But please clarify my above confusions.

Any type of help welcomed, Thanks in advance... :)

Answer

Collins Abitekaniza picture Collins Abitekaniza · Nov 14, 2015

Q1) The getViewType() method will be called before the onCreateViewHolder() method each time your custom view is created.

You need to create a list with your custom list items List<CustomItem> list=method_to_return_your_list() and each of them with a getViewType() getter method.

In your get getItemViewType()

public int getItemViewType(int position) {
    return list.get(position).getViewType();
   /*this returns the view type of each element in the list*/
} 

This can be either 0 or 1 considering your switch case statement in the onCreateViewHolder() method

Q2) The getItemCount() method should return the number of list items.

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

Q3) Do not create another recyclerview adapter for the same recyclerview

Also I fogot. Instead of creating new ViewHolders ,just change the itemView in the view holder like

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