Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nativescript: how to programmatically disable / enable ScrollView scrolling?

Is there a way to programatically disable/enable ScrollView scrolling in NativeScript?

like image 983
terreb Avatar asked Apr 21 '16 14:04

terreb


3 Answers

Ok, I found how to do that. It's actually pretty easy on iOS:

var scrollView = page.getViewById('YOUR_VIEW_ID')
scrollView.ios.scrollEnabled = false // to disable
scrollView.ios.scrollEnabled = true  // to enable back

Android:

Also for my particular case, where I need to disable scrolling of the Scroll View, but drag and drop child views inside. On subview start dragging (panning) event we prevent touch events interception in our Scroll View by:

scrollView._nativeView.requestDisallowInterceptTouchEvent(true)

And enable them again on stop dragging (panning) subview event by:

scrollView._nativeView.requestDisallowInterceptTouchEvent(false)

If you have another case and just want to disable Scroll View scrolling you can use something like this (for Android):

scrollView._nativeView.setOnTouchListener(new android.view.View.OnTouchListener({
   onTouch: function (view, motionEvent) {
       console.log("DISABLED. onTouch event: Got    motionEvent.getAction() " + motionEvent.getAction() + ".");
       return true;
    }
}))
like image 111
terreb Avatar answered Nov 17 '22 02:11

terreb


iOS

scroll_view.ios.scrollEnabled = false; // Disables user scrolling.
scroll_view.ios.scrollEnabled = true; // Enables user scrolling.

Android

scroll_view.android.setScrollEnabled(false); // Disables user scrolling.
scroll_view.android.setScrollEnabled(true); // Enables user scrolling.
like image 4
Mudlabs Avatar answered Nov 17 '22 03:11

Mudlabs


Angular 2 Version:

In your html:

<ScrollView #scrollView >
  ...
</ScrollView>

In your controller:

@ViewChild('scrollView') scrollView: ElementRef;

allowScrolling(e) {
    const scrollView: ScrollView = this.scrollView.nativeElement;
    if (platformModule.device.os === 'Android') {
        scrollView.nativeView.requestDisallowInterceptTouchEvent(e);
    }
    if (platformModule.device.os === 'iOS') {
        scrollView.ios.scrollEnabled = !e;
    }
}
like image 2
kenny Avatar answered Nov 17 '22 01:11

kenny