【发布时间】:2016-04-21 14:47:22
【问题描述】:
有没有办法在 NativeScript 中以编程方式禁用/启用 ScrollView 滚动?
【问题讨论】:
标签: android ios nativescript
有没有办法在 NativeScript 中以编程方式禁用/启用 ScrollView 滚动?
【问题讨论】:
标签: android ios nativescript
好的,我找到了方法。在 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)
如果您有其他情况并且只想禁用 Scroll View 滚动,您可以使用类似这样的东西(适用于 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;
}
}))
【讨论】:
requestDisallowInterceptTouchEvent(true) 似乎对我不起作用,这是否在最新版本的 NativeScript 上测试过?
scrollView.ios.scrollEnabled = false 谢谢!单线对我有用。
setOnTouchListener 的代码块对我有用。请记住,您必须先声明 android,因此应为 declare const android: any;。
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.
【讨论】:
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);
安卓版
【讨论】: