Reputation: 8841
I have a ScrollView
and linear layout inside it. Inside the linear layout i have multiple views. One of the multiple views (DrawView lets say)is used to draw stuff so i have to override onTouchEvent
of that method and draw things. Currently when i drag or move on the DrawView
, the ScrollView
also handles the touch leading the entire view moving. What i want is that when i touch the DrawView
, the scroll view should not move . I went through some of these answers here
How to vary between child and parent view group touch events
but i did not find anything that i can add in DrawView
which will prevent the ScrollView
from moving up and down. Note that DrawView
does not extend ViewGroup
so i don't have access to requestDisallowInterceptTouchEvent
Upvotes: 4
Views: 2552
Reputation: 8841
So i ended up doing this in my DrawView
in the onTouchEvent
method
getParent().requestDisallowInterceptTouchEvent(true);
, it worked!
Upvotes: 1
Reputation: 4294
You can get the ScrollView
from your DrawView
's onTouchEvent
:
ScrollView scrollView = (ScrollView) getParent();
then you can call requestDisallowInterceptTouchEvent
on scrollView
to prevent it from scrolling when you touch the DrawView
.
scrollView.requestDisallowInterceptTouchEvent(true);
Upvotes: 6
Reputation: 82958
You can prevent scroll of ScrollView whenever your DrawView gets touch event. To do That you need to create a custom ScrollView calls like below [Original Code Snippet]
public class MyScrollView extends ScrollView {
private boolean enableScrolling = true;
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (scrollingEnabled()) {
return super.onInterceptTouchEvent(ev);
} else {
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (scrollingEnabled()) {
return super.onTouchEvent(ev);
} else {
return false;
}
}
private boolean scrollingEnabled(){
return enableScrolling;
}
public void setScrolling(boolean enableScrolling) {
this.enableScrolling = enableScrolling;
}
}
And you can call scrollView.setScrolling(true/false)
as per your requirement.
Upvotes: 2