Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 437
Shivam Jamaiwar Avatar asked Sep 07 '21 10:09

Shivam Jamaiwar


2 Answers

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");
                }
            });
like image 115
Gowtham GS Avatar answered Sep 30 '22 15:09

Gowtham GS


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");
                }
            });
like image 30
zhangxaochen Avatar answered Sep 30 '22 16:09

zhangxaochen