I am trying to put a Google map inside a scroll view, so that the user can scroll down other contents to see the map. The problem is that this scroll view is eating up all t
The accepted answer did not work in my case. The Guest's answer did neither (but almost). If this is the case for someone else try this edited version of the Guest's answer.
I have commented out the action bar height if someone needs to use it when calculating the hitbox.
public class InterceptableScrollView extends ScrollView {
List mInterceptScrollViews = new ArrayList();
public InterceptableScrollView(Context context) {
super(context);
}
public InterceptableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InterceptableScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void addInterceptScrollView(View view) {
mInterceptScrollViews.add(view);
}
public void removeInterceptScrollView(View view) {
mInterceptScrollViews.remove(view);
}
private int getRelativeTop(View myView) {
if (myView.getParent() == this)
return myView.getTop();
else
return myView.getTop() + getRelativeTop((View) myView.getParent());
}
private int getRelativeLeft(View myView) {
if (myView.getParent() == this)
return myView.getLeft();
else
return myView.getLeft() + getRelativeLeft((View) myView.getParent());
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// check if we have any views that should use their own scrolling
if (mInterceptScrollViews.size() > 0) {
int x = (int) event.getX();
int y = (int) event.getY();
/*
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
*/
int viewLocationY = 0;
int viewLocationX = 0;
int relativeTop = 0;
int relativeLeft = 0;
for (View view : mInterceptScrollViews) {
relativeTop = getRelativeTop((View) view.getParent());
relativeLeft = getRelativeLeft((View) view.getParent());
viewLocationY = relativeTop - getScrollY();
viewLocationX = relativeLeft - getScrollX();
if (view.getHeight() + viewLocationY > y && y > viewLocationY && view.getWidth() + viewLocationX > x && x > viewLocationX)
{
return false;
}
}
}
return super.onInterceptTouchEvent(event);
}
}