Harminder Singh
Harminder Singh

Reputation: 1766

RecyclerView - Any way to get how many items are visible on screen before user starts scrolling?

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

Upvotes: 1

Views: 1264

Answers (1)

Shashank Pednekar
Shashank Pednekar

Reputation: 247

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());
            }
        });

Upvotes: 0

Related Questions