Skip to content

Commit a3ece59

Browse files
author
farfromrefuge
committed
fix(android): use ContentView for better disposing afterward
1 parent 6ddbe54 commit a3ece59

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/ui-pager/index.android.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { ChangeType, Device, Property, StackLayout, Utils, View, ViewBase, profile } from '@nativescript/core';
2-
import { KeyedTemplate } from '@nativescript/core/ui/core/view';
1+
import { ChangeType, ContentView, Device, KeyedTemplate, Property, ProxyViewContainer, StackLayout, Utils, View, ViewBase } from '@nativescript/core';
32
import { isString } from '@nativescript/core/utils/types';
43
import {
54
ItemEventData,
@@ -754,22 +753,23 @@ function initPagerRecyclerAdapter() {
754753
if (!view && owner._itemViewLoader !== undefined) {
755754
view = owner._itemViewLoader(template.key);
756755
}
757-
const sp = new StackLayout();
758-
if (view) {
759-
sp.addChild(view);
760-
} else {
761-
sp[PLACEHOLDER] = true;
756+
const isNonSync = view === undefined;
757+
if (isNonSync || view instanceof ProxyViewContainer) {
758+
const parentView = new ContentView();
759+
parentView.id = 'pagerViewHolder';
760+
view = parentView;
761+
view[PLACEHOLDER] = true;
762762
}
763-
owner._addView(sp);
763+
owner._addView(view);
764764
// sp._setupAsRootView(owner._context);
765765
// //@ts-ignore
766766
// sp.parent = owner;
767767
// sp._isAddedToNativeVisualTree = true;
768768
// sp.callLoaded();
769-
sp.nativeView.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT));
769+
view.nativeView.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT));
770770

771771
initPagerViewHolder();
772-
const holder = new PagerViewHolder(sp, new WeakRef(owner));
772+
const holder = new PagerViewHolder(view, new WeakRef(owner));
773773
owner._viewHolders.add(holder);
774774
return holder;
775775
}
@@ -822,24 +822,23 @@ function initPagerRecyclerAdapter() {
822822
}
823823
}
824824
const bindingContext = owner._getDataItem(index);
825+
let view = holder.view;
826+
const isNonSync = holder.view[PLACEHOLDER] === true;
827+
view = isNonSync ? view.content : view;
825828
const args = {
826829
eventName: Pager.itemLoadingEvent,
827830
object: owner,
828831
android: holder,
829-
ios: undefined,
830832
index,
831833
bindingContext,
832-
view: holder.view[PLACEHOLDER] ? null : holder.view.getChildAt(0)
834+
view
833835
} as ItemEventData;
834836

835837
owner.notify(args);
836-
if (holder.view[PLACEHOLDER]) {
837-
if (args.view) {
838-
holder.view.addChild(args.view);
839-
} else {
840-
holder.view.addChild(owner._getDefaultItemContent(index));
841-
}
842-
holder.view[PLACEHOLDER] = false;
838+
if (isNonSync && args.view !== view) {
839+
view = args.view;
840+
// the view has been changed on the event handler
841+
(holder.view as ContentView).content = args.view;
843842
}
844843
owner._prepareItem(holder.view, index);
845844
}

src/ui-pager/index.common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export interface ItemEventData {
5353
object: any;
5454
index: number;
5555
view: View;
56-
android: any;
57-
ios: any;
56+
android?: any;
57+
ios?: any;
5858
}
5959

6060
const autoEffectiveItemHeight = 100;

0 commit comments

Comments
 (0)