Snackbar 不移动放置在 Fragment 中的 FAB

Snackbar not moving FAB that placed inside Fragment

有一个方案描述了我的应用程序的视图层次结构。

还有一些XML

MainActivity

. CoordinatorLayout
  . FrameLayout <- ContentFragment inserted here
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
  . AppBarLayout
    . Toolbar

ContentFragment

<FrameLayout
    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.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:src="@drawable/ic_plus_white_36dp"
        android:layout_gravity="bottom|end"
        android:layout_margin="15dp"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp"/>

    <TextView
        android:id="@+id/hint_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/empty_dates_list"
        android:padding="20dp"
        android:textSize="20sp"
        android:layout_gravity="center" />

</FrameLayout>

我用这段代码 SnackBar:

Snackbar snackbar = Snackbar.make(
        mainActivity.getCoordinatorLayout(),
        R.string.date_removed,
        Snackbar.LENGTH_LONG);

我的问题fab 没有按我在 ContentFragment

中制作的 SnackBar 向上移动

我也尝试了点 fab 作为 Snackbar 的观点,但没有带来结果。

为此,FloatingActionButton 应该是 CoordinatorLayout 的 child。你的是 FrameLayout 的 child。

您应该使用 CoordinatorLayout 来避免这个问题,因为 CoordinatorLayout 单独正确协调由其子视图引起的布局更改。

要解决您的问题,只需将 FloatingActionButton 从片段布局移动到 MainActivity 布局即可。或者,如果您打算在片段本身中使用 FloatingActionButton,则将片段的父视图设置为 CoordinatorLayout