From 59ab63c70d85cb2faef96e38916cd3135d546a4b Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Sun, 8 Dec 2024 00:13:19 +0800 Subject: [PATCH] fix KeyboardAvoidingView infinite jump on android --- .../com/facebook/react/ReactRootView.java | 55 ++++++++----------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java index a42933c8a5afd1..e8b02a2c649be8 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java @@ -898,36 +898,27 @@ private void checkForKeyboardEvents() { if (keyboardIsVisible != mKeyboardIsVisible) { mKeyboardIsVisible = keyboardIsVisible; - if (keyboardIsVisible) { - Insets imeInsets = rootInsets.getInsets(WindowInsets.Type.ime()); - Insets barInsets = rootInsets.getInsets(WindowInsets.Type.systemBars()); - int height = imeInsets.bottom - barInsets.bottom; - - ViewGroup.LayoutParams rootLayoutParams = getRootView().getLayoutParams(); - Assertions.assertCondition(rootLayoutParams instanceof WindowManager.LayoutParams); - - int softInputMode = ((WindowManager.LayoutParams) rootLayoutParams).softInputMode; - int screenY = - softInputMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING - ? mVisibleViewArea.bottom - height - : mVisibleViewArea.bottom; - - sendEvent( - "keyboardDidShow", - createKeyboardEventPayload( - PixelUtil.toDIPFromPixel(screenY), - PixelUtil.toDIPFromPixel(mVisibleViewArea.left), - PixelUtil.toDIPFromPixel(mVisibleViewArea.width()), - PixelUtil.toDIPFromPixel(height))); - } else { - sendEvent( - "keyboardDidHide", - createKeyboardEventPayload( - PixelUtil.toDIPFromPixel(mVisibleViewArea.height()), - 0, - PixelUtil.toDIPFromPixel(mVisibleViewArea.width()), - 0)); - } + Insets imeInsets = rootInsets.getInsets(WindowInsets.Type.ime()); + Insets barInsets = rootInsets.getInsets(WindowInsets.Type.systemBars()); + int height = imeInsets.bottom - barInsets.bottom; + + ViewGroup.LayoutParams rootLayoutParams = getRootView().getLayoutParams(); + Assertions.assertCondition(rootLayoutParams instanceof WindowManager.LayoutParams); + + int softInputMode = ((WindowManager.LayoutParams) rootLayoutParams).softInputMode; + int screenY = + softInputMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING + ? mVisibleViewArea.bottom - height + : mVisibleViewArea.bottom; + + String eventName = keyboardIsVisible ? "keyboardDidShow" : "keyboardDidHide"; + sendEvent( + eventName, + createKeyboardEventPayload( + PixelUtil.toDIPFromPixel(screenY), + PixelUtil.toDIPFromPixel(mVisibleViewArea.left), + PixelUtil.toDIPFromPixel(mVisibleViewArea.width()), + PixelUtil.toDIPFromPixel(Math.max(height, 0)))); } } @@ -972,8 +963,8 @@ private void checkForKeyboardEventsLegacy() { sendEvent( "keyboardDidHide", createKeyboardEventPayload( - PixelUtil.toDIPFromPixel(mVisibleViewArea.height()), - 0, + PixelUtil.toDIPFromPixel(mVisibleViewArea.bottom), + PixelUtil.toDIPFromPixel(mVisibleViewArea.left), PixelUtil.toDIPFromPixel(mVisibleViewArea.width()), 0)); }