RecyclerView not calling onScrolled at the bottom

北城余情 提交于 2020-01-15 05:38:06

问题


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?


回答1:


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.




回答2:


If you developing for min sdk level 23 then you can use setOnScrollChangeListener

  recyclerView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

        }
    });


来源:https://stackoverflow.com/questions/46182373/recyclerview-not-calling-onscrolled-at-the-bottom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!