Move cardview on Snackbar - Co-ordinator layout

后端 未结 3 1434
自闭症患者
自闭症患者 2021-01-16 11:35

I have a custom textview in a cardview at the bottom of the screen and using snackbar to display error messages on logging in. Now when the snackbar shows, the sign up textv

3条回答
  •  天涯浪人
    2021-01-16 11:39

    You need to implement a layout behavior for your View and reference it from your layout xml file.

    It is simple to implement your own layout behavior. In your case, you only need to set the translation y of your view when the snackbar appears.

    public class YourCustomBehavior extends CoordinatorLayout.Behavior {
    
        public YourCustomBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
            float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
            child.setTranslationY(translationY);
            return true;
        }
    
        @Override
        public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
            // we only want to trigger the change 
            // only when the changes is from a snackbar
            return dependency instanceof Snackbar.SnackbarLayout;
        }
    }
    

    And add it to your layout xml like this

    
    

    The value for the app:layout_behavior attribute should be the full qualified name of the behavior class.

    You can also refer to this article which explains everything nicely.

提交回复
热议问题