FloatingActionButton does not come down when dismissing Snackbar

后端 未结 3 1974
Happy的楠姐
Happy的楠姐 2020-12-31 10:49

I am trying to use a Snackbar. I have a FloatingActionButton wrapped in a CoordinatorLayout. When the Snackbar shows, the

3条回答
  •  孤城傲影
    2020-12-31 11:13

    To solve this issue I defined a RelativeLayout Behavior like this. This can be done for any view, just replace all RelativeLayout with desired UI element.

    public class RelativeLayoutBehavior extends CoordinatorLayout.Behavior {
    
        public RelativeLayoutBehavior(Context context, AttributeSet attrs) {
    
        }
    
        @Override
        public boolean layoutDependsOn(CoordinatorLayout parent, RelativeLayout child, View dependency) {
            return dependency instanceof Snackbar.SnackbarLayout;
        }
    
        @Override
        public boolean onDependentViewChanged(CoordinatorLayout parent, RelativeLayout child, final View dependency) {
            Float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
            child.setTranslationY(translationY);
            return true;
        }
    
        @Override
        public void onDependentViewRemoved(CoordinatorLayout parent, RelativeLayout child, View dependency) {
            child.setTranslationY(0);
        }
    }
    

    Then you need to add the Behavior into the RelativeLayout "android:layout_behavior" property like this. Make sure the RelativeLayout you want to slide up is inside of a CoordinateLayout also.

    
    
    
        
    
            
    
            
    
        
    
    
    

提交回复
热议问题