我有一个以 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" />