Receiving onTouch and onClick events with Android

后端 未结 7 1048
滥情空心
滥情空心 2020-12-16 04:02

I have a view that need to process onTouch gestures and onClick events. What is the proper way to achieve this?

I have an onTouchListener and

7条回答
  •  清歌不尽
    2020-12-16 04:48

    In you GestureDetector, you can call callOnClick() directly. Note the View.callOnClick API requires API level 15. Just have a try.

     // Create a Gesturedetector
    GestureDetector mGestureDetector = new GestureDetector(context, new MyGestureDetector());
    
    // Add a OnTouchListener into view
    m_myViewer.setOnTouchListener(new OnTouchListener()
    {
    
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            return mGestureDetector.onTouchEvent(event);
        }
    });
    
    private class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
    {
        public boolean onSingleTapUp(MotionEvent e) {
            // ---Call it directly---
            callOnClick();
            return false;
        }
    
        public void onLongPress(MotionEvent e) {
        }
    
        public boolean onDoubleTap(MotionEvent e) {
            return false;
        }
    
        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }
    
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return false;
    
        }
    
        public void onShowPress(MotionEvent e) {
            LogUtil.d(TAG, "onShowPress");
        }
    
        public boolean onDown(MotionEvent e) {            
            // Must return true to get matching events for this down event.
            return true;
        }
    
        public boolean onScroll(MotionEvent e1, MotionEvent e2, final float distanceX, float distanceY) {
            return super.onScroll(e1, e2, distanceX, distanceY);
        }        
    
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            // do something
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }
    

提交回复
热议问题