简体   繁体   中英

How to hide FloatingActionMenu inside CoordinatorLayout on Scroll?

Im using this great library to create a FloatingActionMenu . I use it inside a coordinator layout:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/evaluations_list_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/evaluations_list_textview_time"
    android:layout_below="@+id/evaluations_list_textview_name"
    android:importantForAccessibility="no">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/evaluations_list_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:importantForAccessibility="no">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/evaluations_list_row_parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:importantForAccessibility="no"
            android:padding="10dp"
            android:textColor="@color/textcolorprimary" />
    </android.support.v4.widget.SwipeRefreshLayout>

    <com.github.clans.fab.FloatingActionMenu
        android:id="@+id/evaluations_list_floating_action_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:clickable="true"
        android:scaleType="fitXY"
        android:src="@drawable/vector_drawable_ic_add_white"
        app:layout_anchor="@id/evaluations_list_row_parent"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_behavior="my.project.views.buttons.ScrollFAMBehaviour"
        app:menu_animationDelayPerItem="50"
        app:menu_colorNormal="@color/primaryColorDark"
        app:menu_colorPressed="@color/accentColor"
        app:menu_fab_size="normal"
        app:menu_openDirection="up">

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/evaluations_list_floating_action_button_filter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/vector_drawable_ic_check_white"
            app:fab_colorNormal="@color/primaryColorDark"
            app:fab_colorPressed="@color/accentColor"
            app:fab_label="@string/fab_filter"
            app:fab_size="mini" />

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/evaluations_list_floating_action_button_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/vector_drawable_ic_search_white"
            app:fab_colorNormal="@color/primaryColorDark"
            app:fab_colorPressed="@color/accentColor"
            app:fab_label="@string/fab_search"
            app:fab_size="mini" />

    </com.github.clans.fab.FloatingActionMenu>

</android.support.design.widget.CoordinatorLayout>

Now i want to create a CoordinatorLayout.Behavior to hide the complete menu when the user is scrolling down and i want it to show up again when the user is scrolling up.

Bevor i used the FloatingActionMenu i used the classic FloatingActionButton . For that it was easily possible to achieve what i want using the following code:

public class ScrollFABBehaviour extends FloatingActionButton.Behavior {
    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
    private boolean isAnimatingOut = false;

    public ScrollFABBehaviour(Context context, AttributeSet attrs) {
        super();
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
    }

    @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 && !this.isAnimatingOut && child.getVisibility() == View.VISIBLE) {
            this.animateOut(child);
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            this.animateIn(child);
        }
    }

    private void animateOut(final FloatingActionButton button) {
         ViewCompat.animate(button).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(new ViewPropertyAnimatorListener() {

             public void onAnimationStart(View view) {
                ScrollFABBehaviour.this.isAnimatingOut = true;
             }

             public void onAnimationCancel(View view) {
                 ScrollFABBehaviour.this.isAnimatingOut = false;
             }

             public void onAnimationEnd(View view) {
                 ScrollFABBehaviour.this.isAnimatingOut = false;

                 view.setVisibility(View.GONE);
             }
         }).start();
    }

    private void animateIn(FloatingActionButton button) {
        button.setVisibility(View.VISIBLE);

        ViewCompat.animate(button).scaleX(1.0F).scaleY(1.0F).alpha(1.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(null).start();
    }
}

Unfortunately i cant use this code on the FloatingActionMenu because it is not a FloatingActionButton and also does not inherit it. It is a custom ViewGroup .

How could i create such a behavior for the FloatingActionMenu ?

This behavior is really obtained by creating a CoordinatorLayout.Behavior<View> . So instead of extending from FloatingActionButton.Behavior extend from CoordinatorLayout.Behavior<FloatingActionMenu> directly.

Here is the equivalent code that will work with the FloatingActionMenu .

public class ScrollFAMBehaviour extends CoordinatorLayout.Behavior<FloatingActionMenu>{
    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
    private boolean isAnimatingOut = false;

    public ScrollFAMBehaviour(Context context, AttributeSet attrs) {
        super();
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionMenu child, View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionMenu child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

        if (dyConsumed > 0 && !this.isAnimatingOut && child.getVisibility() == View.VISIBLE) {
            this.animateOut(child);
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            this.animateIn(child);
        }
    }

    private void animateOut(final FloatingActionMenu menu) {
        ViewCompat.animate(menu).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(new ViewPropertyAnimatorListener() {

            public void onAnimationStart(View view) {
                ScrollFAMBehaviour.this.isAnimatingOut = true;
            }

            public void onAnimationCancel(View view) {
                ScrollFAMBehaviour.this.isAnimatingOut = false;
            }

            public void onAnimationEnd(View view) {
                ScrollFAMBehaviour.this.isAnimatingOut = false;

                view.setVisibility(View.GONE);
            }
        }).start();
    }

    private void animateIn(FloatingActionMenu menu) {
        menu.setVisibility(View.VISIBLE);

        ViewCompat.animate(menu).scaleX(1.0F).scaleY(1.0F).alpha(1.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(null).start();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM