Google have released Design library, I am using
compile 'com.android.support:design:22.2.1'
However I cant see any code examples of how to use this library and how to animate the FAB
bar on scroll. I guess I can listen for scroll events on the ListView
and then animate the button myself, but is this not baked into the API (is this not the point of this support library).
Is there examples for this ?
Making a component react to scroll events is most easily done with a custom CoordinatorLayout.Behavior, as they receive scroll events automatically when you override onStartNestedScroll().
An example Behavior that hides and shows the FAB on scroll found in this FABAwareScrollingViewBehavior, built on top of cheesesquare:
Where your scrolling view would have
app:layout_behavior="com.support.android.designlibdemo.FABAwareScrollingViewBehavior"
instead ofapp:layout_behavior="@string/appbar_scrolling_view_behavior"
However you can replace
hide()
andshow()
with any action if you want. Details on how this was done can be found in this post and the follow up post for v22.2.1 and the follow up post for v25.1.0.Note that this, like all scrolling behaviors of the Design Library, require that your view supports nested scrolling, which currently limits you to NestedScrollView, RecyclerView -
ListView
andScrollView
only work on API21+ devices.If you're NOT using a RecycleView (that is, just regular ScrollView) this will do the trick:
Don't forget to declare:
inside you class.
The @ianhanniballake is working fine but the methods
onStartNestedScroll()
andonNestedScroll()
were deprecated. Here is the updated version:Also there is a very good post by @ianhanniballake on this topic: Intercepting everything with CoordinatorLayout Behaviors
If you're using
RecyclerView
and you're looking for something simple, you can try this:By replacing
0
with a constant, you can adjust the sensitivity of triggering, providing smoother experienceUsing CoordinatorLayout is best way. Hoever if you want to attach listener to ListView or RecyclerView you also can do that. I think is more customizable. Here is my example on git hub.
Github Project: Hide FAB(material Library) with listview
This works very well