簡體   English   中英

Nativescript:如何以編程方式禁用/啟用 ScrollView 滾動?

[英]Nativescript: how to programmatically disable / enable ScrollView scrolling?

有沒有辦法以編程方式禁用/啟用 NativeScript 中的 ScrollView 滾動?

好的,我發現了如何做到這一點。 這在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)

如果你有另一種情況,只想禁用滾動視圖滾動,你可以使用這樣的東西(對於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;
    }
}))

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

如果有人正在尋找這個,您可以在ScrollView元素中將isUserInteractionEnabled設置為false ,它將禁用用戶滾動(您仍然可以以編程方式進行)。

https://github.com/NativeScript/NativeScript/issues/2892#issuecomment-253536941

沒有簡單的方法可以在NativeScript中禁用ScrollView滾動。 如果您想將ScrollView用作容器,可以使用ContentView來實現此目的。 但是你可以給我更多信息,你為什么要禁用滾動。

用這個

this.scrollview.isUserInteractionEnabled=false;

或這個

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

對於Android

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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM