Disable ScrollView action
Asked Answered
P

3

15

In my application i've a line for increasing the width of a widget(by dragging the line to right/left) and i've the ScrollView enabled in the same activity. I need to disable the scroll view action when the user touches the line and when user releases, it should be enabled. The ScrollView should be in visible state but the action of the scroll view should be disabled. Please help me in solving this problem. I've tried with these but none of them is working.

scroll.setEnabled(false); 
scroll.setFocusable(false); 
scroll.setHorizontalScrollBarEnabled(false); 
scroll.setVerticalScrollBarEnabled(false);

Thanks in advance.

Perrin answered 9/2, 2010 at 13:4 Comment(0)
C
49

This might be a bit late but I ran into the same problem so my solution is similar to above, had to disable the OnTouchListener as follows:

// Get the ScrollView
final ScrollView myScroll = (ScrollView) findViewById(R.id.display_scrollview);


// Disable Scrolling by setting up an OnTouchListener to do nothing
myScroll.setOnTouchListener( new OnTouchListener(){ 
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true; 
    }
});


// Enable Scrolling by removing the OnTouchListner
tvDisplayScroll.setOnTouchListener(null);    
Crepuscular answered 23/2, 2011 at 11:44 Comment(0)
S
37

This is a bit late, but an even easier way is to just get your parent ViewGroup and call requestDisallowInterceptTouchEvent(true) on it. This causes not only the immediate parent (scroll view) but any other parent objects that might intercept the touch to ignore it for the duration of the particular event. This is great when you have a draggable child AND draggable parent, and want to disable parent dragging while the child is being dragged.

It's also a more general solution than specifically disabling the scroll view. If you re-use your draggable child view somewhere else, it will still work: You don't need to know the scroll view's ID. Code that's re-usable without modification is always good.

Spokane answered 10/10, 2011 at 1:29 Comment(5)
Brilliant. You can use this in views by doing getParent().requestDisallowInterceptTouchEvent(...). I used it in horizontally scrollable gallery inside a ScrollView and it works perfectly!Rounds
Bizarrely I had a scrollview->linear layout->my new control and found I had to actually select the scrollview to call this requestDisallowInterceptTouchEvent(true) on before it had an effect of stopping the scrolling. I think the docs say it will send this message upwards to all parents but that didn't work for me.Piggish
Wouldn't this cause children of the ScrollView to not get touch events as well?Strait
This solution save me a DAY. GREAT!Bergeman
@StackOverflowed, yes, but it only applies for the duration of the particular gesture, so as soon as the finger is lifted, then it goes back to normal behavior. The ideas is that when a gesture starts, it's not clear what view that gesture will belong to, so they all handle it; once it becomes clear, that series of motion events is captured by one view, but only for the duration of the gesture, and other views get a motion cancelled event. requestDisallowInterceptTouchEvent() basically lets a child view tell the parent view "This gesture is meant for me, ignore the rest of it..."Spokane
B
0

If anyone's using Android L or later and overriding onTouch and onInterceptTouchEvent isn't working:
try overriding onStartNestedScroll and return false.

There is also a new XML attribute nestedScrollingEnabled, but it seems like it has to be on the View that is leaking the scroll event, rather than on the ScrollView being affected by the leak, or anywhere in the layout hierarchy between them. So if you don't know in advance what child ScrollViews you might have, overriding onStartNestedScroll for the affected ScrollView is the way to go.

Bubonocele answered 1/12, 2015 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.