1

我有一个以 Coordinator 布局作为主要内容托管的 DrawerLayout。当我使用 Snackbar.make 方法创建 SnackBar 时,托管在 Coordinator 布局中的 FAB 拒绝动画。

我觉得这很奇怪,因为我在没有包装在 DrawerLayout 中的 Coordinator 布局中使用了相同的 FAB,并且它的动画效果很好,导致人们相信 DrawerLayout 以某种方式阻止了回调。

我试过让 CoordinatorLayout 成为顶层视图,包装 DrawerLayout,但这也不起作用。我将尝试分叉 FloatingActionButton Behavior 类并将 updateFabTranslationForSnackbar 方法公开,以便我可以自己调用它。我宁愿不这样做,所以任何想法都将不胜感激。

对于这两个活动,Snackbar.make 调用是从动态添加到具有 ID“容器”的 RelativeLayout 的片段中调用的。传递给调用的视图是活动 XML 中的 CoordinatorLayout,ID 为“coordinator_layout”。

同样,在第一个 XML 中一切正常,但在第二个 XML 中则不然。

这是我用于其他活动的默认 XML,这里的 FAB 动画效果很好:

<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:name="com.app.mobile.app.ui.BusinessActivityFragment"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ProgressBar
    android:id="@+id/progress_bar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="8dp" />

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">

    <ImageView
        android:id="@+id/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:scaleType="centerCrop"
        android:src="@drawable/bg_sign_up"
        android:visibility="invisible" />

    <View
        android:id="@+id/background_mask"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:background="@color/black_40"
        android:visibility="invisible" />
</RelativeLayout>

<include
    android:id="@+id/action_bar"
    layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/abc_action_bar_default_height_material"
    android:layout_gravity="top" />

<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="16dp" />

<ProgressBar
    android:id="@+id/load_more"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="@dimen/half_margin"
    android:layout_gravity="bottom"
    android:indeterminate="true"
    android:visibility="gone" />

这是我的主要活动的 XML,FAB 拒绝动画:

<android.support.v4.widget.DrawerLayout
android:id="@+id/navigation_layout"
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="com.app.mobile.app.ui.HomeActivity">
<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true">
    <!-- Toolbar is the last item in the FrameLayout to cause it to overlay -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:orientation="vertical">

        <ProgressBar
            android:id="@+id/home_progress"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:layout_marginBottom="8dp" />

        <RelativeLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone" />
    </RelativeLayout>

    <include
        android:id="@+id/action_bar"
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/abc_action_bar_default_height_material"
        android:layout_gravity="top" />

    <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="16dp" />

    <ProgressBar
        android:id="@+id/load_more"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="@dimen/half_margin"
        android:layout_gravity="bottom"
        android:indeterminate="true"
        android:visibility="gone" />
</android.support.design.widget.CoordinatorLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment
    android:id="@+id/navigation_drawer"
    android:name="com.app.mobile.app.ui.NavigationDrawerFragment"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:tag="NAVIGATION_DRAWER_TAG"
    tools:layout="@layout/fragment_navigation_drawer" />

4

2 回答 2

1

确保您将正确的视图传递给该Snackbar.make()方法。根据文档,

Snackbar 将尝试从给定的值中找到一个父视图来保存 Snackbar 的视图。Snackbar 将沿着视图树向上寻找合适的父级,该父级定义为 CoordinatorLayout 或窗口装饰的内容视图,以先到者为准。

所以你应该传递一个实际上在内部的视图CoordinatorLayout,这样 Snackbar 就会发现协调器是第一个可用的父级。

于 2015-06-20T13:49:54.083 回答
0

我找到了解决方案。您显然必须移至新设计库中的导航视图小部件。移至此并保持所有其他内容不变,FAB 再次动画。

有关实现,请参阅支持库的官方博客文章和Chris Banes 的库演示

这不是一个理想的解决方案,因为您失去了对静态膨胀 XML 资源的片段的灵活性,但这是我发现使 FAB 仍然具有动画效果的唯一方法。

于 2015-06-20T23:03:51.080 回答