Files
NativeScript/tns-core-modules/ui/scroll-view/scroll-view.android.ts
Alexander Vakrilov 23757e5dfc Enable recycling of nativeView 2 (#4467)
* enable recycling of nativeView

* backgroundInternal is reset if setting new value leads to background.isEmpty() == true.

* android background.getDefault always return copy of the background. Now all controls that mutate the background can be reset to initial state (e.g. Button & ActionBar)
passing resources to copied background so it respect density.
fix properties initNativeView

* reset padding when backgroundInternal is reset.

* Fix text reset
Fix padding reset

* fix tsc errors

* fix ugly text rendering.

* Add unit tests for recycling native views
Fix several issues that came from the above tests
Fix maxLength property missing a converter callback
Remove old files

* Remove old files

* Revert backgroundInternal setter

* change the order of tests so that appium can work again

* Remove suggestion on every TextView & TextField init (strangely it is enabled after view is recycled....)

* Fix function to get parent layout if specified

* Button stateListAnimator restored when button is recycled
zIndex defaultValue is now undefined instead of NaN

* revert zIndex.setNative to always clear stateListAnimator because it was breaking one UI test (setting value=0 was returning the previous stateListAnimator)

* fix search-bar backgound-color recycling

* Fix alignments setters

* Fix imageView recycling
Fix button recycling
Fix edit-text recycling
resetNativeView is called only if recycleNativeView flag is true

* Fix incorrect merge

* Fix text-view & text-field textTransform

* Fix EditText text reset

* Fix runtime crash on ARM emulator API 21

* Fix text-base minHeight. maxHeight reset
Fix reset of isUserInteractionEnabled
2017-06-29 18:01:22 +03:00

135 lines
4.4 KiB
TypeScript

import { ScrollEventData } from ".";
import { ScrollViewBase, layout } from "./scroll-view-common";
export * from "./scroll-view-common";
export class ScrollView extends ScrollViewBase {
nativeView: org.nativescript.widgets.VerticalScrollView | org.nativescript.widgets.HorizontalScrollView;
private _androidViewId: number = -1;
private handler: android.view.ViewTreeObserver.OnScrollChangedListener;
get horizontalOffset(): number {
const nativeView = this.nativeView;
if (!nativeView) {
return 0;
}
return nativeView.getScrollX() / layout.getDisplayDensity();
}
get verticalOffset(): number {
const nativeView = this.nativeView;
if (!nativeView) {
return 0;
}
return nativeView.getScrollY() / layout.getDisplayDensity();
}
get scrollableWidth(): number {
const nativeView = this.nativeView;
if (!nativeView || this.orientation !== "horizontal") {
return 0;
}
return nativeView.getScrollableLength() / layout.getDisplayDensity();
}
get scrollableHeight(): number {
const nativeView = this.nativeView;
if (!nativeView || this.orientation !== "vertical") {
return 0;
}
return nativeView.getScrollableLength() / layout.getDisplayDensity();
}
public scrollToVerticalOffset(value: number, animated: boolean) {
const nativeView = this.nativeView;
if (nativeView && this.orientation === "vertical") {
value *= layout.getDisplayDensity();
if (animated) {
nativeView.smoothScrollTo(0, value);
} else {
nativeView.scrollTo(0, value);
}
}
}
public scrollToHorizontalOffset(value: number, animated: boolean) {
const nativeView = this.nativeView;
if (nativeView && this.orientation === "horizontal") {
value *= layout.getDisplayDensity();
if (animated) {
nativeView.smoothScrollTo(value, 0);
} else {
nativeView.scrollTo(value, 0);
}
}
}
public createNativeView() {
const nativeView = this.orientation === "horizontal" ? new org.nativescript.widgets.HorizontalScrollView(this._context) : new org.nativescript.widgets.VerticalScrollView(this._context);
if (this._androidViewId < 0) {
this._androidViewId = android.view.View.generateViewId();
}
nativeView.setId(this._androidViewId);
return nativeView;
}
public _onOrientationChanged() {
if (this.nativeView) {
const parent = this.parent;
if (parent) {
parent._removeView(this);
parent._addView(this);
}
}
}
protected attachNative() {
const that = new WeakRef(this);
this.handler = new android.view.ViewTreeObserver.OnScrollChangedListener({
onScrollChanged: function () {
const owner: ScrollView = that.get();
if (owner) {
owner._onScrollChanged();
}
}
});
this.nativeView.getViewTreeObserver().addOnScrollChangedListener(this.handler);
}
private _lastScrollX: number = -1;
private _lastScrollY: number = -1;
private _onScrollChanged() {
const nativeView = this.nativeView;
if (nativeView) {
// Event is only raised if the scroll values differ from the last time in order to wokraround a native Android bug.
// https://github.com/NativeScript/NativeScript/issues/2362
let newScrollX = nativeView.getScrollX();
let newScrollY = nativeView.getScrollY();
if (newScrollX !== this._lastScrollX || newScrollY !== this._lastScrollY) {
this.notify(<ScrollEventData>{
object: this,
eventName: ScrollView.scrollEvent,
scrollX: newScrollX / layout.getDisplayDensity(),
scrollY: newScrollY / layout.getDisplayDensity()
});
this._lastScrollX = newScrollX;
this._lastScrollY = newScrollY;
}
}
}
protected dettachNative() {
this.nativeView.getViewTreeObserver().removeOnScrollChangedListener(this.handler);
this.handler = null;
}
}
ScrollView.prototype.recycleNativeView = false;