3

I have a main activity with a drawer that have a container layout where I replace each fragment with FragmentManager.

I want to add a FAB to one of my child fragments that hide/show on scroll but Im not sure what Im doing wrong and get:

enter image description here

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.FloatingActionButton
    style="@style/Widget.Design.FloatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right|end"
    android:layout_marginBottom="@dimen/spacing_medium"
    android:layout_marginRight="@dimen/spacing_medium"
    android:elevation="@dimen/elevation_little"
    app:fabSize="normal"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:srcCompat="@drawable/ic_add_white_18dp" />

</android.support.design.widget.CoordinatorLayout>
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162

3 Answers3

5
  1. Looks like CoordinatorLayout cant handle behaviour of children views that are not "direct children".

  2. You need to add a app:layout_anchorGravity="bottom|right|end" and android:layout_gravity="end|bottom" in your fab view.

For example:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_feeds"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:layout_gravity="end|bottom"
        android:src="@drawable/ic_plus_white"
        app:layout_anchorGravity="bottom|right|end"/>
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
victorldavila
  • 301
  • 2
  • 13
2

add app:layout_anchor="@id/container" to your FAB. Also, by indicating bottom|right there is no need to include end

apelsoczi
  • 1,102
  • 8
  • 11
  • what about this case? http://stackoverflow.com/questions/38330651/fragment-layout-with-fab-conflict-with-coordinatorlayout – Daniel Gomez Rico Jul 12 '16 at 13:45
  • The Floating Action Button is defined within the Fragment itself there, as a member of that ViewGroup, it will obey the scrolling of the parent it is nested in - rather than the Activity – apelsoczi Jul 12 '16 at 14:00
2

Try Below Code

public class FABBehaviour extends FloatingActionButton.Behavior {

    public FABBehaviour(Context context, AttributeSet attrs) {
        super();
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

        //child -> Floating Action Button
        if (dyConsumed > 0) {
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
            int fab_bottomMargin = layoutParams.bottomMargin;
            child.animate().translationY(child.getHeight() + fab_bottomMargin).setInterpolator(new LinearInterpolator()).start();
        } else if (dyConsumed < 0) {
            child.animate().translationY(0).setInterpolator(new LinearInterpolator()).start();
        }
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
    } }

in Xml

app:layout_behavior="Your PpackageName.FABBehaviour"
Nilesh Senta
  • 5,236
  • 2
  • 19
  • 27