GestureDetectorCompat

Added in 1.1.0

public final class GestureDetectorCompat


Detects various gestures and events using the supplied MotionEvents. The OnGestureListener callback will notify users when a particular motion event has occurred. This class should only be used with MotionEvents reported via touch (don't use for trackball events).

This compatibility implementation of the framework's GestureDetector guarantees the newer focal point scrolling behavior from Jellybean MR1 on all platform versions.

To use this class:
  • Create an instance of the GestureDetectorCompat for your View
  • In the onTouchEvent method ensure you call onTouchEvent. The methods defined in your callback will be executed when the events occur.

Summary

Public constructors

Creates a GestureDetectorCompat with the supplied listener.

GestureDetectorCompat(
    @NonNull Context context,
    @NonNull GestureDetector.OnGestureListener listener,
    @Nullable Handler handler
)

Creates a GestureDetectorCompat with the supplied listener.

Public methods

boolean
boolean

Analyzes the given motion event and if applicable triggers the appropriate callbacks on the OnGestureListener supplied.

void
setIsLongpressEnabled(boolean enabled)

Set whether longpress is enabled, if this is enabled when a user presses and holds down you get a longpress event and nothing further.

void

Sets the listener which will be called for double-tap and related gestures.

Public constructors

GestureDetectorCompat

Added in 1.1.0
public GestureDetectorCompat(
    @NonNull Context context,
    @NonNull GestureDetector.OnGestureListener listener
)

Creates a GestureDetectorCompat with the supplied listener. As usual, you may only use this constructor from a UI thread.

Parameters
@NonNull Context context

the application's context

@NonNull GestureDetector.OnGestureListener listener

the listener invoked for all the callbacks, this must not be null.

See also
Handler

GestureDetectorCompat

Added in 1.1.0
public GestureDetectorCompat(
    @NonNull Context context,
    @NonNull GestureDetector.OnGestureListener listener,
    @Nullable Handler handler
)

Creates a GestureDetectorCompat with the supplied listener. As usual, you may only use this constructor from a UI thread.

Parameters
@NonNull Context context

the application's context

@NonNull GestureDetector.OnGestureListener listener

the listener invoked for all the callbacks, this must not be null.

@Nullable Handler handler

the handler that will be used for posting deferred messages

See also
Handler

Public methods

isLongpressEnabled

Added in 1.1.0
public boolean isLongpressEnabled()
Returns
boolean

true if longpress is enabled, else false.

onTouchEvent

Added in 1.1.0
public boolean onTouchEvent(@NonNull MotionEvent event)

Analyzes the given motion event and if applicable triggers the appropriate callbacks on the OnGestureListener supplied.

Parameters
@NonNull MotionEvent event

The current motion event.

Returns
boolean

true if the OnGestureListener consumed the event, else false.

setIsLongpressEnabled

Added in 1.1.0
public void setIsLongpressEnabled(boolean enabled)

Set whether longpress is enabled, if this is enabled when a user presses and holds down you get a longpress event and nothing further. If it's disabled the user can press and hold down and then later moved their finger and you will get scroll events. By default longpress is enabled.

Parameters
boolean enabled

whether longpress should be enabled.

setOnDoubleTapListener

Added in 1.1.0
public void setOnDoubleTapListener(
    @Nullable GestureDetector.OnDoubleTapListener listener
)

Sets the listener which will be called for double-tap and related gestures.

Parameters
@Nullable GestureDetector.OnDoubleTapListener listener

the listener invoked for all the callbacks, or null to stop listening for double-tap gestures.