Know when RecyclerView items are displayed for the first time (Android)

I am trying to use Google Analytics Enhanced Ecommerce to send "impressions" of my products to the RecyclerView StaggeredGrid. Every time a user scrolls, I check which products are visible and send a hit:

public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);

    if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
        for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) {
            Offer offer = mListViewContentFetcher.getApiObjects().get(i);

            Product product = new Product()
                .setId(offer.getCode())
                .setName(offer.getTitle())
                .setCategory(offer.getStore().getName())
                .setPosition(i);

            builder.addImpression(product, "products_list");
        }

        mTracker.setScreenName("Products List");
        mTracker.send(builder.build());
    }
}

But I also need to run this when the RecyclerView is built for the first time, and the first products are visible.

How can I find out that the first elements are ready? I tried using ViewTreeObserver for recyclerview and onBindViewHolder without success.

Edit: This is inside the fragment that is used in the view, so I need to know when the elements are really visible and not only added.

thank

+4
source
1
+3

All Articles