4

I am creating a custom component in HarmonyOS using Java SDK, Where I have to perform some task on double-tap. But I am not able to detect double-tap event.

In Android, with the help of GestureDetector class we can detect double-tap event as follow:

GestureDetector gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                return super.onSingleTapConfirmed(e);
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                return super.onDoubleTap(e);
            }
        });

In HMOS, I have tried to detect double-tap using Component.TouchEventListener as follow:

Component.TouchEventListener touchEventListener = new TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                if(touchEvent.getPointerCount() == 1) {
                    LogUtil.info(TAG, "single click detected");
                }

                if(touchEvent.getPointerCount() == 2) {
                    LogUtil.info(TAG, "double click detected");
                }
                return false;
            }
        };

but, It has not detected double-tap. On double-tap also it has printed single click detected.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Shivam Jamaiwar
  • 524
  • 1
  • 4
  • 14
  • 2
    the answer posted by Gowtham must work because that's the direct callback. Between just FYI - getPointerCount() returns the number of touche pointers for the same event. For example -You can use it to find how many fingers are used for the same touch. – Kanak Sony Sep 07 '21 at 11:01

2 Answers2

2

try registering Component.DoubleClickedListener and you will be able to detect Double Tap in Custom Component, sample Usage

    customComponent.setDoubleClickedListener(new Component.DoubleClickedListener() {
                @Override
                public void onDoubleClick(Component component) {
                    LogUtil.info(TAG, "double click detected");
                }
            });
Gowtham GS
  • 478
  • 2
  • 5
1

Supports component double-clicking in SDK API Version 5 or later.

As@Gowtham GS mentioned, sample usage is as follows:

customComponent.setDoubleClickedListener(new Component.DoubleClickedListener() {                
                @Override
                public void onDoubleClick(Component component) {
                    LogUtil.info(TAG, "double click detected");
                }
            });
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108