Fragment layout with FAB conflict with Coordinator

2019-07-02 01:06发布

问题:

Im using MaterialDrawer with a MainDrawerActivity where I replace each fragment inside container FrameLayout based on selected item, but I want to add a FAB (just for this fragment) that interacts with CoordinatorLayout so it can handle cool animations.

MainDrawer layout:

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

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

    <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?actionBarSize"
      android:minHeight="?actionBarSize"
      android:theme="@style/Toolbar"
      app:layout_scrollFlags="scroll|enterAlways" />

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

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

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

How I replace each fragment:

try {
  supportFragmentManager.beginTransaction().replace(R.id.container, FeedFragment()).commit()
} catch (e: IllegalStateException) {
  Timber.i(e, "Fragment is still there.")
}

Fragment Layout:

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

  <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbars="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:listitem="@layout/feed_item" />

  <android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="@dimen/spacing_medium"
    android:layout_marginRight="@dimen/spacing_medium"
    android:elevation="@dimen/elevation_little"
    app:fabSize="normal"
    app:layout_anchor="@+id/container"
    app:layout_anchorGravity="bottom|right"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:srcCompat="@drawable/ic_add_white_18dp" />

</FrameLayout>

But as result the FAB button is behind bottom bar:

Im trying setting the coordinator layout attribues (layout_anchor and those) to match the main layout ids but it is not working... why?

回答1:

Define the Floating Action Button within the Activity - currently the Fragment's container is considered the parent.

FAB should be a direct child of CoordinatorLayout