MyView.getHitRect () returns the coordinates in realation in parent. How to get them in relation to grandparents?

I am using the following code to create a touch view delegate. The problem is that the view, which the clickable area I want to enlarge, is inside a very narrow LinearLayout. Thus, the code below can increase the viewing area, but only in my narrow LinearLayout. I would like to pass to this function not parent, but grandparent myView (myView.getParent (). GetParent ()). This is a RelativeLayout, which has room for a larger area with clicks. But then touchRect will point to the wrong place and my TouchDelegate will not display correctly.

Because: delegate.getHitRect (touchRect); returns the position with respect to the parent, not to the grandfather (or parent of the 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 a new Rectangle and let the Delegate set its values Rect touchRect = new Rect(); delegate.getHitRect(touchRect); // Modify the dimensions of the Rectangle // Padding values below zero are replaced by 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); // Now we are going to construct the TouchDelegate TouchDelegate touchDelegate = new TouchDelegate(touchRect, delegate); // And set it on the parent parent.setTouchDelegate(touchDelegate); } }; } 

Any suggestions?

+4
source share
2 answers

If someone had a similar problem, then here is the solution I found. The difference is in the bindToGrandparent flag, which can be set to put the delegate in grandparent with an interactive 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 a new Rectangle and let the Delegate set its 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 the dimensions of the Rectangle // Padding values below zero are replaced by 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); // Now we are going to construct the TouchDelegate TouchDelegate touchDelegate = new TouchDelegate(touchRect, delegate); // And set it on the parent if (bindToGrandparent) { grandparent.setTouchDelegate(touchDelegate); } else { parent.setTouchDelegate(touchDelegate); } } }; } 

It should be used as follows:

 grandparentView.post(FuncHelper.getTouchDelegateAction(ivFavorite, true, 30, 30, 30, 30)); 

It is important that the procedure is published for the grandparents of the submission.

or

 parentView.post(FuncHelper.getTouchDelegateAction(ivFavorite, false, 30, 30, 30, 30)); 
+4
source

If you need to create a sensory delegate of your view regarding grandparents or even the great grandparents, you can add only one line to your code:

((ViewGroup)parent).offsetDescendantRectToMyCoords(delegate, touchRect);

So the final code might look like this:

 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() { Rect touchRect = new Rect(); delegate.getHitRect(touchRect); // Modify the dimensions of the Rectangle // Padding values below zero are replaced by 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); // calculates the relative coordinates to the parent ((ViewGroup) parent).offsetDescendantRectToMyCoords(delegate, touchRect); parent.setTouchDelegate(new TouchDelegate(touchRect, delegate)); } }; } 

Here you can find the official documentation.

0
source

Source: https://habr.com/ru/post/1495723/


All Articles