Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView not calling onScrolled at the bottom

I'm trying to create a RecyclerView with pagination and I have a problem with showing progress bar when I try to scroll down being already at the very end of the list. There's a callback RecyclerView.OnScrollListener which has a method onScrolled for handling scroll events, but it's not working when no actual scrolling has happened.

There's onScrollStateChanged method that works when I try to scroll down from the bottom, but my logic requires me to know direction of the gesture (up/down), so this method is not helpfull in my situation.

I currently trying to do it somewhat like this (which is obviously not working):

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            fetchDataFromServer();
        }
    });

EDIT: I tried to detect if the end is reached in onScrolled and it kinda worked, but sometimes when I try to fetch the data from the server I don't receive anything, so after such a request I'm still at the end of the list and I want to scroll down to try to update the list again, but I can't since onScrolled is not getting called.

Any ideas on how to fix that? Is there another callback that I can use?

like image 947
ulmaxy Avatar asked Sep 12 '17 17:09

ulmaxy


2 Answers

I guess to show progress at the end of the recyclerview, you just need to make sure that end has been reached.

This is how it goes -

private int visibleThreshold = 1; // trigger just one item before the end
private int lastVisibleItem, totalItemCount;

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

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                totalItemCount = mLayoutManager.getItemCount();
                lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();

                if (totalItemCount <= (lastVisibleItem + visibleThreshold) {
                    // ... end has been reached...
                    //... do your stuff over here...
                }
            }
        });
    }

EDIT:

sometimes when I try to fetch the data from the server I don't receive anything

Simple! If your network request fails, try to catch an exception. If it is SocketTimeOutException, Retry doing the same request again. If there is an another exception(could be from the back-end or front-end), Get it fixed.

like image 149
Paresh P. Avatar answered Nov 03 '22 03:11

Paresh P.


Check if your recycler view is nested inside NestedScrollView or something... Remove the nested scroll view and it will work. You can achieve any kind of feed by just using recycler view and use the VIEW_TYPE concept of recycler view

like image 3
Shubham Sharma Avatar answered Nov 03 '22 03:11

Shubham Sharma