2016-05-14 4 views
2

Ich benutze diese großartige library, um eine FloatingActionMenu zu erstellen. Ich benutze es in einem Koordinator Layout:FloatingActionMenu in CoordinatorLayout bei Scroll ausblenden?

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/evaluations_list_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/evaluations_list_textview_time" 
    android:layout_below="@+id/evaluations_list_textview_name" 
    android:importantForAccessibility="no"> 

    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/evaluations_list_swipe_refresh_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:importantForAccessibility="no"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/evaluations_list_row_parent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clipToPadding="false" 
      android:importantForAccessibility="no" 
      android:padding="10dp" 
      android:textColor="@color/textcolorprimary" /> 
    </android.support.v4.widget.SwipeRefreshLayout> 

    <com.github.clans.fab.FloatingActionMenu 
     android:id="@+id/evaluations_list_floating_action_menu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="16dp" 
     android:layout_marginEnd="16dp" 
     android:clickable="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/vector_drawable_ic_add_white" 
     app:layout_anchor="@id/evaluations_list_row_parent" 
     app:layout_anchorGravity="bottom|right|end" 
     app:layout_behavior="my.project.views.buttons.ScrollFAMBehaviour" 
     app:menu_animationDelayPerItem="50" 
     app:menu_colorNormal="@color/primaryColorDark" 
     app:menu_colorPressed="@color/accentColor" 
     app:menu_fab_size="normal" 
     app:menu_openDirection="up"> 

     <com.github.clans.fab.FloatingActionButton 
      android:id="@+id/evaluations_list_floating_action_button_filter" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="true" 
      android:scaleType="fitXY" 
      android:src="@drawable/vector_drawable_ic_check_white" 
      app:fab_colorNormal="@color/primaryColorDark" 
      app:fab_colorPressed="@color/accentColor" 
      app:fab_label="@string/fab_filter" 
      app:fab_size="mini" /> 

     <com.github.clans.fab.FloatingActionButton 
      android:id="@+id/evaluations_list_floating_action_button_search" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="true" 
      android:scaleType="fitXY" 
      android:src="@drawable/vector_drawable_ic_search_white" 
      app:fab_colorNormal="@color/primaryColorDark" 
      app:fab_colorPressed="@color/accentColor" 
      app:fab_label="@string/fab_search" 
      app:fab_size="mini" /> 

    </com.github.clans.fab.FloatingActionMenu> 

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

Jetzt i ein CoordinatorLayout.Behavior erstellen möchten das komplette Menü auszublenden, wenn der Benutzer nach unten scrollen, und ich will es noch einmal zeigen, wenn der Benutzer nach oben scrollt.

Bevor ich die FloatingActionMenu verwendet habe ich den Klassiker FloatingActionButton verwendet. Dazu ist es leicht möglich war, zu erreichen, was ich den folgenden Code will mit:

public class ScrollFABBehaviour extends FloatingActionButton.Behavior { 
    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator(); 
    private boolean isAnimatingOut = false; 

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

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

    @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); 

     if (dyConsumed > 0 && !this.isAnimatingOut && child.getVisibility() == View.VISIBLE) { 
      this.animateOut(child); 
     } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { 
      this.animateIn(child); 
     } 
    } 

    private void animateOut(final FloatingActionButton button) { 
     ViewCompat.animate(button).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(new ViewPropertyAnimatorListener() { 

      public void onAnimationStart(View view) { 
       ScrollFABBehaviour.this.isAnimatingOut = true; 
      } 

      public void onAnimationCancel(View view) { 
       ScrollFABBehaviour.this.isAnimatingOut = false; 
      } 

      public void onAnimationEnd(View view) { 
       ScrollFABBehaviour.this.isAnimatingOut = false; 

       view.setVisibility(View.GONE); 
      } 
     }).start(); 
    } 

    private void animateIn(FloatingActionButton button) { 
     button.setVisibility(View.VISIBLE); 

     ViewCompat.animate(button).scaleX(1.0F).scaleY(1.0F).alpha(1.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(null).start(); 
    } 
} 

Leider kann ich nicht diesen Code auf den FloatingActionMenu verwenden, weil es keine FloatingActionButton ist und auch nicht erben wird. Es ist ein Brauch ViewGroup. Wie kann ich ein solches Verhalten für die FloatingActionMenu erstellen?

Antwort

4

Dieses Verhalten wird durch Erstellen eines CoordinatorLayout.Behavior<View> erreicht. Anstatt also von FloatingActionButton.Behavior zu verlängern, erweitern Sie sich direkt von CoordinatorLayout.Behavior<FloatingActionMenu>.

Hier ist der entsprechende Code, der mit der FloatingActionMenu funktioniert.

public class ScrollFAMBehaviour extends CoordinatorLayout.Behavior<FloatingActionMenu>{ 
    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator(); 
    private boolean isAnimatingOut = false; 

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

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

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

     if (dyConsumed > 0 && !this.isAnimatingOut && child.getVisibility() == View.VISIBLE) { 
      this.animateOut(child); 
     } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { 
      this.animateIn(child); 
     } 
    } 

    private void animateOut(final FloatingActionMenu menu) { 
     ViewCompat.animate(menu).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(new ViewPropertyAnimatorListener() { 

      public void onAnimationStart(View view) { 
       ScrollFAMBehaviour.this.isAnimatingOut = true; 
      } 

      public void onAnimationCancel(View view) { 
       ScrollFAMBehaviour.this.isAnimatingOut = false; 
      } 

      public void onAnimationEnd(View view) { 
       ScrollFAMBehaviour.this.isAnimatingOut = false; 

       view.setVisibility(View.GONE); 
      } 
     }).start(); 
    } 

    private void animateIn(FloatingActionMenu menu) { 
     menu.setVisibility(View.VISIBLE); 

     ViewCompat.animate(menu).scaleX(1.0F).scaleY(1.0F).alpha(1.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(null).start(); 
    } 
} 
+1

perfekt. Vielen Dank. – Mulgard

Verwandte Themen