How to move a view above Snackbar just like FloatingButton

前端 未结 8 548
伪装坚强ぢ
伪装坚强ぢ 2021-01-31 07:50

I got a linear layout that I want to move up when a Snackbar appears.

I saw many examples how to do this with FloatingButton, but what about a regular view?

8条回答
  •  北恋
    北恋 (楼主)
    2021-01-31 08:19

    I implemented this and found when the snackbar disappeared the view remained up with white space in the snackbars place, apparently this is known if animations have been disable on the device.

    To fix this I changed the onDependentViewChanged method to store the initial Y position of the view this behaviour is attached to. Then on removal of the snackbar reset the position of that view to the stored Y position

    private static float initialPositionY;
    
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        initialPositionY = child.getY();
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }
    
    @Override
    public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
        super.onDependentViewRemoved(parent, child, dependency);
        child.setTranslationY(initialPositionY);
    }
    

提交回复
热议问题