Why does this FAB disappear, but not reappear?

Asked

Viewed 78 times

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:

FAB: Codepath

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?

  • 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.

  • Try to compare it like this Else if(dyConsumed < 0 && Child.getVisibility() == View.INVISIBLE)

  • @Renefreak has as you"and put the Dépend"encia of your Radle in your question?

  • @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.

  • @Acklay, I already added

  • 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);

  • I found a possible bug regarding the dependencies, I will post an answer and you test there. The logic of Fabscrollbehavior is correct.

Show 3 more comments

1 answer

1

The established logic to your FABScrollBehavior is correct. It should stay the way you set it. But by running some tests here, I found a possible bug between versions of Google support libraries.

You can remove the lib RecyclerView dependence, because this version of its appcompact already has support for RecyclerView. I suspect there is some conflict preventing your button from working properly. See how it should look:

compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:cardview-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
 // Simple Location Library
.
.
.

With the lib Recyclerview

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:design:25.2.0'
    compile 'com.android.support:recyclerview-v7:25.2.0'
}

inserir a descrição da imagem aqui

Without the lib Recyclerview:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:design:25.2.0'
}

inserir a descrição da imagem aqui

If you want to make a comparison, I put the fabscroll project on Github.

  • It didn’t, but it gave me an idea. I’ll work on it and come back with the solution or more information. Vlw anyway!

  • @Renefreak I don’t know, maybe there’s some other lib that’s fighting. Sometimes I think it doesn’t make much sense, but I tested, I re-did, and the result was the gifs. Take a look and tell me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.