简体   繁体   English

显示SnackBar时,按钮必须充当FAB

[英]Button has to act as FAB when the SnackBar is shown

What I want to achieve is to have a Button container in a CoordinatorLayout 我要实现的是在CoordinatorLayout有一个Button容器

android.support.design.widget.CoordinatorLayout
android.widget.button

that reacts to the SnackBar like the FloatingActionButton . 对SnackBar的反应类似于FloatingActionButton
That one uses a FloatingActionButton . 那个使用FloatingActionButton
Behavior which extends CoordinatorLayout.Behavior<FloatingActionButton> . 扩展CoordinatorLayout.Behavior<FloatingActionButton>行为。

Do I have to define a custom behaviour myself or are there any generic components that react the same way? 我是否必须自己定义自定义行为,或者是否有任何通用组件以相同的方式做出反应?

You can define your own layout and a custom CoordinatorLayout.Behavior 您可以定义自己的布局和自定义CoordinatorLayout.Behavior
You can clone the FloatingActionButton structure. 您可以克隆FloatingActionButton结构。

Something like: 就像是:

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

       }

    }

You should make your own custom CoordinatorLayout.Behavior 您应该制作自己的自定义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
    }
}

and you have to assign that behaviour to any direct child view of the CoordinatorLayout by using one of the below two methods 并且您必须使用以下两种方法之一将该行为分配给CoordinatorLayout的任何直接子视图

1- By using the layout_behavior custom CoordinatorLayout attribute 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 - Or you can also assign the behaviour to your own custom view by implementing CoordinatorLayout.AttachedBehavior interface 2-或者您也可以通过实现CoordinatorLayout.AttachedBehavior接口将行为分配给自己的自定义视图

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM