如何让小吃店和工厂一起移动

How to make snack bar and fab to move together

我想移动小吃店和 fab 一起移动,就像在默认行为中发生的那样,但默认情况下它仅在我们单击 fab 时发生,fab 为 snackbar 腾出位置并且 fab 本身向上移动,

但是希望它在其他事件发生时发生,例如布局上喜欢的其他按钮而不是 fab 本身,当布局上的其他按钮点击时,我如何触发 fab 向上移动并为 snackbar 腾出位置

谢谢

点击按钮:

 public void onClick(View view) {
             if(spinner.getText().length() <= 0){
                 showListMenu(spinner);
             }else{
                 showSnack("Added to bag we will hold it for 1 hour!", view.getRootView());
             }
        }

showSnack 内部:

 Snackbar.make(view.getRootView(), getResources().getString(R.string.fab_msg), Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

    final Animation myAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.bounce);
    double animationDuration = 2 * 1000;
    myAnim.setDuration((long)animationDuration);

    MyBounceInterpolator interpolator = new MyBounceInterpolator(0.20, 20.00);

    myAnim.setInterpolator(interpolator);
    fab.startAnimation(myAnim);

像这样用小吃店锚定工厂:

Snackbar.make(view.findViewById(R.id.fab), getResources().getString(R.string.fab_msg), Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();

CoordinatorLayout就够了,不用改Java代码。在 CoordinatorLayout

中添加按钮
    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

点击事件SnackBarFab都平滑向上移动

  fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    toolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });