Shared OnTouchListener Issue in Custom Android Button View

34 views Asked by At

I made my custom button view in android and made it a library. I am making an animation with setontouchlistener in this library. However, when I use this listener in an activity that is specifically implemented in a project from elsewhere, the animation does not work.

So the setontouch listener on the source page is being overridden. How do I prevent this?

From Custom View Class (Inherited FrameLayout)

@SuppressLint("ClickableViewAccessibility")
    private fun initListeners() {
        this.setOnTouchListener { _, motionEvent: MotionEvent ->
            if (toScale != 1.0F || toAlpha != 1.0F) {
                when (motionEvent.action) {
                    MotionEvent.ACTION_DOWN -> {
                        if (toScale != 1.0F)
                            scaleAnimation(
                                controller = true
                            )
                        if (toAlpha != 1.0F)
                            alphaAnimation(controller = true)
                    }

                    MotionEvent.ACTION_UP -> {
                        if (toScale != 1.0F)
                            scaleAnimation(
                                controller = false
                            )
                        if (toAlpha != 1.0F)
                            alphaAnimation(controller = false)
                    }

                    MotionEvent.ACTION_CANCEL -> {
                        if (toScale != 1.0F)
                            scaleAnimation(
                                controller = false
                            )
                        if (toAlpha != 1.0F)
                            alphaAnimation(controller = false)
                    }
                }
            }

            return@setOnTouchListener false
        }

        this.setOnClickListener { /* NO-OP */ }
    }

From Activity (Implemented Project Page)

flexi.setOnClickListener {
            Log.i("FlexiButton", "Clicked!") // It works properly. Overriding clickListener in custom view. However this is not a problem for me.
}
        
flexi.setOnTouchListener { _, _ -> return@setOnTouchListener false }

I tried testing the return values ​​of the touch and click listeners. However, it didn't work. My expectation is that the touch listener in the custom view is not overridden. The click listener is being overridden and that works for me anyway. That's why I don't have a problem with the click listener.

0

There are 0 answers