Open tlevavasseur opened 11 years ago
It works!
First, sorry for my English ;)
I agree with tlevavasseur, the hitRect method returns rect regarding the parent coordinates. It works if you add the content view in ignored views list but not if you add a child view... Using View.getGlobalVisibleRect is a good solution but we have to add a global offset point to obtain the correct rect calculation :
private boolean isInIgnoredView(MotionEvent ev) {
Rect rect = new Rect();
for (View v : mIgnoredViews) {
int location[] = {0,0};
v.getLocationOnScreen(location);
v.getGlobalVisibleRect(rect, new Point(location[0], location[1]));
rect.offset(0, - rect.height()/2);
if (rect.contains((int)ev.getX(), (int)ev.getY())) return true;
}
return false;
}
Maybe it's not the best solution but it works in my case (ignore an horizontal scrollview contained into several layouts).
Note : I added an offset because my Y rect position is defined on center (maybe because my layout is vertically centered, ths pivotY is defined on center... I don't know :D)
Remarks to Bismuth76 comment:
So my version of method is following:
private boolean isInIgnoredView(MotionEvent ev) { Rect rect = new Rect(); for (View v : mIgnoredViews) { int[] location = {0,0}; v.getLocationOnScreen(location); if (v.getGlobalVisibleRect(rect, new Point(location[0], location[1])) && rect.contains((int)ev.getRawX(), (int)ev.getRawY())) return true; } return false; }
nice work, It's very helpful
@Starkom Please use the appropriate Markdown so that your code samples are more presentable. Thnx for your contributions.
private boolean isInIgnoredView(MotionEvent ev) {
Rect rect = new Rect();
for (View v : mIgnoredViews) {
int[] location = {0,0};
v.getLocationOnScreen(location);
if (v.getGlobalVisibleRect(rect, new Point(location[0], location[1])) &&
rect.contains((int)ev.getRawX(), (int)ev.getRawY())) return true;
}
return false;
}
Ignored View hitrect is in parent coordinates which may not start at 0;0.
I used View.getGlobalVisibleRect to make it work with my ignored view :