Skip to content Skip to sidebar Skip to footer

Expand Touchdelegate Beyond View's Parent

I have a layout that looks like this: and I am trying to expand clickable area for the green view. The code that I use for this purpose is: public static void setTouchDelegate(Vie

Solution 1:

Basically you need to set the delegateArea relative to the view' ancestor, and set the TouchDelegate on this ancestor.

In your image the target ancestor is the view with the blue background.

fungetRelativeLeft(view: View?, ancestor: View) : Int {
    if (view == null)
        return0returnif (view.parent == ancestor)
        view.left
    else
        view.left + getRelativeLeft(view.parent as? View, ancestor)
}

fungetRelativeTop(view: View?, ancestor: View) : Int {
    if (view == null)
        return0returnif (view.parent == ancestor)
        view.top
    else
        view.top + getRelativeTop(view.parent as? View, ancestor)
}


val relativeTop = Point(getRelativeLeft(view, ancestorView), getRelativeTop(view, ancestorView))

// View area in ancestor coordinatesval relativeRc = Rect(relativeTop.x, relativeTop.y, relativeTop.x + view.width, relativeTop.y + view.height) 

// Increase touch area
relativeRc.inset(-offsetWidth, -offsetHeight)

// Set touch delegate on ancestorView with target view
ancestorView.touchDelegate = TouchDelegate(relativeRc, view)

Also it is better to use the modified TouchDelegate, due to a bug in the original version https://gist.github.com/patrickhammond/6d49e9ac08f96bd8d302#gistcomment-2385782

Post a Comment for "Expand Touchdelegate Beyond View's Parent"