6

My question is, can We call RecyclerView's onScrolled() Method from onScrollStateChanged() method. The reason is I want to check two conditions. If SCROLL_STATE_SETTLING is true only then check if the user has scrolled down. Is there any other way to check Both of them?

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

            if (newState == RecyclerView.SCROLL_STATE_SETTLING) {

              // Now I have to check if the user has scrolled down or up.  

            }
        }

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


        }
    });
Prabh deep
  • 1,024
  • 11
  • 15
Abhi
  • 2,115
  • 2
  • 18
  • 29
  • what ever you are trying to achieve can be done in onscrillstatechanged itself. provide some more details so that i can tell how to do that – Mohammed Atif Nov 12 '16 at 09:34
  • Done. When newState == RecyclerView.SCROLL_STATE_SETTLING, I want to check if user has scrolled down or not. – Abhi Nov 12 '16 at 09:39

3 Answers3

0

Detect start scroll and end scroll in recyclerview

GiridharaSPK
  • 496
  • 7
  • 17
0

Is this what you want?

From onScrolled method you can set a boolean flag 'isScrolledDown' by checking if dy is less than 0 to know that user scrolled down. Then whenever the scroll state is SCROLL_STATE_SETTLING and isScrolledDown is true then do the operation you want.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        private boolean isScrolledDown = false;

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

            if (newState == RecyclerView.SCROLL_STATE_SETTLING && isScrolledDown) {

              // Do something...

            }
        }

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

            isScrolledDown = dy < 0;
        }
    });
Ahmed Shendy
  • 1,424
  • 15
  • 28
0

you may start by declaring these two variable outside your listener

//chose appropriate initial values
  var updatedRange = Range(...) 
  var lastVisibilityRange = Range(...)

then in the listener callback you'll have something like this:

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)

                    val first = manager.findFirstVisibleItemPosition()
                    val last = manager.findLastVisibleItemPosition()
                    lastVisibilityRange = updatedRange
                    updatedRange = Range(first , last)

                    //or you can even call this section in onScrollStateChanged 
                    if (updatedRange.lower <= lastVisibilityRange.lower) {
                        //has scrolled up
                    } else {
                        //has scrolled down
                    }

                }

the code hasn't been tested, you may make it more reactive by throwing a LiveData in there, feel free to let me know if this is not what you're looking for, hope this will help or point you in the right direction, have a good day y'all.

Aleyam
  • 1,215
  • 1
  • 14
  • 14