Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 675
the newbie coder Avatar asked Jan 25 '17 09:01

the newbie coder


1 Answers

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

@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;
        }
    }
}
like image 68
the newbie coder Avatar answered Oct 26 '22 17:10

the newbie coder