0

I have a floating action button which is shown or hidden on RecyclerView using a custom behaviour. Anyway, I'd like the FAB to be hidden also when I'm selecting some items from the list (so when the ActionMode is active). I actually hide the FAB when the ActionMode starts (onCreateActionMode), but it reappears if I scroll the list. Any ideas?

Layout:

<?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:id="@+id/coordinator">

    <android.support.v7.widget.RecyclerView
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list" />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_gravity="bottom|end"
        android:src="@drawable/ic_add"
        app:layout_behavior="package.Layout.FABScrollBehavior"
        app:backgroundTint="@color/accent"
        android:id="@+id/fab"/>
</android.support.design.widget.CoordinatorLayout>

Behaviour:

public class FABScrollBehavior extends FloatingActionButton.Behavior {
    public FABScrollBehavior(Context context, AttributeSet attributeSet){
        super();
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
        return dependency instanceof RecyclerView;
    }

    @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();
        }
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }
}
  • Have you tried setting a boolean "ActionMode" flag that gets checked in your `onNestedScroll`? – freddieptf Jul 12 '16 at 11:21
  • I was thinking about adding an interface (in the activity) and implement it in the behaviour, but I was wondering if there is an easier way to achieve this. – Michele Scuttari Jul 13 '16 at 12:35
  • 1
    I think an interface here is overkill. I would create a method in your behavior that sets a boolean flag which will get checked in the onNestedScroll. This also means you'd have apply the behavior in your Java code so you can have an instance of it to use the method – freddieptf Jul 13 '16 at 13:42
  • Oh you're right, that is absolutely more simple.By the way I get it working also with interfaces, but, as you said, it's a bit too much invasive solution. Thanks! – Michele Scuttari Jul 13 '16 at 15:44

0 Answers0