onTouchListener warning: onTouch should call View#performClick when a click is detected

前端 未结 5 1737
长情又很酷
长情又很酷 2020-12-23 15:48

I have created a onTouchListener. Unfortunately onTouch() method throws me a warning:

com/calculator/activitys/Calculat         


        
5条回答
  •  囚心锁ツ
    2020-12-23 16:23

    I had a similar issue with a MultiTouchListener and solved it implementing a GestureDetector and listening for a SingleTap (This does not remove the warning but starts to triggering onClick events on my view)

    class TouchListener(context: Context) : MultiTouchListener() {
    
        private var tochedView: View? = null
        private var mGestureDetector = CustomGestureDetector()
        private var gestureDetector: GestureDetector = GestureDetector(context, mGestureDetector)
    
        @SuppressLint("ClickableViewAccessibility")
        override fun onTouch(view: View?, event: MotionEvent?): Boolean {
            val aux = super.onTouch(view, event)
    
            tochedView = view
            gestureDetector.onTouchEvent(event)
    
            return aux
        }
    
        private inner class CustomGestureDetector: GestureDetector.SimpleOnGestureListener() {
    
            override fun onSingleTapUp(e: MotionEvent?): Boolean {
                // this will be called even when a double tap is
                tochedView?.performClick()
                return super.onSingleTapUp(e)
            }
    
            override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
                // this will only be called after the detector is confident that the
                // user's first tap is not followed by a second tap leading to a double-tap gesture.
                tochedView?.performClick()
                return super.onSingleTapConfirmed(e)
            }
    
        }
    
    }
    

提交回复
热议问题