简体   繁体   English

如何检测用户何时滚动到RecyclerView中的最顶层项目

[英]How to detect when user have scrolled to the top most item in a RecyclerView

I have tried writing the following code 我尝试编写以下代码

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  super.onScrolled(recyclerView, dx, dy);
  if(dy > 0){
    //scrolling up
  } else {
    //scrolling down
    int pastVisibleItems = linearLayoutManager.findFirstVisibleItemPosition();
    if (pastVisibleItems  == 0) {
      Toast.makeText(getContext(),"Top most item",Toast.LENGTH_SHORT).show();
    }
  }
}

but it doesn't work. 但它不起作用。 I have also tried using the android's SwipeRefreshLayout to do this since it does what I want which is able to detect when user has scroll to the topmost item. 我也尝试使用android的SwipeRefreshLayout来做这个,因为它做了我想要的,它能够检测用户何时滚动到最顶层的项目。 I decided to not use it because I can't seem to prevent the loading indicator from popping out (I don't want to see the loading indicator come out at all). 我决定不使用它,因为我似乎无法阻止加载指示器弹出(我不想看到加载指示器出来)。

So, basically what I want to do is: 所以,基本上我想做的是:

  1. check user scrolling 检查用户滚动
  2. is it the topmost item already? 它已经是最顶级的项目了吗?
  3. if yes, execute something, if no then nothing 如果是的话,执行一些事情,如果没有则执行任务

Any idea how to accomplish this? 知道怎么做到这一点?

Thanks. 谢谢。

Replace your code with below code: 用以下代码替换您的代码:

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

  int pastVisibleItems = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
  if (pastVisibleItems  == 0) {
    Toast.makeText(getContext(),"Top most item",Toast.LENGTH_SHORT).show();                
  }
}

it works for me, if you get any error then comment. 它对我有用,如果你有任何错误然后评论。

I have solved my question by myself. 我自己解决了我的问题。 I used the code below instead 我使用下面的代码

private boolean isUserScrolling = false;
private boolean isListGoingUp = true;

Then in the RecyclerView OnScrollListener 然后在RecyclerView OnScrollListener中

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    //detect is the topmost item visible and is user scrolling? if true then only execute
    if(newState ==  RecyclerView.SCROLL_STATE_DRAGGING){
        isUserScrolling = true;
        if(isListGoingUp){
            //my recycler view is actually inverted so I have to write this condition instead
            if(linearLayoutManager.findLastCompletelyVisibleItemPosition() + 1 == list.size()){
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(isListGoingUp) {
                            if (linearLayoutManager.findLastCompletelyVisibleItemPosition() + 1 == list.size()) {
                                Toast.makeText(getContext(),"exeute something", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                },50);
                //waiting for 50ms because when scrolling down from top, the variable isListGoingUp is still true until the onScrolled method is executed
            }
        }
    }
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    if(isUserScrolling){
        if(dy > 0){
            //means user finger is moving up but the list is going down
            isListGoingUp = false;
        }
        else{
            //means user finger is moving down but the list is going up
            isListGoingUp = true;
        }
    }
}

You Can Do This easily use Code 你能做到这很容易使用Code

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

        if(newState ==  RecyclerView.SCROLL_STATE_DRAGGING) {
            isUserScrolling = true;
        }
    }

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

        super.onScrolled(recyclerView, dx, dy);

        if(isUserScrolling){
            if(dy > 0){
               //Scroll list Down
            }
            else{
                if(!recyclerView.canScrollVertically(-1)){

                  //This Your Top View do Something
                }
            }
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM