android - myView.getHitRect() returns coordinates in realation to parent. How to get them in relation to the grandparent? -
i using following code create touch delegate of view. problem view clickable area want increase inside of narrow linearlayout. below code can increase clickable area of view, in scope of narrow linearlayout. pass function not parent grandparent of myview (myview.getparent().getparent()). it's relativelayout has space bigger clickable area. touchrect pointing in wrong place , touchdelegate position incorrectly..
because: delegate.gethitrect(touchrect); returns position in relation parent, not grandparent (or parent of parent).
public static runnable gettouchdelegateaction(final view delegate, final view parent, final int toppadding, final int bottompadding, final int leftpadding, final int rightpadding) { return new runnable() { @override public void run() { // construct new rectangle , let delegate set values rect touchrect = new rect(); delegate.gethitrect(touchrect); // modify dimensions of rectangle // padding values below 0 replaced zeros touchrect.top -= math.max(0, toppadding); touchrect.bottom += math.max(0, bottompadding); touchrect.left -= math.max(0, leftpadding); touchrect.right += math.max(0, rightpadding); // going construct touchdelegate touchdelegate touchdelegate = new touchdelegate(touchrect, delegate); // , set on parent parent.settouchdelegate(touchdelegate); } }; }
any suggestions?
if had similar problem, here solution found. difference flag bindtograndparent can set in order put delegate grandparent of clickable view.
public static runnable gettouchdelegateaction(final view delegate, final boolean bindtograndparent, final int toppadding, final int bottompadding, final int leftpadding, final int rightpadding) { return new runnable() { @override public void run() { view parent = (view) delegate.getparent(); view grandparent = (view) parent.getparent(); // construct new rectangle , let delegate set values rect touchrect = new rect(); delegate.gethitrect(touchrect); if (bindtograndparent) { touchrect.top += parent.gettop(); touchrect.left += parent.getleft(); touchrect.bottom += parent.gettop(); touchrect.right += parent.getleft(); } // modify dimensions of rectangle // padding values below 0 replaced zeros touchrect.top -= math.max(0, toppadding); touchrect.bottom += math.max(0, bottompadding); touchrect.left -= math.max(0, leftpadding); touchrect.right += math.max(0, rightpadding); // going construct touchdelegate touchdelegate touchdelegate = new touchdelegate(touchrect, delegate); // , set on parent if (bindtograndparent) { grandparent.settouchdelegate(touchdelegate); } else { parent.settouchdelegate(touchdelegate); } } }; }
it should used follow:
grandparentview.post(funchelper.gettouchdelegateaction(ivfavorite, true, 30, 30, 30, 30));
it's important procedure should posted grandparent of view.
or:
parentview.post(funchelper.gettouchdelegateaction(ivfavorite, false, 30, 30, 30, 30));
Comments
Post a Comment