UNPKG

5.6 kBJavaScriptView Raw
1import { ScrollViewBase, scrollBarIndicatorVisibleProperty, isScrollEnabledProperty } from './scroll-view-common';
2import { layout } from '../../utils';
3import { isUserInteractionEnabledProperty } from '../core/view';
4export * from './scroll-view-common';
5export class ScrollView extends ScrollViewBase {
6 constructor() {
7 super(...arguments);
8 this._androidViewId = -1;
9 this._lastScrollX = -1;
10 this._lastScrollY = -1;
11 }
12 get horizontalOffset() {
13 const nativeView = this.nativeViewProtected;
14 if (!nativeView) {
15 return 0;
16 }
17 return nativeView.getScrollX() / layout.getDisplayDensity();
18 }
19 get verticalOffset() {
20 const nativeView = this.nativeViewProtected;
21 if (!nativeView) {
22 return 0;
23 }
24 return nativeView.getScrollY() / layout.getDisplayDensity();
25 }
26 get scrollableWidth() {
27 const nativeView = this.nativeViewProtected;
28 if (!nativeView || this.orientation !== 'horizontal') {
29 return 0;
30 }
31 return nativeView.getScrollableLength() / layout.getDisplayDensity();
32 }
33 get scrollableHeight() {
34 const nativeView = this.nativeViewProtected;
35 if (!nativeView || this.orientation !== 'vertical') {
36 return 0;
37 }
38 return nativeView.getScrollableLength() / layout.getDisplayDensity();
39 }
40 [isUserInteractionEnabledProperty.setNative](value) {
41 // NOTE: different behavior on iOS & Android:
42 // iOS disables user interaction recursively for all subviews as well
43 this.nativeViewProtected.setClickable(value);
44 this.nativeViewProtected.setFocusable(value);
45 this.nativeViewProtected.setScrollEnabled(value);
46 }
47 [isScrollEnabledProperty.getDefault]() {
48 return this.nativeViewProtected.getScrollEnabled();
49 }
50 [isScrollEnabledProperty.setNative](value) {
51 this.nativeViewProtected.setScrollEnabled(value);
52 }
53 [scrollBarIndicatorVisibleProperty.getDefault]() {
54 return true;
55 }
56 [scrollBarIndicatorVisibleProperty.setNative](value) {
57 if (this.orientation === 'horizontal') {
58 this.nativeViewProtected.setHorizontalScrollBarEnabled(value);
59 }
60 else {
61 this.nativeViewProtected.setVerticalScrollBarEnabled(value);
62 }
63 }
64 scrollToVerticalOffset(value, animated) {
65 const nativeView = this.nativeViewProtected;
66 if (nativeView && this.orientation === 'vertical' && this.isScrollEnabled) {
67 value *= layout.getDisplayDensity();
68 if (animated) {
69 nativeView.smoothScrollTo(0, value);
70 }
71 else {
72 nativeView.scrollTo(0, value);
73 }
74 }
75 }
76 scrollToHorizontalOffset(value, animated) {
77 const nativeView = this.nativeViewProtected;
78 if (nativeView && this.orientation === 'horizontal' && this.isScrollEnabled) {
79 value *= layout.getDisplayDensity();
80 if (animated) {
81 nativeView.smoothScrollTo(value, 0);
82 }
83 else {
84 nativeView.scrollTo(value, 0);
85 }
86 }
87 }
88 createNativeView() {
89 return this.orientation === 'horizontal' ? new org.nativescript.widgets.HorizontalScrollView(this._context) : new org.nativescript.widgets.VerticalScrollView(this._context);
90 }
91 initNativeView() {
92 super.initNativeView();
93 if (this._androidViewId < 0) {
94 this._androidViewId = android.view.View.generateViewId();
95 }
96 this.nativeViewProtected.setId(this._androidViewId);
97 }
98 _onOrientationChanged() {
99 if (this.nativeViewProtected) {
100 const parent = this.parent;
101 if (parent) {
102 parent._removeView(this);
103 parent._addView(this);
104 }
105 }
106 }
107 attachNative() {
108 const that = new WeakRef(this);
109 this.handler = new android.view.ViewTreeObserver.OnScrollChangedListener({
110 onScrollChanged: function () {
111 const owner = that.get();
112 if (owner) {
113 owner._onScrollChanged();
114 }
115 },
116 });
117 this.nativeViewProtected.getViewTreeObserver().addOnScrollChangedListener(this.handler);
118 }
119 _onScrollChanged() {
120 const nativeView = this.nativeViewProtected;
121 if (nativeView) {
122 // Event is only raised if the scroll values differ from the last time in order to wokraround a native Android bug.
123 // https://github.com/NativeScript/NativeScript/issues/2362
124 const newScrollX = nativeView.getScrollX();
125 const newScrollY = nativeView.getScrollY();
126 if (newScrollX !== this._lastScrollX || newScrollY !== this._lastScrollY) {
127 this.notify({
128 object: this,
129 eventName: ScrollView.scrollEvent,
130 scrollX: newScrollX / layout.getDisplayDensity(),
131 scrollY: newScrollY / layout.getDisplayDensity(),
132 });
133 this._lastScrollX = newScrollX;
134 this._lastScrollY = newScrollY;
135 }
136 }
137 }
138 dettachNative() {
139 var _a;
140 (_a = this.nativeViewProtected) === null || _a === void 0 ? void 0 : _a.getViewTreeObserver().removeOnScrollChangedListener(this.handler);
141 this.handler = null;
142 }
143}
144ScrollView.prototype.recycleNativeView = 'never';
145//# sourceMappingURL=index.android.js.map
\No newline at end of file