简体   繁体   English

Android设计支持库的菜单和Autohide FloatingActionButton

[英]Menu and Autohide FloatingActionButton of Android Design Support Library

I'm using Android Design Support Library and I want a FloatingActionButton that have AutoHide by Scrolling, 我正在使用Android设计支持库,我想要一个通过滚动具有AutoHide的FloatingActionButton,

my Layout is: 我的布局是:

<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">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/LargeText" />
    </ScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_anchor="@id/scrollView"
        android:src="@drawable/abc_btn_rating_star_off_mtrl_alpha" />

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

FloatingActionButton always showing when scrolling text, i want to autohide it when scrolling text. FloatingActionButton总是在滚动文本时显示,我想在滚动文本时自动隐藏它。

And, I want to have a FloatingActionButton Menu by clicking on FloatingActionButton, like this: 并且,我希望通过单击FloatingActionButton来获得FloatingActionButton菜单,如下所示:

在此输入图像描述

FloatingActionButton that have AutoHide by Scrolling, 通过滚动具有AutoHide的FloatingActionButton,

You must use android.support.v4.widget.NestedScrollView instead of ScrollView . 您必须使用android.support.v4.widget.NestedScrollView而不是ScrollView You can't use the ScrollView. 您无法使用ScrollView。 You have to use the NestedScrollView or a view that implements the NestedScrollingChild interface, like a RecyclerView. 您必须使用NestedScrollView或实现NestedScrollingChild接口的视图,如RecyclerView。

To achieve this kind of pattern you have to implement your custom Behavior . 要实现此类模式,您必须实现自定义Behavior There is a nice code posted by a Googler that hides the FAB when the user scrolls down and shows it when they scroll back up. Googler发布了一个很好的代码,当用户向下滚动时会隐藏FAB,并在向上滚动时显示它。 Reuses the same animation that FloatingActionButton.Behavior uses for hiding/showing the FAB in reaction to the AppBarLayout exiting/entering. 重用FloatingActionButton.Behavior用于隐藏/显示FAB以响应退出/进入的AppBarLayout的相同动画。

UPDATED 18/07/2015 2015年7月18日更新

With the 22.2.1 you can simply add the code posted below, using the pre-built animations. 使用22.2.1,您只需使用预先构建的动画添加下面发布的代码即可。 Just use a class like this: (original source here ) 只要使用这样的类:(原始的源位置

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

    @Override
    public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
                                       final View directTargetChild, final View target, final int nestedScrollAxes) {
        // Ensure we react to vertical scrolling
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
                || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
                               final View target, final int dxConsumed, final int dyConsumed,
                               final int dxUnconsumed, final int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
            // User scrolled down and the FAB is currently visible -> hide the FAB
            child.hide();
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            // User scrolled up and the FAB is currently not visible -> show the FAB
            child.show();
        }
    }
}

Then you can apply this behaviour to your FAB using: 然后,您可以使用以下方法将此行为应用于FAB:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
     app:layout_behavior="com.support.android.designlibdemo.ScrollAwareFABBehavior" />

With Design 22.2.0 : You have to use a class like this: (original source here ) 随着设计22.2.0:你要使用这样的类:(原始的源位置

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

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

    @Override
    public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
                                       final View directTargetChild, final View target, final int nestedScrollAxes) {
        // Ensure we react to vertical scrolling
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
                || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
                               final View target, final int dxConsumed, final int dyConsumed,
                               final int dxUnconsumed, final int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {
            // User scrolled down and the FAB is currently visible -> hide the FAB
            animateOut(child);
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            // User scrolled up and the FAB is currently not visible -> show the FAB
            animateIn(child);
        }
    }

    // Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits
    private void animateOut(final FloatingActionButton button) {
        if (Build.VERSION.SDK_INT >= 14) {
            ViewCompat.animate(button).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer()
                    .setListener(new ViewPropertyAnimatorListener() {
                        public void onAnimationStart(View view) {
                            ScrollAwareFABBehavior.this.mIsAnimatingOut = true;
                        }

                        public void onAnimationCancel(View view) {
                            ScrollAwareFABBehavior.this.mIsAnimatingOut = false;
                        }

                        public void onAnimationEnd(View view) {
                            ScrollAwareFABBehavior.this.mIsAnimatingOut = false;
                            view.setVisibility(View.GONE);
                        }
                    }).start();
        } else {
            Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_out);
            anim.setInterpolator(INTERPOLATOR);
            anim.setDuration(200L);
            anim.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation animation) {
                    ScrollAwareFABBehavior.this.mIsAnimatingOut = true;
                }

                public void onAnimationEnd(Animation animation) {
                    ScrollAwareFABBehavior.this.mIsAnimatingOut = false;
                    button.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(final Animation animation) {
                }
            });
            button.startAnimation(anim);
        }
    }

    // Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters
    private void animateIn(FloatingActionButton button) {
        button.setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= 14) {
            ViewCompat.animate(button).scaleX(1.0F).scaleY(1.0F).alpha(1.0F)
                    .setInterpolator(INTERPOLATOR).withLayer().setListener(null)
                    .start();
        } else {
            Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_in);
            anim.setDuration(200L);
            anim.setInterpolator(INTERPOLATOR);
            button.startAnimation(anim);
        }
    }
}

Then you can apply this behaviour to your FAB using: 然后,您可以使用以下方法将此行为应用于FAB:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
     app:layout_behavior="com.support.android.designlibdemo.ScrollAwareFABBehavior" />

Of course you can change this code to obtain your favorite pattern. 当然,您可以更改此代码以获取您喜欢的模式。

And, I want to have a FloatingActionButton Menu by clicking on FloatingActionButton, like this: 并且,我希望通过单击FloatingActionButton来获得FloatingActionButton菜单,如下所示:

Currently the original FAB doesn't support this pattern. 目前,原始FAB不支持此模式。 You have to implement a custom code to achieve it. 您必须实现自定义代码才能实现它。

You can achieve it 你可以实现它

Here's my code. 这是我的代码。

Step 1: 步骤1:

First make Getter of FloatingActionMenu so that you can call it from another activity or from fragment where your recycleview is used 首先制作GetA of FloatingActionMenu,以便您可以从另一个活动或使用您的recycleview的片段中调用它

public FloatingActionMenu getFloatingActionMenu() {
        return fabMenu;
    }

Step 2: 第2步:

Call below line from another activity or from fragment 从另一个活动或片段调用以下行

   FloatingActionMenu fabMenu=((MainActivity)getActivity()).getFloatingActionMenu();

Step 3: 第3步:

Now Check wheather recycleview is scrolling or not depend on "dy" position Here I have used animation fabMenu 现在检查wheather recycleview是否滚动或不依赖于“dy”位置这里我使用了动画fabMenu

Animation FabMenu_fadOut = AnimationUtils.loadAnimation(getActivity(),
            R.anim.fade_out);
Animation  FabMenu_fadIn = AnimationUtils.loadAnimation(getActivity(),
            R.anim.abc_grow_fade_in);

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0 && floatingActionButton.isShown()) {
                    //fabMenu.startAnimation(FabMenu_fadIn);
                    fabMenu.setVisibility(View.GONE);
                }
                if (dy < 0 && !floatingActionButton.isShown()) {
                   // fabMenu.startAnimation(FabMenu_fadOut);
                    fabMenu.setVisibility(View.VISIBLE);
                }
            }
        });

Note : If you want to hide FloatingActionButton on scroll then use same code as in FloatingActionMenu. 注意:如果要在滚动时隐藏FloatingActionButton,请使用与FloatingActionMenu中相同的代码。

Thank you. 谢谢。

You can implement floating action menu using this . 您可以使用实现浮动操作菜单。

Regarding the animation for floating action menu, you can create a class called ScrollAwareFloatingActionMenuBehaviour: 关于浮动操作菜单的动画,您可以创建一个名为ScrollAwareFloatingActionMenuBehaviour的类:

public class ScrollAwareFloatingActionMenuBehaviour extends CoordinatorLayout.Behavior<FloatingActionsMenu> {
    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
    private boolean mIsAnimatingOut = false;
    private boolean mIsAnimatingIn = false;

    public ScrollAwareFloatingActionMenuBehaviour(Context context, AttributeSet attrs) {
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionsMenu child, View dependency) {
        return dependency instanceof Snackbar.SnackbarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionsMenu child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
                                       FloatingActionsMenu 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, FloatingActionsMenu child,
                               View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
                dyUnconsumed);

        if (dyConsumed > 10 && !this.mIsAnimatingOut && !this.mIsAnimatingIn && child.getVisibility() == View.VISIBLE) {
            // User scrolled down and the FAB is currently visible -> hide the FAB
            animateOut(child);
        } else if (dyConsumed < -10 && !this.mIsAnimatingOut && !this.mIsAnimatingIn && child.getVisibility() != View.VISIBLE) {
            // User scrolled up and the FAB is currently not visible -> show the FAB
            animateIn(child);
        }
    }

    // Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits
    private void animateOut(final FloatingActionsMenu button) {
        if (Build.VERSION.SDK_INT >= 14) {
            ViewCompat.animate(button).translationYBy(200F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer()
                    .setListener(new ViewPropertyAnimatorListener() {
                        public void onAnimationStart(View view) {
                            ScrollAwareFloatingActionMenuBehaviour.this.mIsAnimatingOut = true;
                        }

                        public void onAnimationCancel(View view) {
                            ScrollAwareFloatingActionMenuBehaviour.this.mIsAnimatingOut = false;
                        }

                        public void onAnimationEnd(View view) {
                            ScrollAwareFloatingActionMenuBehaviour.this.mIsAnimatingOut = false;
                            view.setVisibility(View.GONE);
                        }
                    }).start();
        } else {
            Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.design_fab_out);
            anim.setInterpolator(INTERPOLATOR);
            anim.setDuration(200L);
            anim.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation animation) {
                    ScrollAwareFloatingActionMenuBehaviour.this.mIsAnimatingOut = true;
                }

                public void onAnimationEnd(Animation animation) {
                    ScrollAwareFloatingActionMenuBehaviour.this.mIsAnimatingOut = false;
                    button.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(final Animation animation) {
                }
            });
            button.startAnimation(anim);
        }
    }

    // Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters
    private void animateIn(FloatingActionsMenu button) {
        button.setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= 14) {
            ViewCompat.animate(button).translationYBy(-200F).alpha(1.0F)
                    .setInterpolator(INTERPOLATOR).withLayer().setListener(new ViewPropertyAnimatorListener() {
                @Override
                public void onAnimationStart(View view) {
                    ScrollAwareFloatingActionMenuBehaviour.this.mIsAnimatingIn = true;

                }

                @Override
                public void onAnimationEnd(View view) {
                    ScrollAwareFloatingActionMenuBehaviour.this.mIsAnimatingIn = false;

                }

                @Override
                public void onAnimationCancel(View view) {
                    ScrollAwareFloatingActionMenuBehaviour.this.mIsAnimatingIn = false;

                }
            })
                    .start();
        } else {
            Animation anim = AnimationUtils.loadAnimation(button.getContext(), android.support.design.R.anim.design_fab_in);
            anim.setDuration(200L);
            anim.setInterpolator(INTERPOLATOR);
            button.startAnimation(anim);
        }
    }
}

Note that R.anim.fab_in and R.anim.fab_out are replaced by R.anim.design_fab_in and R.anim.design_fab_out respectively. 请注意,R.anim.fab_in和R.anim.fab_out分别由R.anim.design_fab_in和R.anim.design_fab_out取代。

Use it in the xml: 在xml中使用它:

<com.getbase.floatingactionbutton.FloatingActionsMenu
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fab_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        fab:fab_addButtonColorNormal="@color/colorAccent"
        fab:fab_addButtonColorPressed="@color/colorAccentLight"
        fab:fab_addButtonPlusIconColor="@android:color/white"
        app:layout_behavior="com.example.widgets.behaviour.ScrollAwareFloatingActionMenuBehaviour"
        fab:fab_labelStyle="@style/menu_labels_style"
        fab:fab_labelsPosition="left">

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/fab_normal_blue"
            fab:fab_colorPressed="@color/fab_normal_blue_pressed"
            fab:fab_icon="@drawable/ic_social_share"
            fab:fab_title="@string/fab_menu_group_chat" />

</com.getbase.floatingactionbutton.FloatingActionsMenu>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有Android Design支持库的FloatingActionButton(FAB)没有提升 - FloatingActionButton (FAB) with Android Design support library not elevating 如何为新的设计支持库的FloatingActionButton设置动画 - How to animate FloatingActionButton of new Design Support Library Android支持设计FloatingActionButton形状的问题 - Issue with Android Support Design FloatingActionButton shape Android设计支持库辅助抽屉菜单 - Android Design Support Library Secondary Drawer Menu 导入 android.support.design.widget.FloatingActionButton; - import android.support.design.widget.FloatingActionButton; 在 Androidx 库上膨胀类 android.support.design.widget.FloatingActionButton 时出错 - Error inflating class android.support.design.widget.FloatingActionButton on Androidx library 带支持库的 FloatingActionButton 示例 - FloatingActionButton example with Support Library 在Android中输入类android.support.design.widget.FloatingActionButton时出错 - Error inflating class android.support.design.widget.FloatingActionButton in Android 错误膨胀类android.support.design.widget.FloatingActionButton - android - Error inflating class android.support.design.widget.FloatingActionButton - android Android设计支持库 - Android Design Support Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM