Make FAB appear again after it has been hidden

This commit is contained in:
serso 2017-07-24 12:10:47 +02:00
parent cc75b1f7b1
commit e6aa39659f

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator.view; package org.solovyev.android.calculator.view;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewCompat;
@ -11,6 +12,18 @@ import android.view.View;
public class PagerViewFabBehavior extends FloatingActionButton.Behavior { public class PagerViewFabBehavior extends FloatingActionButton.Behavior {
@NonNull
private final FloatingActionButton.OnVisibilityChangedListener
visibilityListener = new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onHidden(FloatingActionButton fab) {
// by default, FloatingActionButton#hide causes FAB to be GONE which blocks any
// consequent scroll updates in CoordinatorLayout#onNestedScroll. Let's make the
// FAB invisible instead
fab.setVisibility(View.INVISIBLE);
}
};
public PagerViewFabBehavior() { public PagerViewFabBehavior() {
super(); super();
} }
@ -49,13 +62,13 @@ public class PagerViewFabBehavior extends FloatingActionButton.Behavior {
private boolean onScroll(FloatingActionButton child, float scrollX, float scrollY) { private boolean onScroll(FloatingActionButton child, float scrollX, float scrollY) {
if (scrollY > 0 && child.getVisibility() == View.VISIBLE) { if (scrollY > 0 && child.getVisibility() == View.VISIBLE) {
child.hide(); child.hide(visibilityListener);
return true; return true;
} else if (scrollY < 0 && child.getVisibility() != View.VISIBLE) { } else if (scrollY < 0 && child.getVisibility() != View.VISIBLE) {
child.show(); child.show(visibilityListener);
return true; return true;
} else if (scrollX != 0 && child.getVisibility() != View.VISIBLE) { } else if (scrollX != 0 && child.getVisibility() != View.VISIBLE) {
child.show(); child.show(visibilityListener);
return true; return true;
} }
return false; return false;