I started this rabbit hole, trying to hide the floating action button (FAB) when the RecyclerView scrolls. The correct way to do this according to several sources is to extend FloatingActionButton.Behavior , override the onStartNestedScroll and onStopNestedScroll and bind your behavior to FAB, for example app:layout_behavior="com.justingarrick.ui.ScrollAwareFabBehavior" . This works for normal (slow) scroll events, but onStopNestedScroll not called when the reset ends.
There are currently several open issues with behavioral change and scrolling; The workaround for me was to implement the OnScrollListener for my RecyclerView and just change the state of the FAB programmatically, for example.
public class MyFragment extends Fragment { @Bind(R.id.account_list) RecyclerView recyclerView; @Bind(R.id.button_fab) FloatingActionButton fab; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_accounts, container, false); ButterKnife.bind(this, view); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(adapter); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_DRAGGING) fab.hide();
UPDATE This works correctly 99% of the time, but if you use the show() and hide() methods from version 22.2.1 of the design library, you will run into problems when you try to scroll up at the top of your RecyclerView or down at the bottom of your RecyclerView, because viewing The recycler switches states from RecyclerView.SCROLL_STATE_DRAGGING to RecyclerView.SCROLL_STATE_IDLE so fast that it creates a race condition in FloatingActionButtonHoneycombMr1#show() . Thus, to fix this (sigh), you either need to switch to setVisibility() calls if you don't need animations, or re-implement animations without a race condition, for example.
private void hideFab() { fab.animate().scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setDuration(200L).setInterpolator(new FastOutSlowInInterpolator()).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { fab.setVisibility(View.GONE); } }); } private void showFab() { fab.animate().scaleX(1.0F).scaleY(1.0F).alpha(1.0F).setDuration(200L).setInterpolator(new FastOutSlowInInterpolator()).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { fab.setVisibility(View.VISIBLE); } }); }
source share