Search code examples
androidkotlinandroid-recyclerviewtouch-event

On Touch listener has a bad behavior for views while using recyclerview


I have a Recyclerview and a Custom view inside one RelativeLayout like that image

My custom view has On Touch listener for it with the following code

            override fun onTouch(view: View?, e: MotionEvent?): Boolean {

            when (e!!.action) {

                MotionEvent.ACTION_DOWN -> {
                    _isMouseCaptured = true
                    bubbleFR.visibility = VISIBLE
                    getTouchedItem(e.y)
                    return true
                }
                MotionEvent.ACTION_UP   -> {

                    _isMouseCaptured = false
                    hideBubble()
                }
                MotionEvent.ACTION_MOVE -> {

                    _isMouseCaptured = true
                    getTouchedItem(e.y)
                }
                //                    MotionEvent.ACTION_CANCEL -> {
                //                        
                //                    }
            }
            //  lineLL1.performClick()
            return true
        } 

But a non expecting behavior occurred like following:

  1. When I start mouse down on my custom view and then mouse move (like dragging) I got motionevent.canceled for my custom view and then The mouse move not fired and also mouse up not fired of my custom view
  2. When I start scrolling my recyclerview to a far position and during its scrolling operation I can then touch my custom view and then the mouse move and also mouse up fired on my custom view.

So. I can hold and capture mouse move for my custom view (like dragging) only when I started it while the recyclerview is scrolling, otherwise it is not working. It is like some other view steals the focus of my custom view I want to touch my custom view and move my finger on it normally like swiping my custom view.


Solution

  • I solved my problem by using drag drop technique.