浮动操作按钮不上下移动

时间:2016-06-07 11:07:04

标签: android android-coordinatorlayout floating-action-button

问题:我已经实施了FloatingActionButton,我跟随了Androidhive的示例。但是,当显示SnackBar时,FloatingActionButton不会向上移动。

问题:如果出现FloatingActionButton,我如何才能实现SnackBar上下移动?

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout android:id="@+id/maschineCoordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <include
            android:id="@+id/toolbar_main"
            layout="@layout/toolbar"></include>

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

    <FrameLayout
        android:id="@+id/maschinelistcontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior = "@string/appbar_scrolling_view_behavior"
       />

    <com.github.jorgecastilloprz.FABProgressCircle
        android:id="@+id/fabProgressCircle_download_maschinen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        >

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_download_maschinen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:borderWidth="0dp"
            app:elevation="@dimen/fab_elevation"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email" />

    </com.github.jorgecastilloprz.FABProgressCircle>

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

3 个答案:

答案 0 :(得分:0)

这是FloatingAction Behavior class

<强> FloatingActionButtonBehavior.java

public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {

  public FloatingActionButtonBehavior(Context context, AttributeSet attrs) {
  }

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

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

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Snackbar.make(view, "Hello Snackbar", Snackbar.LENGTH_LONG).show();
      }
    });
  }
}

<强> activity_main.xml中

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

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        app:layout_behavior="com.getbase.coordinatorlayoutdemo.FloatingActionButtonBehavior"
        app:fab_icon="@drawable/ic_done" />

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

输出

enter image description here

有关详细信息,请访问:https://github.com/ggajews/coordinatorlayoutwithfabdemo

答案 1 :(得分:0)

我正在使用com.android.support:design:24.2.0并且为了工作你需要将yor FAB放在CoordinatorLayout中,我不知道你是否可以像你一样进行包装。 然后写下您的FAB:

app:layout_anchor="@id/maschinelistcontainer"
app:layout_anchorGravity="bottom|right|end"

像这样:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_download_maschinen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="bottom|end"
    app:borderWidth="0dp"
    app:elevation="@dimen/fab_elevation"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email"
    app:layout_anchor="@id/maschinelistcontainer"
    app:layout_anchorGravity="bottom|right|end"     />

有关详情:https://code.google.com/p/android/issues/detail?id=220250

答案 2 :(得分:-1)

我认为你确实实现了FABProgressListener接口

如果没有。你已经这样做了。

你应该使用下面的零食吧:

@Override public void onFABProgressAnimationEnd() {
    Snackbar.make(fabProgressCircle, R.string.cloud_upload_complete, Snackbar.LENGTH_LONG)
        .setAction("Action", null)
        .show();
}

你是芦苇this page吗?