Skip to content

Commit e60ce7c

Browse files
committed
chore: testing layoutCompleted event
1 parent 345f64b commit e60ce7c

File tree

1 file changed

+52
-48
lines changed

1 file changed

+52
-48
lines changed

src/collectionview.android.ts

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ import { CLog, CLogTypes, CollectionViewBase, ListViewViewTypes, isScrollEnabled
2121
export * from './collectionview-common';
2222

2323

24+
// @NativeClass
25+
// class PreCachingGridLayoutManager extends com.nativescript.collectionview.PreCachingGridLayoutManager {
26+
// owner: WeakRef<CollectionView>;
27+
// constructor(context, span) {
28+
// super(context, span);
29+
// return global.__native(this);
30+
// }
31+
// onLayoutCompleted(state) {
32+
// super.onLayoutCompleted(state);
33+
// const owner = this.owner && this.owner.get();
34+
// if (owner) {
35+
// owner.notify({
36+
// eventName: 'layoutCompleted',
37+
// object: owner,
38+
// });
39+
// }
40+
// }
41+
// }
42+
2443
declare module '@nativescript/core/ui/core/view' {
2544
interface View {
2645
layoutChangeListenerIsSet: boolean;
@@ -88,15 +107,23 @@ export class CollectionView extends CollectionViewBase {
88107
owner?: WeakRef<CollectionView>;
89108
};
90109

91-
templateTypeNumberString = new Map<string, number>();
92-
templateStringTypeNumber = new Map<number, string>();
93-
_currentNativeItemType = 0;
110+
private templateTypeNumberString = new Map<string, number>();
111+
private templateStringTypeNumber = new Map<number, string>();
112+
private _currentNativeItemType = 0;
94113

95114
// used to store viewHolder and make sure they are not garbaged
96-
_viewHolders = new Array<CollectionViewCellHolder>();
115+
private _viewHolders = new Array<CollectionViewCellHolder>();
97116

98117
// used to "destroy" cells when possible
99-
_viewHolderChildren = new Array();
118+
private _viewHolderChildren = new Array();
119+
120+
private _scrollOrLoadMoreChangeCount = 0;
121+
private _nScrollListener: com.nativescript.collectionview.OnScrollListener.Listener;
122+
scrolling = false;
123+
124+
private _hlayoutParams: android.view.ViewGroup.LayoutParams;
125+
private _vlayoutParams: android.view.ViewGroup.LayoutParams;
126+
private _lastLayoutKey: string;
100127

101128
private _listViewAdapter: com.nativescript.collectionview.Adapter;
102129

@@ -139,6 +166,8 @@ export class CollectionView extends CollectionViewBase {
139166
layoutManager = CollectionViewBase.layoutStyles[this.layoutStyle].createLayout(this);
140167
} else {
141168
layoutManager = new com.nativescript.collectionview.PreCachingGridLayoutManager(this._context, 1);
169+
// layoutManager = new PreCachingGridLayoutManager(this._context, 1);
170+
// (layoutManager as any).owner = new WeakRef(this);
142171
}
143172
// this.spanSize
144173
nativeView.setLayoutManager(layoutManager);
@@ -186,9 +215,6 @@ export class CollectionView extends CollectionViewBase {
186215
this.attachScrollListener();
187216
}
188217

189-
_scrollOrLoadMoreChangeCount = 0;
190-
_nScrollListener: com.nativescript.collectionview.OnScrollListener.Listener;
191-
scrolling = false;
192218
private attachScrollListener() {
193219
if (this._scrollOrLoadMoreChangeCount > 0 && this.isLoaded) {
194220
const nativeView = this.nativeViewProtected;
@@ -303,8 +329,6 @@ export class CollectionView extends CollectionViewBase {
303329
get layoutManager() {
304330
return this.nativeViewProtected && this.nativeViewProtected.layoutManager;
305331
}
306-
_hlayoutParams: android.view.ViewGroup.LayoutParams;
307-
_vlayoutParams: android.view.ViewGroup.LayoutParams;
308332
_getViewLayoutParams() {
309333
if (this.isHorizontal()) {
310334
if (!this._hlayoutParams) {
@@ -423,17 +447,17 @@ export class CollectionView extends CollectionViewBase {
423447
const owner = this;
424448
this.layoutChangeListenerIsSet = true;
425449
this.layoutChangeListener =
426-
this.layoutChangeListener ||
427-
new android.view.View.OnLayoutChangeListener({
428-
onLayoutChange(v: android.view.View, left: number, top: number, right: number, bottom: number, oldLeft: number, oldTop: number, oldRight: number, oldBottom: number): void {
429-
if (left !== oldLeft || top !== oldTop || right !== oldRight || bottom !== oldBottom) {
430-
owner.onLayout(left, top, right, bottom);
431-
if (owner.hasListeners(View.layoutChangedEvent)) {
432-
owner._raiseLayoutChangedEvent();
433-
}
434-
}
435-
},
436-
});
450+
this.layoutChangeListener ||
451+
new android.view.View.OnLayoutChangeListener({
452+
onLayoutChange(v: android.view.View, left: number, top: number, right: number, bottom: number, oldLeft: number, oldTop: number, oldRight: number, oldBottom: number): void {
453+
if (left !== oldLeft || top !== oldTop || right !== oldRight || bottom !== oldBottom) {
454+
owner.onLayout(left, top, right, bottom);
455+
if (owner.hasListeners(View.layoutChangedEvent)) {
456+
owner._raiseLayoutChangedEvent();
457+
}
458+
}
459+
},
460+
});
437461

438462
this.nativeViewProtected.addOnLayoutChangeListener(this.layoutChangeListener);
439463
}
@@ -451,7 +475,12 @@ export class CollectionView extends CollectionViewBase {
451475
if (this.layoutManager && this.layoutManager['setSpanCount']) {
452476
this.layoutManager['setSpanCount'](this.computeSpanCount());
453477
}
454-
setTimeout(()=>this.refresh(),0);
478+
// there is no need to call refresh if it was triggered before with same size.
479+
// this refresh is just to handle size change
480+
const layoutKey = this._innerWidth + '_' + this._innerHeight;
481+
if (this._lastLayoutKey !== layoutKey) {
482+
setTimeout(()=>this.refresh(),0);
483+
}
455484
}
456485
public onSourceCollectionChanged(event: ChangedData<any>) {
457486
if (!this._listViewAdapter) {
@@ -505,6 +534,7 @@ export class CollectionView extends CollectionViewBase {
505534
return;
506535
}
507536
this._isDataDirty = false;
537+
this._lastLayoutKey = this._innerWidth + '_' + this._innerHeight;
508538
let adapter = this._listViewAdapter;
509539
if (!adapter) {
510540
adapter = this._listViewAdapter = this.createComposedAdapter(this.nativeViewProtected);
@@ -514,7 +544,6 @@ export class CollectionView extends CollectionViewBase {
514544
view.setAdapter(adapter);
515545
}
516546

517-
// nativeView.adapter.owner = new WeakRef(this);
518547
const layoutManager = view.getLayoutManager();
519548
if (layoutManager['setSpanCount']) {
520549
layoutManager['setSpanCount'](this.computeSpanCount());
@@ -572,14 +601,6 @@ export class CollectionView extends CollectionViewBase {
572601
nativeView.setPadding(newValue.left, newValue.top, newValue.right, newValue.bottom);
573602
}
574603

575-
// private _getLayoutManagarOrientation() {
576-
// let orientation = androidx.recyclerview.widget.LinearLayoutManager.VERTICAL;
577-
// if (this.isHorizontal()) {
578-
// orientation = androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL;
579-
// }
580-
581-
// return orientation;
582-
// }
583604
private createComposedAdapter(recyclerView: CollectionViewRecyclerView) {
584605
const adapter = new com.nativescript.collectionview.Adapter();
585606
adapter.adapterInterface = new com.nativescript.collectionview.AdapterInterface({
@@ -626,11 +647,6 @@ export class CollectionView extends CollectionViewBase {
626647
this.templateStringTypeNumber.clear();
627648
}
628649

629-
// public notifyDataSetChanged() {
630-
// this.disposeViewHolderViews();
631-
// super.notifyDataSetChanged();
632-
// }
633-
634650
public getItemViewType(position: number) {
635651
let resultType = 0;
636652
let selectorType: string = 'default';
@@ -664,8 +680,6 @@ export class CollectionView extends CollectionViewBase {
664680
return this.templateStringTypeNumber.get(viewType);
665681
}
666682

667-
// private onClickListener;
668-
669683
@profile
670684
public onCreateViewHolder(parent: android.view.ViewGroup, viewType: number) {
671685
let view: View = this.getViewForViewType(ListViewViewTypes.ItemView, this.getKeyByValue(viewType));
@@ -767,15 +781,6 @@ export class CollectionView extends CollectionViewBase {
767781
}
768782
}
769783

770-
// interface CollectionViewAdapter extends androidx.recyclerview.widget.RecyclerView.Adapter<any> {
771-
// // tslint:disable-next-line:no-misused-new
772-
// new (owner: WeakRef<CollectionView>): CollectionViewAdapter;
773-
// clearTemplateTypes();
774-
// disposeViewHolderViews();
775-
// }
776-
// let CollectionViewAdapter: CollectionViewAdapter;
777-
778-
// Snapshot friendly CollectionViewAdapter
779784
interface CollectionViewCellHolder extends com.nativescript.collectionview.CollectionViewCellHolder {
780785
// tslint:disable-next-line:no-misused-new
781786
new (androidView: android.view.View): CollectionViewCellHolder;
@@ -787,7 +792,6 @@ let CollectionViewCellHolder: CollectionViewCellHolder;
787792

788793
export interface CollectionViewRecyclerView extends com.nativescript.collectionview.RecyclerView {
789794
// tslint:disable-next-line:no-misused-new
790-
// new (context: any, owner: WeakRef<CollectionView>): CollectionViewRecyclerView;
791795
new (context: any): CollectionViewRecyclerView;
792796
}
793797

0 commit comments

Comments
 (0)