1

One of my activities contain a RecyclerView. On launching the activity, I want to know how many times onBindViewHolder() would run before user does any scrolling. Using logging, I checked that it runs almost as many times as the number of items visible on the screen. So essentially I want to know how many items are visible on the screen.

getItemCount() gives the total number of items. This is not what I want.

Is there any other way or method to get only the count of visible items?

I checked this post, but it did not help my case - Get visible items in RecyclerView

Harminder Singh
  • 1,577
  • 1
  • 11
  • 19
  • "it did not help my case" -- what do you mean by this? `RecyclerView` does not know how many items are visible until they become visible, as the size of the items is not known ahead of time. – CommonsWare May 06 '18 at 15:47
  • You can use a for loop in your `OnBindViewHolder()` and store the value in prefs... or give a ID for each item... – HB. May 06 '18 at 16:01
  • @CommonsWare I don't need to know ahead of time. I need to know only once they have become visible. Is there any way? – Harminder Singh May 07 '18 at 09:50
  • Then I do not understand why the question that you linked to "did not help in [your] case". – CommonsWare May 07 '18 at 10:22

1 Answers1

0

To get RecyclerView visible count you can use layoutManager.getChildCount(). But you have to use like this :

layoutManager.postOnAnimation(new Runnable() {
            @Override
            public void run() {
                System.out.println("Visible count " + layoutManager.getChildCount());
            }
        });