RecyclerView scrolling to bottom of page doesn't trigger .addOnScrollListener's canScrollVertically

david s. :

I'm trying to implement a skipping feature in my vertical recyclerview. When a user clicks a button, the recyclerView will scroll all the way to the bottom and trigger an API call. Right now to check if the user is at the bottom of the screen, I am using .addOnScrollListener on my recyclerview.

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

                //SCROLL_STATE_IDE prevents repeated calls from happening
                // when near bottom of screen and slightly scroll up a bit
                if(!mGridRecycler.canScrollVertically(1) && newState==RecyclerView.SCROLL_STATE_IDLE){
                    Log.d(TAG, "onScrollStateChanged: CALLED WTF");
                    mActivity.getNextPageGridView();
                }
            }
        });

And to implement the skipping feature to the last item, I am using this method on a button:

mGridRecycler.scrollToPosition(mList.size() - 1);

It skips to the last item, however the API call is not triggered because the if-statement is not called. Anyone know what I can make it work successfully?

Tiko :

RecyclerView does not know how LayoutManager will handle the scroll.
You'll only receive a call to onScrolled if first and or last child position changes after a layout, you're not gonna get onScrollStateChanged callback.

What should you do?

Alternatively you can use smoothScrollToPosition(..) method, OR override onScrolled method, OR make a request to your API right after the skip button click, which is the best solution for your scenario.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=16945&siteId=1