public final class

GestureDetectorCompat

extends java.lang.Object

 java.lang.Object

↳androidx.core.view.GestureDetectorCompat

Gradle dependencies

compile group: 'androidx.core', name: 'core', version: '1.15.0-alpha02'

  • groupId: androidx.core
  • artifactId: core
  • version: 1.15.0-alpha02

Artifact androidx.core:core:1.15.0-alpha02 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.core:core com.android.support:support-compat

Androidx class mapping:

androidx.core.view.GestureDetectorCompat android.support.v4.view.GestureDetectorCompat

Overview

Detects various gestures and events using the supplied MotionEvents. The 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:

Summary

Constructors
publicGestureDetectorCompat(Context context, OnGestureListener listener)

Creates a GestureDetectorCompat with the supplied listener.

publicGestureDetectorCompat(Context context, OnGestureListener listener, Handler handler)

Creates a GestureDetectorCompat with the supplied listener.

Methods
public booleanisLongpressEnabled()

public booleanonTouchEvent(MotionEvent event)

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

public voidsetIsLongpressEnabled(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.

public voidsetOnDoubleTapListener(OnDoubleTapListener listener)

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

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public GestureDetectorCompat(Context context, OnGestureListener listener)

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

Parameters:

context: the application's context
listener: the listener invoked for all the callbacks, this must not be null.

See also: android.os.Handler

public GestureDetectorCompat(Context context, OnGestureListener listener, Handler handler)

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

Parameters:

context: the application's context
listener: the listener invoked for all the callbacks, this must not be null.
handler: the handler that will be used for posting deferred messages

See also: android.os.Handler

Methods

public boolean isLongpressEnabled()

Returns:

true if longpress is enabled, else false.

public boolean onTouchEvent(MotionEvent event)

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

Parameters:

event: The current motion event.

Returns:

true if the consumed the event, else false.

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:

enabled: whether longpress should be enabled.

public void setOnDoubleTapListener(OnDoubleTapListener listener)

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

Parameters:

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

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.core.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Detects various gestures and events using the supplied {@link MotionEvent}s.
 * The {@link OnGestureListener} callback will notify users when a particular
 * motion event has occurred. This class should only be used with {@link MotionEvent}s
 * reported via touch (don't use for trackball events).
 *
 * <p>This compatibility implementation of the framework's GestureDetector guarantees
 * the newer focal point scrolling behavior from Jellybean MR1 on all platform versions.</p>
 *
 * To use this class:
 * <ul>
 *  <li>Create an instance of the {@code GestureDetectorCompat} for your {@link View}
 *  <li>In the {@link View#onTouchEvent(MotionEvent)} method ensure you call
 *          {@link #onTouchEvent(MotionEvent)}. The methods defined in your callback
 *          will be executed when the events occur.
 * </ul>
 *
 * @deprecated Use {@link android.view.GestureDetector} directly
 */
@Deprecated
public final class GestureDetectorCompat {
    private final GestureDetector mDetector;

    /**
     * Creates a GestureDetectorCompat with the supplied listener.
     * As usual, you may only use this constructor from a UI thread.
     * @see android.os.Handler#Handler()
     *
     * @param context the application's context
     * @param listener the listener invoked for all the callbacks, this must
     * not be null.
     */
    public GestureDetectorCompat(@NonNull Context context, @NonNull OnGestureListener listener) {
        this(context, listener, null);
    }

    /**
     * Creates a GestureDetectorCompat with the supplied listener.
     * As usual, you may only use this constructor from a UI thread.
     * @see android.os.Handler#Handler()
     *
     * @param context the application's context
     * @param listener the listener invoked for all the callbacks, this must
     * not be null.
     * @param handler the handler that will be used for posting deferred messages
     */
    public GestureDetectorCompat(@NonNull Context context, @NonNull OnGestureListener listener,
            @Nullable Handler handler) {
        mDetector = new GestureDetector(context, listener, handler);
    }

    /**
     * @return true if longpress is enabled, else false.
     */
    public boolean isLongpressEnabled() {
        return mDetector.isLongpressEnabled();
    }

    /**
     * Analyzes the given motion event and if applicable triggers the
     * appropriate callbacks on the {@link OnGestureListener} supplied.
     *
     * @param event The current motion event.
     * @return true if the {@link OnGestureListener} consumed the event,
     *              else false.
     */
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }

    /**
     * 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.
     *
     * @param enabled whether longpress should be enabled.
     */
    @SuppressLint("KotlinPropertyAccess")
    public void setIsLongpressEnabled(boolean enabled) {
        mDetector.setIsLongpressEnabled(enabled);
    }

    /**
     * Sets the listener which will be called for double-tap and related
     * gestures.
     *
     * @param listener the listener invoked for all the callbacks, or
     *        null to stop listening for double-tap gestures.
     */
    public void setOnDoubleTapListener(@Nullable OnDoubleTapListener listener) {
        mDetector.setOnDoubleTapListener(listener);
    }
}