FAB disappearing on RecyclerView with StaggeredGridLayout
I'm trying to implement hide/show events on scrolling a RecyclerView with a StaggeredGridLayout. Right now even if I don't invoke fab.attachToRecyclerView(recyclerView) it keeps disappearing when I scroll down. During the scrolling in the onScrolled callback the method fab.isShown() does always return true.
The layout is really trivial:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e8e8e8"
android:scrollbars="vertical" />
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_plus"
app:fab_colorNormal="@color/accentLight"
app:fab_colorPressed="@color/accent" />
</FrameLayout>
while in my fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_notes_list, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(howManyColumnsDisplayable(), StaggeredGridLayoutManager.VERTICAL));
recyclerView.setAdapter(adapter);
fab = (FloatingActionButton) view.findViewById(R.id.fab);
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
boolean scrollDown;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (scrollDown) {
if (toolbarHandler.isToolbarShown())
toolbarHandler.hideToolbar();
if (fab.isShown()) {
Log.d(TAG, "Hiding FAB");
fab.hide(true);
}
} else {
if (toolbarHandler.isToolbarHidden())
toolbarHandler.showToolbar();
if (!fab.isShown()) {
Log.d(TAG, "Showing FAB");
fab.show(true);
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 70) {
scrollDown = true;
} else if (dy < -5) {
scrollDown = false;
}
}
});
return view;
}
Am I missing something?
That's because dy is the amount of scroll and not the currentScroll position. You should keep a global variable: private currentScrollY=0;
and then update it:
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); currentScrollY+=dy; if (currentScrollY > 70) { scrollDown = true; } else if (currentScrollY < -5) { scrollDown = false; } }
what is "toolbarHandler"???????????
this is correct answer :
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();
}
}
}
});