简体   繁体   中英

Android Kotlin child onClick blocks parent OnTouch

I have this layout hierarchy:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.xxxxxx.Widget
        android:id="@+id/widget1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <com.xxxxxx.Widget
        android:id="@+id/widget2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

I have touch Events for the Parent LinearLayout like this:

parent.setOnTouchListener(myCustomTouchParent)

    class MyCustomTouchParent(ctx: Context): View.OnTouchListener {

        private var isScrollingDown = false
        private var isScrollingUp = false
        private val myGestureDetected = GestureDetector(ctx, MyGestureListener())

        var onRecyclerViewMovingDown: (() -> Unit)? = null

        override fun onTouch(p0: View?, e: MotionEvent): Boolean {
            myGestureDetected.onTouchEvent(e)

            when(e.action){
                MotionEvent.ACTION_UP -> {
                    if (isScrollingDown) {
                        onRecyclerViewMovingDown?.invoke()
                    }
                }

                MotionEvent.ACTION_DOWN -> {
                    Log.i("TAg", "Action Down")
                    isScrollingDown = false
                    isScrollingUp = false

                }
            }
            return true
        }

        inner class MyGestureListener: GestureDetector.SimpleOnGestureListener() {
            override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float): Boolean {
                if(e2.y - e1.y > 0){
                    isScrollingUp = true
                } else if(e2.y - e1.y < 0){
                    isScrollingDown = true
                }
                return super.onScroll(e1, e2, distanceX, distanceY)
            }


        }
    }

Basically, this will detect a 'Scroll Up' event on the parent, and will perform some animations. The problem is, as soon as I set a click listener for widget1 and widget2, the touch event of the parent is no longer working. Is there any workaround for this?

The only thing that worked for me: In the parent LinearLayout, intercept the touch, call onTouchEvent and return false:

override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        onTouchEvent(ev)
        return false
    }

Move the TouchInterceptor with the Gesture Detector class to the parent onTouchEvent:

override fun onTouchEvent(e: MotionEvent): Boolean {
            myGestureDetected.onTouchEvent(e)

            when(e.action){
                MotionEvent.ACTION_UP -> {
                    if (isScrollingDown) {
                        onRecyclerViewMovingDown?.invoke()
                    }
                }

                MotionEvent.ACTION_DOWN -> {
                    isScrollingDown = false
                    isScrollingUp = false

                }
            }
            return super.onTouchEvent(e)
        }

I don't know if there is a better solution, but this one let me handle touch event on the parent first, then it passed the touch to the childs. There you can set your click listeners.

Also, if you don't set click listeners, the area of touch that contains the clickable item won't trigger touch. so, better set

clickable=true

in all the items, and then only set listeners when you need.

You have to override both onTouchListeners on your children views and return false, that will make them not override their parent ontouch.

widget1.onTouch { view, motionEvent -> return@onTouch false }
widget2.onTouch { view, motionEvent -> return@onTouch false }

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