如何检测用户何时滚动到回收器视图中最顶部的项目



我尝试编写以下代码

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

但它不起作用。我也尝试使用android的SwipeRefreshLayout来做到这一点,因为它可以做我想要的,能够检测用户何时滚动到最上面的项目。我决定不使用它,因为我似乎无法阻止加载指示器弹出(我根本不想看到加载指示器出来)。

所以,基本上我想做的是:

  1. 检查用户滚动
  2. 它已经是最顶层的项目了吗?
  3. 如果是,则执行某事,如果否,则什么都不执行

知道如何做到这一点吗?

谢谢。

我已经自己解决了我的问题。我改用了下面的代码

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

然后在回收器中查看滚动侦听器

@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;
        }
    }
}

将代码替换为以下代码:

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

它对我有用,如果您收到任何错误,请发表评论。

您可以使用代码轻松执行此操作

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

最新更新