1

Lange Rede, kurzer Sinn, können Sie den floatingActionButton nach oben und unten bewegen, wenn eine benutzerdefinierte Ansicht auf die gleiche Weise wie bei der Snackbar verschoben wird?Verwenden des Koordinatorlayouts zum Verschieben der schwebenden Aktionsschaltfläche, wenn ein benutzerdefiniertes Popup animiert wird

Ich habe einen Floating Action Button in einem Koordinatorlayout, das sich nach oben bewegt, wenn die Snackbar angezeigt wird. Ich habe es sogar geschafft, die FABs "Behaviour" durch Tutorials so anzupassen, dass es schrumpft und wächst, anstatt sich auf und ab zu bewegen.

Ich habe auch ein benutzerdefiniertes Popup (LinearLayout), das von unten nach oben und wieder zurück animiert, wenn eine Taste gedrückt wird und die gerade hinter der schwebenden Aktionsschaltfläche erscheint, die sich nicht bewegt. Beide sind in einem CoordinatorLayout Ich möchte, dass die Floating-Action-Taste hoch und runter geht, wenn das benutzerdefinierte Popup auf und ab animiert ... genauso wie es für die Snackbar ist.

Ich habe es irgendwie geschafft, es zu arbeiten, indem ich mein eigenes Popup als Abhängigkeit von Aufrufen der layoutDependsOn-Methode der FloatingActionButton-Verhaltensklasse hinzufügte. Meine Implementierung für die onDependentViewChanged-Methode, um den FAB tatsächlich animieren zu lassen, fühlt sich einfach falsch an, da ich den floatingActionButton effektiv mit einer koordinierten Animation animieren muss, um der angepassten Popup-Animation zu entsprechen.

Kann mir jemand zeigen, wie man das besser und einfacher macht?

Ich werde versuchen, sinnvollen Code-Schnipsel zu bieten ....

benutzerdefinierte: Pop-up

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="bottom" 
android:id="@+id/selected_item_popup_linear_layout" 
android:elevation="4dp" 
android:visibility="gone" 
> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/selectedItemPopupBackground" android:elevation="4dp" 
    android:id="@+id/selected_item_popup_content"> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/selected_item_popup_delete_button" 
     android:layout_gravity="center" 
     android:padding="14dp" 
     android:src="@drawable/ic_delete_black_24dp" 
     android:background="@color/selectedItemPopupBackground" 
     android:tint="@color/selectedItemPopupDeleteTint" 
     /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/selected_item_popup_edit_button" 
     android:layout_gravity="center" 
     android:padding="14dp" 
     android:src="@drawable/ic_mode_edit_black_24dp" 
     android:background="@color/selectedItemPopupBackground" 
     android:tint="@color/selectedItemPopupEditTint"/> 

</LinearLayout> 

Floating Action-Taste:

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/floating_action_button_add_item" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_add_white_24dp" 
    android:visibility="visible" 
    android:layout_marginBottom="@dimen/fab_margin_bottom" 
    android:layout_marginRight="@dimen/fab_margin_right" 
    app:layout_anchor="@id/pager" 
    app:layout_anchorGravity="bottom|right|end" 
    app:layout_behavior=".FloatingActionButtonBehaviour" 
    app:rippleColor="@color/boardItemBackgroundSelected"/> 

Floating Action Button-Verhalten:

public class FloatingActionButtonBehaviour extends CoordinatorLayout.Behavior<FloatingActionButton> { 

    private Context mContext; 

    public FloatingActionButtonBehaviour(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mContext = context; 
    } 

    @Override 
    public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) { 

     if (dependency.equals(parent.findViewById(R.id.selected_item_popup_linear_layout))) { 
      return true; 
     } 

     if (dependency instanceof Snackbar.SnackbarLayout) { 
      return true; 
     } 

     return false; 
    } 


    @Override 
    public boolean onDependentViewChanged(CoordinatorLayout parent, final FloatingActionButton child, View dependency) { 

     if(dependency instanceof Snackbar.SnackbarLayout) { 

      float translationY = getFabTranslationYForSnackbar(parent, child); 
      float percentComplete = -translationY/dependency.getHeight(); 
      float scaleFactor = 1 - percentComplete; 

      child.setScaleX(scaleFactor); 
      child.setScaleY(scaleFactor); 
     } 

     if (dependency.equals(parent.findViewById(R.id.selected_item_popup_linear_layout))) { 

      View contentView = dependency.findViewById(R.id.selected_item_popup_content); 
      int contentViewHeight = contentView.getHeight(); 

      if (dependency.getVisibility() == View.VISIBLE) { 

       Animation bottomUp = AnimationUtils.loadAnimation(mContext, R.anim.fab_move_up); 
       child.startAnimation(bottomUp); 
       child.setVisibility(View.VISIBLE); 
       float translationY = Math.min(0, dependency.getTranslationY() - contentViewHeight); 
       child.setTranslationY(translationY); 

      } else { 

       Animation bottomDown = AnimationUtils.loadAnimation(mContext, R.anim.fab_move_down); 
       child.startAnimation(bottomDown); 
       child.setVisibility(View.VISIBLE); 
       child.setTranslationY(dependency.getTranslationY()); 
      } 

     } 

     return true; 

    } 

    private float getFabTranslationYForSnackbar(CoordinatorLayout parent, FloatingActionButton fab) { 
     float minOffset = 0; 
     final List<View> dependencies = parent.getDependencies(fab); 
     for (int i = 0, z = dependencies.size(); i < z; i++) { 
      final View view = dependencies.get(i); 
      if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(fab, view)) { 
       minOffset = Math.min(minOffset, ViewCompat.getTranslationY(view) - view.getHeight()); 
      } 
     } 

     return minOffset; 
    } 
} 

Code Animieren die benutzerdefinierte: Pop-up

 Animation bottomUp = AnimationUtils.loadAnimation(mActivity, R.anim.bottom_up); 
    final ViewGroup selectedItemPopupLinearLayout = (ViewGroup)mActivity.findViewById(R.id.selected_item_popup_linear_layout); 
    selectedItemPopupLinearLayout.startAnimation(bottomUp); 
    selectedItemPopupLinearLayout.setVisibility(View.VISIBLE); 

Antwort