Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain what getItemCount method in RecyclerView.LayoutManager return?

The documentation says the following about getItemCount():

Returns the number of items in the adapter bound to the parent RecyclerView. Note that this number is not necessarily equal to State#getItemCount().

So, does it return all the items in the adapter or the items that are visible on the screen? I don't get it. Can someone explain this method?

like image 943
Aravind Pulagam Avatar asked Feb 15 '18 06:02

Aravind Pulagam


People also ask

What is LayoutManager in RecyclerView?

This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.

What is getItemCount?

getItemCount() - returns The number of items currently available in adapter. This method returns the size of the collection that contains the items we want to display.


3 Answers

getItemCount() - returns The number of items currently available in adapter

  • This method returns the size of the collection that contains the items we want to display.

    @Override
    public int getItemCount() {
        return models.size();
    }
    
  • It returns The number of items currently available in adapter.

Reference:

  • getItemCount()

  • More about layout manager and getItemCount()

like image 145
Vidhi Dave Avatar answered Oct 23 '22 00:10

Vidhi Dave


The getItemCount() method returns the number of list items.The number of items this adapter is adapting.

Reference Code :

ArrayList<Games> list;

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

The getItemCount() method is return the number of items in the collection you're adapting, in above case list, which is just an array of Game objects. Arrays have a property that allows you to get their length which is all you need to return.

like image 1
Abhishek kumar Avatar answered Oct 23 '22 00:10

Abhishek kumar


It returns the size of all the items in the adapter not only the size of visible items. In simple term, getItemCount() returns the size for the whole adapter.

like image 1
dazed'n'confused Avatar answered Oct 22 '22 22:10

dazed'n'confused