Last active
February 23, 2018 09:38
-
-
Save vakrilov/89f4850b48b659327ab065117f844550 to your computer and use it in GitHub Desktop.
Notify Layout Event Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from "@angular/core"; | |
import { View } from "tns-core-modules/ui/core/view"; | |
@Component({ | |
selector: "ns-app", | |
template: ` | |
<GridLayout rows="auto * auto"> | |
<Label text="------>>>>>"></Label> | |
<notify-layout-view row="1" backgroundColor="lightgreen" (layout)="onLayout($event)"></notify-layout-view> | |
<Label text="------>>>>>" row="2"></Label> | |
</GridLayout>` | |
}) | |
export class AppComponent { | |
public onLayout(args) { | |
const view = <View>args.object; | |
console.log("onLayout size: " + view.getActualSize().width + " / " + view.getActualSize().height); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { StackLayout } from "tns-core-modules/ui/layouts/stack-layout"; | |
import { isAndroid, isIOS } from "tns-core-modules/platform"; | |
import { View } from "tns-core-modules/ui/core/view"; | |
import { registerElement } from "nativescript-angular/element-registry"; | |
export class NotifyLayoutView extends StackLayout { | |
private listener: android.view.View.OnLayoutChangeListener | |
public createNativeView() { | |
const nativeView = super.createNativeView(); | |
if (isAndroid) { | |
const owner = this; | |
this.listener = new android.view.View.OnLayoutChangeListener({ | |
onLayoutChange( | |
v: android.view.View, | |
left: number, top: number, right: number, bottom: number, | |
oldLeft: number, oldTop: number, oldRight: number, oldBottom: number): void { | |
owner.notifyLayoutChanged(); | |
} | |
}); | |
(<android.view.View>nativeView).addOnLayoutChangeListener(this.listener); | |
} | |
return nativeView; | |
} | |
onLayout(left: number, top: number, right: number, bottom: number): void { | |
super.onLayout(left, top, right, bottom); | |
if (isIOS) { | |
this.notifyLayoutChanged(); | |
} | |
} | |
notifyLayoutChanged() { | |
this.notify({ object: this, eventName: "layout" }); | |
} | |
} | |
// Register tag in the ns-angular renderer | |
registerElement("notify-layout-view", () => NotifyLayoutView); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment