Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context menu popping up in lists on Android #123

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
});
}

Expand Down