I'm trying to animate an FAB so that when it scrolls up, it hides the FAB and when it scrolls down, it reappears.
I'm following the example of these tutorials that are very similar:
Hide the FloatingActionButton when scrolling to RecyclerView
Hiding the FAB works, but it does not reappear.
In the debug, I noticed that when the FAB is hidden, the events responsible for the effect hide / show: onStartNestedScroll
e
onNestedScroll
stop occurring!
This is the layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:background="@color/colorPrimaryDark">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvRunList"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:fabSize="normal"
app:layout_anchor="@+id/rvRunList"
app:layout_anchorGravity="bottom|center"
app:srcCompat="@drawable/ic_fab_add"
app:layout_behavior="br.com.medamais.motonoix.FABScrollBehavior"
/>
</android.support.design.widget.CoordinatorLayout>
This is the code that hides (and should show) the FAB
public class FABScrollBehavior extends FloatingActionButton.Behavior {
public FABScrollBehavior(Context context, AttributeSet attributeSet){
super();
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
if(dyConsumed > 0 && child.getVisibility() == View.VISIBLE){
child.hide();
} else if(dyConsumed < 0 && child.getVisibility() == View.GONE){
child.show();
}
}
}
Gradle dependencies:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:cardview-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
// Simple Location Library
compile 'io.nlopez.smartlocation:library:3.2.11'
// keep play-services v. 10.0.1 while using smartlocation v. 3.2.11
compile 'com.google.android.gms:play-services-maps:10.0.1'
compile 'com.google.android.gms:play-services-ads:10.0.1'
compile 'com.google.android.gms:play-services-places:10.0.1'
// material dialog for permissions
compile 'me.drakeet.materialdialog:library:1.3.1'
testCompile 'junit:junit:4.12'
}
How do I reappear after the FAB resumes?