簡體   English   中英

如何檢測用戶何時滾動到RecyclerView中的最頂層項目

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

我嘗試編寫以下代碼

@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. 如果是的話,執行一些事情,如果沒有則執行任務

知道怎么做到這一點?

謝謝。

用以下代碼替換您的代碼:

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

它對我有用,如果你有任何錯誤然后評論。

我自己解決了我的問題。 我使用下面的代碼

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

然后在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;
        }
    }
}

你能做到這很容易使用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