javaandroidhuawei-mobile-serviceshuawei-developersharmonyos

How to detect double-tap in custom component in HarmonyOS?


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.


Solution

  • 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");
                    }
                });