2015-07-28 2 views
0

У меня есть FAB (Floating кнопки действий)Android FAB нестандартное поведение теряет по умолчанию закусочной поведение настройки

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

Этот FAB существует на вершине RecycleView что я прокручивать. На определенном этапе прокрутки я показываю SnackBar, и все работает так, как рекламируется (в частности, SnackBar сдвигает FAB, чтобы избежать блокировки.

Теперь я хочу реализовать пользовательское поведение, которое при прокрутке вниз я хочу скрыть FAB.

Так что я создал пользовательское поведение:

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; 
} 

}

Это в значительной степени скрывает мой FAB, когда я прокручиваю вниз (прячется вместе с AppBarLayout Так я добавляю это к моему FAB хт l декларация

app:layout_behavior="com.example.CustomBehavior" 

Когда я это делаю, FAB правильно скрывается, но SnackBar перекрывает его, когда он показывает. Значение по умолчанию исчезло ...

Возможно ли иметь и?

+0

Вам необходимо реализовать оба поведения в «CustomBehavior». Это сообщение должно предоставить вам сведения о реализации для репликации поведения по умолчанию: https://lab.getbase.com/introduction-to-coordinator-layout-on-android/. – siger

ответ

0

вам нужно сохранить локальный экземпляр поведения и придерживаться поведения по умолчанию. попробуйте установить поведение по умолчанию, когда snackbar будет уволен, а затем ваше пользовательское поведение, когда оно будет показано. что-то вроде:

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*/); 

      } 
     }); 

удачи!

Смежные вопросы