0

I am working on android app and using recyclerview and scrollview in some activities. Now I want to show some layout when recyclerview/scrollview scrolls in up direction and hide the layout when it scrolls in down direction. I want a function which can tell that recyclerview/scrollview is scrolling in up or down direction.

Please help if anyone know how to do this for both recyclerview and scrollview.

Thanks a lot in advanced.

Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78

2 Answers2

3

A sure short answer for this issue

scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {


                    int x = scrollY - oldScrollY;
                    if (x > 0) {
                        //scroll up
                    } else if (x < 0) {
                       //scroll down
                    } else {

                    }


        }
    });
2
Try this way:

private static int firstVisibleInListview;

firstVisibleInListview = yourLayoutManager.findFirstVisibleItemPosition();

In your scroll listener:

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

 int currentFirstVisible = yourLayoutManager.findFirstVisibleItemPosition();

 if(currentFirstVisible > firstVisibleInListview)
   Log.i("RecyclerView scrolled: ", "scroll up!");
 else
   Log.i("RecyclerView scrolled: ", "scroll down!");  

 firstVisibleInListview = currentFirstVisible;

}
Divyang Patel
  • 347
  • 3
  • 14