How to know when the RecyclerView has finished laying down the items?

I also needed to execute code after my recycler view finished inflating all elements. I tried checking in onBindViewHolder in my Adapter, if the position was the last, and then notified the observer. But at that point, the recycler view still was not fully populated.

As RecyclerView implements ViewGroup, this anwser was very helpful. You simply need to add an OnGlobalLayoutListener to the recyclerView:

View recyclerView = findViewById(R.id.myView);
recyclerView
    .getViewTreeObserver()
    .addOnGlobalLayoutListener(
        new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // At this point the layout is complete and the
                // dimensions of recyclerView and any child views 
                // are known.
                recyclerView
                    .getViewTreeObserver()
                    .removeOnGlobalLayoutListener(this);
            }
        });

Leave a Comment