Skip to content

Commit 8ac1c21

Browse files
authored
Fix context menu popping up issue on Android while the list is scrolled in the bottom sheet (#123)
1 parent 0b59bce commit 8ac1c21

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

android/src/main/java/com/mpiannucci/reactnativecontextmenu/ContextMenuView.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class ContextMenuView extends ReactViewGroup implements View.OnCreateCont
3737

3838
boolean cancelled = true;
3939

40+
int[] longPressStartLocation = new int[2];
41+
4042
protected boolean dropdownMenuMode = false;
4143

4244
protected boolean disabled = false;
@@ -61,12 +63,32 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
6163

6264
@Override
6365
public void onLongPress(MotionEvent e) {
66+
int[] location = new int[2];
67+
getLocationOnScreen(location);
68+
69+
int dx = location[0] - longPressStartLocation[0];
70+
int dy = location[1] - longPressStartLocation[1];
71+
double distance = Math.sqrt(dx * dx + dy * dy);
72+
// Cancel long press if the user moves their finger more than 10 pixels
73+
// (e.g. the context menu is used inside the scrollable component and it
74+
// moves as the user scrolls)
75+
if (distance > 10) {
76+
cancelled = true;
77+
return;
78+
}
79+
6480
if (!dropdownMenuMode && !disabled) {
6581
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
6682
showContextMenu(e.getX(), e.getY());
6783
}
6884
}
6985
}
86+
87+
@Override
88+
public boolean onDown(MotionEvent e) {
89+
getLocationOnScreen(longPressStartLocation);
90+
return super.onDown(e);
91+
}
7092
});
7193
}
7294

0 commit comments

Comments
 (0)