Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When getItemCount and getItemViewType method get called in Recycler Adapter

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

like image 465
Ganesh Avatar asked Nov 14 '15 10:11

Ganesh


People also ask

Which RecyclerView method is called when getting the size of the data set?

getItemCount() : RecyclerView calls this method to get the size of the data set. For example, in an address book app, this might be the total number of addresses. RecyclerView uses this to determine when there are no more items that can be displayed.

Which method must be overridden to implement a RecyclerView Adaptor?

Adapter. RecyclerView includes a new kind of adapter. It's a similar approach to the ones you already used, but with some peculiarities, such as a required ViewHolder . You will have to override two main methods: one to inflate the view and its view holder, and another one to bind data to the view.

How do I call notifyDataSetChanged in RecyclerView adapter?

In, them adapter method, just add that new data in your main arraylist and do notifyItemInserted() instead of notifyDataSetChanged, in this way you will also see the default adding animation of recyclerView. Save this answer.

How many times onCreateViewHolder is called?

By default it have 5. you can increase as per your need. Save this answer.


1 Answers

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); 
         ... 
     } 
} 
like image 162
Collins Abitekaniza Avatar answered Sep 22 '22 09:09

Collins Abitekaniza