当显示SnackBar时,Button必须充当FAB

时间:2016-01-13 15:34:59

标签: android material-design android-design-library android-coordinatorlayout floating-action-button

我想要实现的是Button

中有一个CoordinatorLayout容器
android.support.design.widget.CoordinatorLayout
android.widget.button

FloatingActionButton一样对SnackBar作出反应 那个使用FloatingActionButton 扩展CoordinatorLayout.Behavior<FloatingActionButton>的行为。

我是否必须自己定义自定义行为,或者是否存在任何反应相同的通用组件?

2 个答案:

答案 0 :(得分:1)

您可以定义自己的布局和自定义CoordinatorLayout.Behavior
您可以克隆FloatingActionButton结构。

类似的东西:

    @CoordinatorLayout.DefaultBehavior(ButtonLayout.Behavior.class)
    public class ButtonLayout extends LinearLayout {


       public static class Behavior extends 
            android.support.design.widget.CoordinatorLayout.Behavior<ButtonLayout> {

            public boolean layoutDependsOn(CoordinatorLayout parent, ButtonLayout child, View dependency) {
               return dependency instanceof Snackbar.SnackbarLayout;
            }

            public boolean onDependentViewChanged(CoordinatorLayout parent, ButtonLayout child, View dependency) {
               if(dependency instanceof Snackbar.SnackbarLayout) {
                  this.updateFabTranslationForSnackbar(parent, child, dependency);
               } 
               return false;
            }

            private void updateFabTranslationForSnackbar(CoordinatorLayout parent, ButtonLayout fab, View snackbar) {
               //copy from FloatingActionButton.Behavior
            }

       }

    }

答案 1 :(得分:0)

您应该自定义CoordinatorLayout.Behavior

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.snackbar.Snackbar

class SnackbarAwareBehaviour(context: Context, attrs: AttributeSet)
    : CoordinatorLayout.Behavior<View>(context, attrs) {

    override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
        return dependency is Snackbar.SnackbarLayout
    }

    override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
        child.translationY = Math.min(0f, dependency.translationY - dependency.height)
        return true
    }

    override fun onDependentViewRemoved(parent: CoordinatorLayout, child: View, dependency: View) {
        child.translationY = 0f
    }
}

,您必须使用以下两种方法之一将该行为分配给CoordinatorLayout的任何直接子视图

1-通过使用layout_behavior自定义CoordinatorLayout属性

<androidx.coordinatorlayout.widget.CoordinatorLayout
     ...>

    ...

    <!-- The view which has the Behaviour should be a direct child to the CoordinatorLayout -->
    <Button
       ...
       app:layout_behavior="my.package.SnackbarAwareBehaviour"
       />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

2-或者,您也可以通过实现CoordinatorLayout.AttachedBehavior界面将行为分配给自己的自定义视图

class MyButton(context: Context, attrs: AttributeSet?)
    : Button(context, attrs), CoordinatorLayout.AttachedBehavior {
   ...
   override fun getBehavior(): CoordinatorLayout.Behavior<*> {
      return SnackbarAwareBehaviour(context, null)
   }
}