0

How would you detect when the RecyclerView is near the bottom? Right now it only detects when it's at the absolute bottom and can't scroll anymore:

mGridRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);


                if(!mGridRecycler.canScrollVertically(1) && newState==RecyclerView.SCROLL_STATE_IDLE){

                    //Make network call here
                }
            }
        });
DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83

1 Answers1

0

In your OnScrollListener calculate the difference between the bottom of the last child view in the Recycler view and the bottom of the Recycler view itself, if the last child view is close enough to the bottom of the recycler view you can make your network call.

mGridRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            View view = (View) recyclerView.getChildAt(recyclerView.getChildCount() - 1);
            int difference = (view.getBottom() - (recyclerView.getHeight() + recyclerView.getScrollY()));

            if (difference == 0) {
                //Make network call here
            }
        }
    });

difference == 0 means the absolute bottom when the Recycler view can't scroll anymore, you can change the condition to check when it 5px from the recycler view bottom difference == 5.

Thecarisma
  • 1,424
  • 18
  • 19
  • Solution is wrong, here is what works for me with padding check this out: ```int difference = lastItem.getBottom() + recyclerView.getPaddingBottom() - recyclerView.getHeight()``` – Muhammed Aydogan Aug 24 '21 at 10:55