I'm trying to only display a floating action button when the Recycler View is scrolling down and hide when position is at the very top. I'm sorry my english is very bad
Asked
Active
Viewed 741 times
-2
-
6Possible duplicate of [Hide FloatingActionButton on scroll of RecyclerView](https://stackoverflow.com/questions/33208613/hide-floatingactionbutton-on-scroll-of-recyclerview) – Mohan Sai Manthri Sep 17 '19 at 05:00
3 Answers
1
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx,int dy){
super.onScrolled(recyclerView, dx, dy);
if (dy >0) {
// Scroll Down
if (fab.isShown()) {
fab.hide();
}
}
else if (dy <0) {
// Scroll Up
if (!fab.isShown()) {
fab.show();
}
}
}
})

Mohan Sai Manthri
- 2,808
- 1
- 11
- 26
0
here what you can do to identify scrolling state
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!recyclerView.canScrollVertically(1) && newState==RecyclerView.SCROLL_STATE_IDLE) {
//do what you want if recyclerview scrolled to bottom
}else{
//do what you want if recyclerview not scrolled to bottom
}
}
});

Black mamba
- 462
- 4
- 15
0
Add scroll listener for recylerview & hide fab when scrolling
fun hideFabWhenScroll(fab: FloatingActionButton, recyclerView: RecyclerView){
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (dy > 0 || dy < 0 && fab.isShown) {
fab.hide()
}
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
fab.show()
}
super.onScrollStateChanged(recyclerView, newState)
}
})
}

Ranjithkumar
- 16,071
- 12
- 120
- 159