1

Ich habe eine Hauptaktivität mit einer Schublade, die eine container layout haben, wo ich jedes Fragment mit FragmentManager ersetzen.CoordinatorLayout, FAB und Container Layoutkonflikt

Ich möchte ein FAB zu einem meiner Kind hinzufügen Fragmente, die hide/show auf scroll aber ich bin nicht sicher, was Im falsch zu machen und bekommen:

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> 
+0

erhöhen nur den unteren Rand und es sieht gut aus. –

+0

Sie können den FAB-Block auch in FAB BLOCK setzen und alignParentBottom = true und alignParentRight = true setzen. –

Antwort

2
  1. Sieht aus wie CoordinatorLayout kippe Griff Verhalten von Kindern Ansichten, die nicht "direkte Kinder".

  2. Sie müssen eine app:layout_anchorGravity="bottom|right|end" und android:layout_gravity="end|bottom" in Ihrer fabelhaften Ansicht hinzufügen.

Zum Beispiel:

<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"/> 
+0

Ja, aber der Rand ist derjenige, der es erscheinen – Caipivara

+0

, aber Sie haben App hinzufügen: layout_anchorGravity = "unten | rechts | Ende" Linie? – victorldavila

+1

yeah, ich habe den 'FrameLayout' oder den' RecyclerView' verwendet und gesetzt, aber keine Ergebnisse – Caipivara

1

fügen Sie app:layout_anchor="@id/container" zu Ihrem FAB hinzu. Auch durch die Angabe bottom|right gibt es keine Notwendigkeit zu schließen end

+0

was ist mit diesem Fall? http://stackoverflow.com/questions/38330651/fragment-layout-with-fab-conflict-with-coordinatorlayout – Caipivara

+0

Der Floating-Action-Button ist innerhalb des Fragments selbst definiert, als Mitglied dieser ViewGroup wird er dem Scrollen gehorchen von dem Elternteil ist es verschachtelt - anstatt der Aktivität – apelsoczi

1

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" 
+0

Das löst das Problem nicht, aber es ist eine coole Animation, ich bevorzuge 'child.hide()' und 'child.show()' :). – Caipivara