Example scroll aware FAB Behavior
In the Android Design Support Library [1], we introduced CoordinatorLayout and CoordinatorLayout.Behavior: a framework for rich interactions between views.

On my Design Library post [2], +Bruce too had an excellent question: how do I receive scroll events (say, to hide/show the FAB like G+ does) when there isn't a ListView.setOnScrollListener()? The answer is Behaviors: their onStartNestedScroll() and onNestedScroll() methods make it easy to interact with scroll events.

So I decided to put it to the test with cheesesquare [3]: how can you make your FAB scroll aware, hiding when the user scrolls down and showing when they scroll back up? Turns out, the actual Behavior part is incredibly easy: just two methods to override and remembering to put the app:layout_behavior XML attribute on the FAB, overriding the default FAB behavior with my custom Behavior.

The vast majority of my code was in putting together the actual animation itself: I chose to mimic the FAB's scale/alpha animation used when an overlapping AppBarLayout enters/exits the screen. Of course, you could use any animation or just simply set it VISIBLE or GONE.

You'll note that the Material Design specs for FAB [4] don't have this type of pattern listed, so I'd definitely consider long and hard on if it is the best thing to do - completely hiding things based on scroll without an anchoring view (as is the case in the cheesesquare's detail screen) can be more difficult for new users to work with - some don't know you need to scroll up for the FAB to return!

[1] - http://android-developers.blogspot.com/2015/05/android-design-support-library.html
[2] - https://plus.google.com/+IanLake/posts/QGR5XNcPPeG
[3] - https://github.com/chrisbanes/cheesesquare
[4] - http://www.google.com/design/spec/components/buttons-floating-action-button.html
Shared publiclyView activity