2015-07-28 3 views
0

Je SnackBar ai un (Bouton d'action flottant) FABcomportement Android FAB personnalisé perd le comportement de réglage par défaut

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/coordinatorLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

... 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fabButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|right" 
    android:layout_margin="@dimen/fab_margin" 
    app:layout_anchorGravity="bottom|right|end" 
    android:src="@drawable/img" 
    app:borderWidth="0dp" 
    /> 

Ce FAB existe sur une RecycleView que je faire défiler. À un certain stade du défilement, je montre SnackBar et tout fonctionne comme annoncé (en particulier, le SnackBar déplace le FAB pour éviter le blocage.)

Maintenant, je veux mettre en œuvre un comportement personnalisé, celui qui défile vers le bas, je veux cacher FAB.

J'ai donc créé un comportement personnalisé:

public class CustomBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> { 
private int toolbarHeight; 

public CustomBehavior(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.toolbarHeight = Utils.getToolbarHeight(context); 
} 

@Override 
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) { 
    return dependency instanceof AppBarLayout; 
} 

@Override 
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) { 
    if (dependency instanceof AppBarLayout) { 
     CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); 
     int fabBottomMargin = lp.bottomMargin; 
     int distanceToScroll = fab.getHeight() + fabBottomMargin; 
     float ratio = (float)dependency.getY()/(float)toolbarHeight; 
     fab.setTranslationY(-distanceToScroll * ratio); 
    } 
    return true; 
} 

}

ce peu près cache mon FAB quand je défiler vers le bas (il se cache avec AppBarLayout J'ajoute donc à mon FAB xm l déclaration

app:layout_behavior="com.example.CustomBehavior" 

Lorsque je fais cela, le FAB se cache correctement, mais le SnackBar se chevauche quand il montre. Ce qui signifie que le comportement par défaut est parti ...

Est-il possible d'avoir les deux?

+0

Vous devez implémenter les deux comportements dans 'CustomBehavior'. Ce post devrait vous donner les détails d'implémentation pour répliquer le comportement par défaut: https://lab.getbase.com/introduction-to-coordinator-layout-on-android/. – siger

Répondre

0

Vous devez conserver une instance locale de comportement et conserver le comportement par défaut. essayez de rétablir le comportement par défaut lorsque snackbar est fermé, puis votre comportement personnalisé lorsqu'il est affiché. quelque chose comme:

mSnackbar.setCallback(new Snackbar.Callback() { 
      @Override 
      public void onDismissed(Snackbar snackbar, int event) { 
       super.onDismissed(snackbar, event); 
       ((CoordinatorLayout.LayoutParams)mSnackbar.getView().getLayoutParams()).setBehavior(/*default behavior*/); 
      } 

      @Override 
      public void onShown(Snackbar snackbar) { 
       super.onShown(snackbar); 
       ((CoordinatorLayout.LayoutParams)mSnackbar.getView().getLayoutParams()).setBehavior(/*your custom behavior*/); 

      } 
     }); 

bonne chance!