14

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... :)

Kushal
  • 8,100
  • 9
  • 63
  • 82
Ganesh
  • 1,820
  • 2
  • 20
  • 40
  • `getItemCount` returns the total number of items in your `RecyclerView` and `getViewType` is called with values [0..`cnt`-1] where `cnt` is returned by `getItemCount` – pskink Nov 14 '15 at 11:11
  • @pskink When i use two or more Holders in the same Adapter, Each holder will have its seperate Count and Seperate operations in OnCreate and OnBind, How can i identify and code it? – Ganesh Nov 14 '15 at 11:15
  • 1
    add some `Log.d` inside those methods, it is the best method of learning – pskink Nov 14 '15 at 11:25
  • @pskink Cool.. that's the preety awesome method. :D – Ganesh Nov 14 '15 at 12:32

1 Answers1

8

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); 
         ... 
     } 
} 
Collins Abitekaniza
  • 4,496
  • 2
  • 28
  • 43
  • Thanks for your answer, Could you explain more about getItemViewType() , 1. Who and where this getItemViewType will be called with integer argument . i mean what will be in "position". Is it called each time for each ViewHolders or it will be called for OnBind method? What is the purpose of it? – Ganesh Nov 14 '15 at 11:22
  • This can be a getter method inside your CustomItem class which is the type of data list List list; It has to be set when creating your list element like CustomItem item=new CustomItem();item.setViewType=0;list.add(item); – Collins Abitekaniza Nov 14 '15 at 11:30