Skip to content

Instantly share code, notes, and snippets.

@ar-android
Created January 19, 2018 18:14
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ar-android/58e60b23ff1e51f69e08a22cd6466038 to your computer and use it in GitHub Desktop.
Save ar-android/58e60b23ff1e51f69e08a22cd6466038 to your computer and use it in GitHub Desktop.
Show and Hide Floating Action Button when scrolling recyclerview
// Java
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0 && fab.getVisibility() == View.VISIBLE) {
fab.hide();
} else if (dy < 0 && fab.getVisibility() != View.VISIBLE) {
fab.show();
}
}
});
// Kotlin
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0 && fab.visibility === View.VISIBLE) {
fab.hide()
} else if (dy < 0 && mFloatingActionButton.visibility !== View.VISIBLE) {
fab.show()
}
}
})
@mostafa-n3ma
Copy link

great thanks

@NawfalHadi
Copy link

Hey, "addOnScrollListener" are depecrated, what's the alternative ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment