N
N
neRazrabitchik2021-07-10 10:59:25
Android
neRazrabitchik, 2021-07-10 10:59:25

How to return onClickListener for child elements when using onTouchEvent?

Hello everyone, can someone help with custom scrollview, I needed to track when a person swipes left / right being at the end of the list to display the ui element (something like swipe to refresh layout is only horizontal and works in both directions) Used GestureDetector , swipes are tracked and everything is fine, but the scroll stopped listening to click events from child elements and I can’t figure out how to return it

. I store table cells in the scroll, each has its own click handler

override fun onTouchEvent(ev: MotionEvent): Boolean {
        when (ev.action){
            MotionEvent.ACTION_DOWN -> {
                performClick()
                x1 = ev.x
                y1 = ev.y
            }
            MotionEvent.ACTION_UP -> {
                val diffX = x1.minus(ev.x)
                val diffY = y1.minus(ev.y)
                if (abs(diffX) > abs(diffY)){
                    if (abs(diffX) > MIN_DISTANCE && FINAL_SCROLL_POS){
                        if (diffX < 0){
                            Log.e("SCROLL direction", "RIGHT")
                            scrollViewListener!!.onRightScroll()
                        }else {
                            Log.e("SCROLL direction", "LEFT")
                            scrollViewListener!!.onLeftScroll()
                        }
                        vibrate()
                    }
                }
            }
        }
        return super.onTouchEvent(ev)
    }


The click stops working after overriding the onInterceptTouchEvent method, if it is not overridden, then the x1 value (the position of the first tap on the screen) will always be 0.0
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return if (gestureDetector!!.onTouchEvent(ev)) true else super.onInterceptTouchEvent(ev)
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
neRazrabitchik, 2021-07-10
@neRazrabitchik

In general, I fixed it, if suddenly someone encounters the same problem:
Remove the dispatchTouchEvent method, then simply override onSingleTapConfirmed and return false in it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question