From 94728c71513d328188bcca2f0229936d931f3103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81opaci=C5=84ski?= Date: Mon, 24 Jun 2024 11:29:14 +0200 Subject: [PATCH] Fix context menu popping up issue on Android while the list is scrolled in the bottom sheet --- .../ContextMenuView.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/android/src/main/java/com/mpiannucci/reactnativecontextmenu/ContextMenuView.java b/android/src/main/java/com/mpiannucci/reactnativecontextmenu/ContextMenuView.java index 4d8e51a..c50f0de 100644 --- a/android/src/main/java/com/mpiannucci/reactnativecontextmenu/ContextMenuView.java +++ b/android/src/main/java/com/mpiannucci/reactnativecontextmenu/ContextMenuView.java @@ -37,6 +37,8 @@ public class ContextMenuView extends ReactViewGroup implements View.OnCreateCont boolean cancelled = true; + int[] longPressStartLocation = new int[2]; + protected boolean dropdownMenuMode = false; protected boolean disabled = false; @@ -61,12 +63,32 @@ public boolean onSingleTapConfirmed(MotionEvent e) { @Override public void onLongPress(MotionEvent e) { + int[] location = new int[2]; + getLocationOnScreen(location); + + int dx = location[0] - longPressStartLocation[0]; + int dy = location[1] - longPressStartLocation[1]; + double distance = Math.sqrt(dx * dx + dy * dy); + // Cancel long press if the user moves their finger more than 10 pixels + // (e.g. the context menu is used inside the scrollable component and it + // moves as the user scrolls) + if (distance > 10) { + cancelled = true; + return; + } + if (!dropdownMenuMode && !disabled) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { showContextMenu(e.getX(), e.getY()); } } } + + @Override + public boolean onDown(MotionEvent e) { + getLocationOnScreen(longPressStartLocation); + return super.onDown(e); + } }); }