1
I’m trying to animate a FAB so that when the scroll occurs up, hides the FAB and when it rolls down, it reappears.
I am following the example of these tutorials that are very similar:
Hide the Floatingactionbutton when scrolling a Recyclerview
Hiding the FAB works, but it doesn’t reappear.
In debug, I realized that when the FAB is hidden, the events responsible for the effect hide/show: onStartNestedScroll
and
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();
}
}
}
Dependencies of Gradle:
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 to make the FAB reappear after it is gone?
It’s gone but it’s not coming, right?
– viana
Exactly. The behavior should be: 1) rolled down, hide. 2) rolled up, reappears. But it only hides and as I said, events cease to occur.
– Rene Freak
Try to compare it like this Else if(dyConsumed < 0 && Child.getVisibility() == View.INVISIBLE)
– Thiago Luiz Domacoski
@Renefreak has as you"and put the Dépend"encia of your Radle in your question?
– viana
@Thiagoluizdomacoski, I’ve done it before. The problem is that after you hide the FAB, the method that this line belongs to is no longer called and so it doesn’t matter if it is INVISIBLE or GONE.
– Rene Freak
@Acklay, I already added
– Rene Freak
Got it, on the first link , he calls the super on the return! : Return nestedScrollAxes == Viewcompat.SCROL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, Child, directTargetChild, target, nestedScrollAxes);
– Thiago Luiz Domacoski
I found a possible bug regarding the dependencies, I will post an answer and you test there. The logic of Fabscrollbehavior is correct.
– viana