Skip to content

Commit 795bada

Browse files
committed
fix: spanSize function now has (item, index) as parameters to allow complex layouts
1 parent 4c111d3 commit 795bada

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

src/collectionview/collectionview-common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
384384
}
385385
this.refresh();
386386
};
387-
spanSize: (position: number) => number;
387+
spanSize: (item, index: number) => number;
388388
onSpanSizeChangedInternal = (oldValue, newValue) => {
389389
this.spanSize = newValue;
390390
this.refresh();

src/collectionview/collectionview.android.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export class CollectionView extends CollectionViewBase {
281281
// colWidthProperty.coerce(this);
282282
// rowHeightProperty.coerce(this);
283283
}
284-
_getSpanSize: (position: number) => number;
284+
_getSpanSize: (item, index) => number;
285285
public getViewForItemAtIndex(index: number): View {
286286
let result: View;
287287
this._viewHolders.some(function (cellItemView, key) {
@@ -295,7 +295,7 @@ export class CollectionView extends CollectionViewBase {
295295
return result;
296296
}
297297
//@ts-ignore
298-
set spanSize(inter: (position: number) => number) {
298+
set spanSize(inter: (item, index) => number) {
299299
if (!(typeof inter === 'function')) {
300300
return;
301301
}
@@ -306,7 +306,10 @@ export class CollectionView extends CollectionViewBase {
306306
inter
307307
? new com.nativescript.collectionview.SpanSizeLookup(
308308
new com.nativescript.collectionview.SpanSizeLookup.Interface({
309-
getSpanSize: inter
309+
getSpanSize: (position)=>{
310+
const dataItem = this.getItemAtIndex(position);
311+
return inter(dataItem, position);
312+
}
310313
})
311314
)
312315
: null
@@ -665,6 +668,19 @@ export class CollectionView extends CollectionViewBase {
665668
this.nativeViewProtected.addOnLayoutChangeListener(this.layoutChangeListener);
666669
}
667670
}
671+
_updateSpanCount() {
672+
if (this.isLayoutValid && this.layoutManager && this.layoutManager['setSpanCount']) {
673+
this.layoutManager['setSpanCount'](this.computeSpanCount());
674+
}
675+
}
676+
_onColWidthPropertyChanged(oldValue: CoreTypes.PercentLengthType, newValue: CoreTypes.PercentLengthType) {
677+
this._updateSpanCount();
678+
super._onColWidthPropertyChanged(oldValue, newValue);
679+
}
680+
_onRowHeightPropertyChanged(oldValue: CoreTypes.PercentLengthType, newValue: CoreTypes.PercentLengthType) {
681+
this._updateSpanCount();
682+
super._onRowHeightPropertyChanged(oldValue, newValue);
683+
}
668684
public onLayout(left: number, top: number, right: number, bottom: number) {
669685
super.onLayout(left, top, right, bottom);
670686
const p = CollectionViewBase.plugins[this.layoutStyle];
@@ -675,9 +691,7 @@ export class CollectionView extends CollectionViewBase {
675691
const p = CollectionViewBase.plugins[k];
676692
p.onLayout && p.onLayout(this, left, top, right, bottom);
677693
});
678-
if (this.layoutManager && this.layoutManager['setSpanCount']) {
679-
this.layoutManager['setSpanCount'](this.computeSpanCount());
680-
}
694+
this._updateSpanCount();
681695
// there is no need to call refresh if it was triggered before with same size.
682696
// this refresh is just to handle size change
683697
const layoutKey = this._innerWidth + '_' + this._innerHeight;
@@ -794,7 +808,7 @@ export class CollectionView extends CollectionViewBase {
794808
return;
795809
}
796810
const view = this.nativeViewProtected;
797-
if (!this.isLoaded) {
811+
if (!this.isLoaded || !this.isLayoutValid) {
798812
this._isDataDirty = true;
799813
return;
800814
}
@@ -1020,7 +1034,7 @@ export class CollectionView extends CollectionViewBase {
10201034
let width = this._effectiveColWidth;
10211035
let height = this._effectiveRowHeight;
10221036
if (this._getSpanSize) {
1023-
const spanSize = this._getSpanSize(position);
1037+
const spanSize = this._getSpanSize(bindingContext, position);
10241038
const horizontal = this.isHorizontal();
10251039
if (horizontal) {
10261040
height *= spanSize;

src/collectionview/collectionview.ios.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,8 @@ export class CollectionView extends CollectionViewBase {
674674
let width = this._effectiveColWidth;
675675
let height = this._effectiveRowHeight;
676676
if (this.spanSize) {
677-
const spanSize = this.spanSize(index);
677+
const dataItem = this.getItemAtIndex(index);
678+
const spanSize = this.spanSize(dataItem, index);
678679
const horizontal = this.isHorizontal();
679680
if (horizontal) {
680681
height *= spanSize;
@@ -706,7 +707,9 @@ export class CollectionView extends CollectionViewBase {
706707
let height = this._effectiveRowHeight;
707708
const horizontal = this.isHorizontal();
708709
if (this.spanSize) {
709-
const spanSize = this.spanSize(index.row);
710+
const position = index.row;
711+
const dataItem = this.getItemAtIndex(position);
712+
const spanSize = this.spanSize(dataItem, position);
710713
if (horizontal) {
711714
height *= spanSize;
712715
} else {

0 commit comments

Comments
 (0)