How to hide the BottomNavigationView below keyboard with adjustResize set

后端 未结 4 855
情歌与酒
情歌与酒 2020-12-13 09:00

According to the material design spec, when the keyboard appears, the BottomNavigationView should hide underneath it. However, if I set android:windowSoft

4条回答
  •  温柔的废话
    2020-12-13 09:17

    There is another solution, which doesn't require adjustSpan, but it works only for API >= 21. You can detect if keyboard is shown/hidden by tracking system insets. Say you have BottomNavigationView, which is child of LinearLayout and you need to hide it when keyboard is shown:

    > LinearLayout
      > ContentView
      > BottomNavigationView
    

    All you need to do is to extend LinearLayout in such way:

    public class KeyboardAwareLinearLayout extends LinearLayout {
        public KeyboardAwareLinearLayout(Context context) {
            super(context);
        }
    
        public KeyboardAwareLinearLayout(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public KeyboardAwareLinearLayout(Context context,
                                         @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        public KeyboardAwareLinearLayout(Context context, AttributeSet attrs,
                                         int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        @Override
        public WindowInsets onApplyWindowInsets(WindowInsets insets) {
            int childCount = getChildCount();
            for (int index = 0; index < childCount; index++) {
                View view = getChildAt(index);
                if (view instanceof BottomNavigationView) {
                    int bottom = insets.getSystemWindowInsetBottom();
                    if (bottom >= ViewUtils.dpToPx(200)) {
                        view.setVisibility(GONE);
                    } else {
                        view.setVisibility(VISIBLE);
                    }
                }
            }
            return insets;
        }
    }
    

    The idea is that when keyboard is shown, system insets are changed with pretty big .bottom value.

提交回复
热议问题