Reputation: 5111
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.
Upvotes: 0
Views: 6598
Reputation: 61
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 {
}
}
});
Upvotes: 3
Reputation: 347
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;
}
Upvotes: 2