FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(view-android): override on/off in order to attach and detach fro… · NativeScript/NativeScript@dc1b36b · GitHub

Commit dc1b36b

Browse files
authored andcommitted
feat(view-android): override on/off in order to attach and detach from OnLayoutChangeListener
1 parent 2e45de7 commit dc1b36b

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎tests/app/ui/view/view-tests-layout-event.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ import * as helper from "../helper";
77
import * as TKUnit from "../../TKUnit";
88
import * as utils from "tns-core-modules/utils/utils";
99

10+
export function test_event_LayoutChanged_GetActualSize() {
11+
const test = function (views: Array<View>) {
12+
let buttonLayoutChanged = false;
13+
14+
views[1].on(View.layoutChangedEvent, (data) => {
15+
buttonLayoutChanged = true;
16+
});
17+
18+
TKUnit.waitUntilReady(() => buttonLayoutChanged);
19+
TKUnit.assert(views[1].getActualSize().height > 0);
20+
TKUnit.assert(views[1].getActualSize().width > 0);
21+
};
22+
23+
helper.do_PageTest_WithStackLayout_AndButton(test);
24+
};
25+
1026
export function test_event_LayoutChanged_Listeners() {
1127
const test = function (views: Array<View>) {
1228
let stackLayoutChanged = false;

‎tns-core-modules/ui/core/view/view.android.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ViewCommon, layout, isEnabledProperty, originXProperty, originYProperty, automationTextProperty, isUserInteractionEnabledProperty,
88
traceEnabled, traceWrite, traceCategories, traceNotifyEvent,
99
paddingLeftProperty, paddingTopProperty, paddingRightProperty, paddingBottomProperty,
10-
Color
10+
Color, EventData
1111
} from "./view-common";
1212

1313
import {
@@ -243,6 +243,26 @@ export class View extends ViewCommon {
243243
}
244244
}
245245

246+
on(eventNames: string, callback: (data: EventData) => void, thisArg?: any) {
247+
super.on(eventNames, callback, thisArg);
248+
const isLayoutEvent = typeof eventNames === "string" ? eventNames.indexOf(ViewCommon.layoutChangedEvent) !== -1 : false;
249+
250+
if (this.isLoaded && !this.layoutChangeListenerIsSet && isLayoutEvent) {
251+
this.setOnLayoutChangeListener();
252+
}
253+
}
254+
255+
off(eventNames: string, callback?: any, thisArg?: any) {
256+
super.off(eventNames, callback, thisArg);
257+
const isLayoutEvent = typeof eventNames === "string" ? eventNames.indexOf(ViewCommon.layoutChangedEvent) !== -1 : false;
258+
259+
// Remove native listener only if there are no more user listeners for LayoutChanged event
260+
if (this.isLoaded && this.layoutChangeListenerIsSet && isLayoutEvent && !this.hasListeners(ViewCommon.layoutChangedEvent)) {
261+
this.nativeViewProtected.removeOnLayoutChangeListener(this.layoutChangeListener);
262+
this.layoutChangeListenerIsSet = false;
263+
}
264+
}
265+
246266
public _getFragmentManager(): android.app.FragmentManager {
247267
let manager = this._manager;
248268
if (!manager) {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL