简体   繁体   中英

OnTouchListener doesn't record ACTION_UP when navigating to another fragment

While holding down button with one finger and I navigate to another Fragment with a different finger, MotionEvent.ACTION_UP doesn't fire after lifting my finger in the next fragment or at all.

view.findViewById<Button>(R.id.button).setOnTouchListener(OnTouchListener { _, event ->
    when (event.action) {
        MotionEvent.ACTION_DOWN -> {             // PRESSED
            doAction(true)
            return@OnTouchListener true
        }
        MotionEvent.ACTION_UP -> {                // RELEASED
            doAction(false)
            return@OnTouchListener true
        }
    }
    false
})

How do I fix this issue?

try to catch MotionEvent.ACTION_CANCEL action also, ACTION_UP means that user intentionally removed finger from View and in your case finger is still there (touching screen), but framework will probably dismiss/cancel all MotionEvent s when leaving Fragment /destroying View

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM