RecyclerView不调用onScrolled在底部 [英] RecyclerView not calling onScrolled at the bottom

查看:438
本文介绍了RecyclerView不调用onScrolled在底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建带有分页的RecyclerView,并且当我尝试向下滚动列表时已经显示了进度条,这是一个问题.有一个回调RecyclerView.OnScrollListener,该回调具有用于处理滚动事件的方法onScrolled,但是当没有实际滚动发生时,它不起作用.

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.

当我尝试从底部向下滚动时,有一种onScrollStateChanged方法有效,但是我的逻辑要求我知道手势的方向(上/下),因此该方法对我的情况没有帮助.

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();
        }
    });

我试图检测是否在onScrolled中达到了末尾,这有点奏效,但是有时当我尝试从服务器获取数据时我什么也没收到,所以在收到这样的请求之后仍然在列表的末尾,我想向下滚动以尝试再次更新列表,但是由于onScrolled没有被调用,所以我不能.

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?

推荐答案

我想在recyclerview的末尾显示进度,您只需要确保已到达末尾即可.

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

事情就是这样-

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...
                }
            }
        });
    }

有时,当我尝试从服务器获取数据时,我没有收到 任何东西

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

简单!如果您的网络请求失败,请尝试捕获异常.如果是SocketTimeOutException,请再次尝试执行相同的请求.如果还有另一个异常(可能来自后端或前端),请进行修复.

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.

这篇关于RecyclerView不调用onScrolled在底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆