Nativescript:如何在程序中禁用/启用ScrollView滚动?

5
有没有一种方法在 NativeScript 中以编程方式禁用/启用 ScrollView 滚动?
6个回答

13

好的,我找到了如何做到这一点。在iOS上实际上非常容易:

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

安卓:

对于我的特殊情况,我需要禁用滚动视图的滚动,但允许在其中拖放子视图。在子视图开始拖动(滑动)事件时,我们通过以下方式防止触摸事件被拦截:

scrollView._nativeView.requestDisallowInterceptTouchEvent(true)

当停止拖动(平移)子视图事件时,通过以下方式重新启用它们:

scrollView._nativeView.requestDisallowInterceptTouchEvent(false)

如果你有另一个情况,只想禁用滚动视图,你可以使用类似这样的代码(适用于安卓):

scrollView._nativeView.setOnTouchListener(new android.view.View.OnTouchListener({
   onTouch: function (view, motionEvent) {
       console.log("DISABLED. onTouch event: Got    motionEvent.getAction() " + motionEvent.getAction() + ".");
       return true;
    }
}))

4
“requestDisallowInterceptTouchEvent(true)” 这个方法似乎不能正常工作,我在最新版的NativeScript上测试过了,这个问题有没有被验证过? - bnussey
scrollView.ios.scrollEnabled = false - mster
1
使用 setOnTouchListener 的代码块对我有效。请记住,您必须首先声明android,因此应为 declare const android: any; - JillevdW

4

iOS

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

安卓

scroll_view.android.setScrollEnabled(false); // Disables user scrolling.
scroll_view.android.setScrollEnabled(true); // Enables user scrolling.

2

Angular 2版本:

在您的HTML中:

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

在你的控制器中:

@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;
    }
}

1

0

NativeScript 中没有一种简单的方法来禁用 ScrollView 的滚动。如果你想使用 ScrollView 作为容器,可以使用 ContentView。然而,你能否给我更多信息,为什么你想要禁用滚动。


1
我在ScrollView中有可拖动的元素。当我把它们向下拖动时,ScrollView也会滚动。我想一旦触发pan“began”事件就禁用ScrollView,而在注册pan“ended”之后再重新激活它。 - terreb

0

使用这个

this.scrollview.isUserInteractionEnabled=false;

或者这个

this.scrollView.nativeElement).android.getParent().requestDisallowInterceptTouchEvent(false);

适用于安卓


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接