@nativescript-community/ui-collectionview 4.0.47 → 4.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,889 +0,0 @@
1
- import { ChangeType, ContentView, Property, ProxyViewContainer, Trace, View, booleanConverter, paddingBottomProperty, paddingLeftProperty, paddingRightProperty, paddingTopProperty, profile } from '@nativescript/core';
2
- import { layout } from '@nativescript/core/utils/utils';
3
- import { reorderLongPressEnabledProperty, reorderingEnabledProperty, reverseLayoutProperty, scrollBarIndicatorVisibleProperty } from './collectionview';
4
- import { CLog, CLogTypes, CollectionViewBase, ListViewViewTypes, isScrollEnabledProperty, orientationProperty } from './collectionview-common';
5
- export * from './collectionview-common';
6
- var SimpleCallback = /** @class */ (function (_super) {
7
- __extends(SimpleCallback, _super);
8
- function SimpleCallback(param1, param2) {
9
- var _this = _super.call(this, param1, param2) || this;
10
- _this.startPosition = -1;
11
- _this.endPosition = -1;
12
- return global.__native(_this);
13
- }
14
- SimpleCallback.prototype.onMove = function (recyclerview, viewHolder, target) {
15
- var startPosition = viewHolder.getAdapterPosition();
16
- var endPosition = target.getAdapterPosition();
17
- if (this.startPosition === -1) {
18
- this.startPosition = startPosition;
19
- }
20
- this.endPosition = endPosition;
21
- var owner = this.owner && this.owner.get();
22
- if (owner) {
23
- owner._reorderItemInSource(startPosition, endPosition);
24
- return true;
25
- }
26
- return false;
27
- };
28
- SimpleCallback.prototype.onSelectedChanged = function (viewHolder, state) {
29
- if (viewHolder) {
30
- if (this.startPosition === -1) {
31
- this.startPosition = viewHolder.getAdapterPosition();
32
- }
33
- }
34
- if (!viewHolder) {
35
- // this is where we identify the end of the drag and call the end event
36
- var owner = this.owner && this.owner.get();
37
- if (this.endPosition === -1) {
38
- this.endPosition = this.startPosition;
39
- }
40
- if (owner) {
41
- var item = owner.getItemAtIndex(this.startPosition);
42
- owner._callItemReorderedEvent(this.startPosition, this.endPosition, item);
43
- }
44
- this.startPosition = -1;
45
- this.endPosition = -1;
46
- owner.isDragging = false;
47
- }
48
- };
49
- SimpleCallback.prototype.onSwiped = function (viewHolder, direction) { };
50
- SimpleCallback.prototype.isItemViewSwipeEnabled = function () {
51
- // disabled for now
52
- return false;
53
- };
54
- SimpleCallback.prototype.isLongPressDragEnabled = function () {
55
- // we use our custom longpress gesture handler
56
- return false;
57
- };
58
- return SimpleCallback;
59
- }(androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback));
60
- var LongPressGestureListenerImpl = /** @class */ (function (_super) {
61
- __extends(LongPressGestureListenerImpl, _super);
62
- function LongPressGestureListenerImpl(_owner) {
63
- var _this = _super.call(this) || this;
64
- _this._owner = _owner;
65
- return global.__native(_this);
66
- }
67
- LongPressGestureListenerImpl.prototype.onLongPress = function (motionEvent) {
68
- var owner = this._owner && this._owner.get();
69
- if (owner) {
70
- owner.onReorderLongPress(motionEvent);
71
- }
72
- };
73
- return LongPressGestureListenerImpl;
74
- }(android.view.GestureDetector.SimpleOnGestureListener));
75
- let CellViewHolder;
76
- let LayoutParams;
77
- const extraLayoutSpaceProperty = new Property({
78
- name: 'extraLayoutSpace'
79
- });
80
- const itemViewCacheSizeProperty = new Property({
81
- name: 'itemViewCacheSize'
82
- });
83
- const nestedScrollingEnabledProperty = new Property({
84
- name: 'nestedScrollingEnabled',
85
- defaultValue: true,
86
- valueConverter: booleanConverter
87
- });
88
- export class CollectionView extends CollectionViewBase {
89
- constructor() {
90
- super(...arguments);
91
- this.templateTypeNumberString = new Map();
92
- this.templateStringTypeNumber = new Map();
93
- this._currentNativeItemType = 0;
94
- this._viewHolders = new Array();
95
- this._viewHolderChildren = new Array();
96
- this._scrollOrLoadMoreChangeCount = 0;
97
- this.scrolling = false;
98
- this.needsScrollStartEvent = false;
99
- this.animateItemUpdate = false;
100
- this.isDragging = false;
101
- this._layedOut = false;
102
- }
103
- createNativeView() {
104
- if (!CollectionViewRecyclerView) {
105
- CollectionViewRecyclerView = com.nativescript.collectionview.RecyclerView;
106
- }
107
- const recyclerView = CollectionViewRecyclerView.createRecyclerView(this._context);
108
- return recyclerView;
109
- }
110
- initNativeView() {
111
- this.setOnLayoutChangeListener();
112
- super.initNativeView();
113
- const nativeView = this.nativeViewProtected;
114
- nativeView.owner = new WeakRef(this);
115
- let layoutManager;
116
- if (CollectionViewBase.layoutStyles[this.layoutStyle]) {
117
- layoutManager = CollectionViewBase.layoutStyles[this.layoutStyle].createLayout(this);
118
- }
119
- else {
120
- layoutManager = new com.nativescript.collectionview.PreCachingGridLayoutManager(this._context, 1);
121
- }
122
- nativeView.setLayoutManager(layoutManager);
123
- nativeView.layoutManager = layoutManager;
124
- this.spanSize = this._getSpanSize;
125
- const animator = new com.h6ah4i.android.widget.advrecyclerview.animator.RefactoredDefaultItemAnimator();
126
- animator.setSupportsChangeAnimations(false);
127
- nativeView.setItemAnimator(animator);
128
- this.refresh();
129
- }
130
- getViewForItemAtIndex(index) {
131
- let result;
132
- this._viewHolders.some(function (cellItemView, key) {
133
- if (cellItemView && cellItemView.getAdapterPosition() === index) {
134
- result = cellItemView.view;
135
- return true;
136
- }
137
- return false;
138
- });
139
- return result;
140
- }
141
- set spanSize(inter) {
142
- if (!(typeof inter === 'function')) {
143
- return;
144
- }
145
- this._getSpanSize = inter;
146
- const layoutManager = this.layoutManager;
147
- if (layoutManager && layoutManager['setSpanSizeLookup']) {
148
- layoutManager['setSpanSizeLookup'](inter
149
- ? new com.nativescript.collectionview.SpanSizeLookup(new com.nativescript.collectionview.SpanSizeLookup.Interface({
150
- getSpanSize: (position) => {
151
- const dataItem = this.getItemAtIndex(position);
152
- return inter(dataItem, position);
153
- }
154
- }))
155
- : null);
156
- }
157
- }
158
- get spanSize() {
159
- return this._getSpanSize;
160
- }
161
- onLoaded() {
162
- super.onLoaded();
163
- this.attachScrollListener();
164
- }
165
- attachScrollListener() {
166
- if (this._scrollOrLoadMoreChangeCount > 0 && this.isLoaded) {
167
- const nativeView = this.nativeViewProtected;
168
- if (!nativeView.scrollListener) {
169
- this._nScrollListener = new com.nativescript.collectionview.OnScrollListener.Listener({
170
- onScrollStateChanged: this.onScrollStateChanged.bind(this),
171
- onScrolled: this.onScrolled.bind(this)
172
- });
173
- const scrollListener = new com.nativescript.collectionview.OnScrollListener(this._nScrollListener);
174
- nativeView.addOnScrollListener(scrollListener);
175
- nativeView.scrollListener = scrollListener;
176
- }
177
- }
178
- }
179
- dettachScrollListener() {
180
- if (this._scrollOrLoadMoreChangeCount === 0 && this.isLoaded) {
181
- const nativeView = this.nativeViewProtected;
182
- if (nativeView.scrollListener) {
183
- this.nativeView.removeOnScrollListener(nativeView.scrollListener);
184
- nativeView.scrollListener = null;
185
- this._nScrollListener = null;
186
- }
187
- }
188
- }
189
- computeScrollEventData(view, eventName, dx, dy) {
190
- const horizontal = this.isHorizontal();
191
- const offset = horizontal ? view.computeHorizontalScrollOffset() : view.computeVerticalScrollOffset();
192
- const range = horizontal ? view.computeHorizontalScrollRange() : view.computeVerticalScrollRange();
193
- const extent = horizontal ? view.computeHorizontalScrollExtent() : view.computeVerticalScrollExtent();
194
- return {
195
- object: this,
196
- eventName,
197
- scrollOffset: offset / layout.getDisplayDensity(),
198
- scrollOffsetPercentage: offset / (range - extent),
199
- dx,
200
- dy
201
- };
202
- }
203
- onScrolled(view, dx, dy) {
204
- if (!this || !this.scrolling) {
205
- return;
206
- }
207
- if (this.needsScrollStartEvent) {
208
- this.needsScrollStartEvent = false;
209
- if (this.hasListeners(CollectionViewBase.scrollStartEvent)) {
210
- this.notify(this.computeScrollEventData(view, CollectionViewBase.scrollStartEvent, dx, dy));
211
- }
212
- }
213
- if (this.hasListeners(CollectionViewBase.scrollEvent)) {
214
- this.notify(this.computeScrollEventData(view, CollectionViewBase.scrollEvent, dx, dy));
215
- }
216
- if (this.hasListeners(CollectionViewBase.loadMoreItemsEvent) && this.items) {
217
- const layoutManager = view.getLayoutManager();
218
- if (layoutManager['findLastCompletelyVisibleItemPosition']) {
219
- const lastVisibleItemPos = layoutManager['findLastCompletelyVisibleItemPosition']();
220
- const loadMoreItemIndex = this.items.length - this.loadMoreThreshold;
221
- if (lastVisibleItemPos === loadMoreItemIndex) {
222
- this.notify({
223
- eventName: CollectionViewBase.loadMoreItemsEvent,
224
- object: this
225
- });
226
- }
227
- }
228
- else if (layoutManager['findLastCompletelyVisibleItemPositions'] && layoutManager['getSpanCount']) {
229
- let positions = Array.create('int', layoutManager['getSpanCount']());
230
- positions = layoutManager['findLastCompletelyVisibleItemPositions'](positions);
231
- let lastVisibleItemPos = 0;
232
- for (let i = 0; i < positions.length; i++) {
233
- if (positions[i] > lastVisibleItemPos) {
234
- lastVisibleItemPos = positions[i];
235
- }
236
- }
237
- const loadMoreItemIndex = this.items.length - this.loadMoreThreshold;
238
- if (lastVisibleItemPos >= loadMoreItemIndex) {
239
- this.notify({
240
- eventName: CollectionViewBase.loadMoreItemsEvent,
241
- object: this
242
- });
243
- }
244
- }
245
- }
246
- }
247
- onScrollStateChanged(view, newState) {
248
- if (this.scrolling && newState === 0) {
249
- this.scrolling = false;
250
- if (this.hasListeners(CollectionViewBase.scrollEndEvent)) {
251
- this.notify(this.computeScrollEventData(view, CollectionViewBase.scrollEndEvent));
252
- }
253
- }
254
- else if (!this.scrolling && newState === 1) {
255
- this.needsScrollStartEvent = true;
256
- this.scrolling = true;
257
- }
258
- }
259
- addEventListener(arg, callback, thisArg) {
260
- super.addEventListener(arg, callback, thisArg);
261
- if (arg === CollectionViewBase.scrollEvent || arg === CollectionViewBase.scrollStartEvent || arg === CollectionViewBase.scrollEndEvent || arg === CollectionViewBase.loadMoreItemsEvent) {
262
- this._scrollOrLoadMoreChangeCount++;
263
- this.attachScrollListener();
264
- }
265
- }
266
- removeEventListener(arg, callback, thisArg) {
267
- super.removeEventListener(arg, callback, thisArg);
268
- if (arg === CollectionViewBase.scrollEvent || arg === CollectionViewBase.scrollStartEvent || arg === CollectionViewBase.scrollEndEvent || arg === CollectionViewBase.loadMoreItemsEvent) {
269
- this._scrollOrLoadMoreChangeCount--;
270
- this.dettachScrollListener();
271
- }
272
- }
273
- disposeNativeView() {
274
- const nativeView = this.nativeView;
275
- if (nativeView.scrollListener) {
276
- this.nativeView.removeOnScrollListener(nativeView.scrollListener);
277
- nativeView.scrollListener = null;
278
- this._nScrollListener = null;
279
- }
280
- nativeView.layoutManager = null;
281
- this._listViewAdapter = null;
282
- this._itemTouchHelper = null;
283
- this._simpleItemTouchCallback = null;
284
- this.disposeViewHolderViews();
285
- this._hlayoutParams = null;
286
- this._vlayoutParams = null;
287
- this.clearTemplateTypes();
288
- super.disposeNativeView();
289
- }
290
- get android() {
291
- return this.nativeView;
292
- }
293
- get layoutManager() {
294
- return this.nativeViewProtected && this.nativeViewProtected.layoutManager;
295
- }
296
- _getViewLayoutParams() {
297
- if (this.isHorizontal()) {
298
- if (!this._hlayoutParams) {
299
- LayoutParams = LayoutParams || android.view.ViewGroup.LayoutParams;
300
- this._hlayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
301
- }
302
- return this._hlayoutParams;
303
- }
304
- else {
305
- if (!this._vlayoutParams) {
306
- LayoutParams = LayoutParams || android.view.ViewGroup.LayoutParams;
307
- this._vlayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
308
- }
309
- return this._vlayoutParams;
310
- }
311
- }
312
- [paddingTopProperty.getDefault]() {
313
- return { value: this._defaultPaddingTop, unit: 'px' };
314
- }
315
- [paddingTopProperty.setNative](value) {
316
- this._setPadding({ top: this.effectivePaddingTop });
317
- }
318
- [paddingRightProperty.getDefault]() {
319
- return { value: this._defaultPaddingRight, unit: 'px' };
320
- }
321
- [paddingRightProperty.setNative](value) {
322
- this._setPadding({ right: this.effectivePaddingRight });
323
- }
324
- [paddingBottomProperty.getDefault]() {
325
- return { value: this._defaultPaddingBottom, unit: 'px' };
326
- }
327
- [paddingBottomProperty.setNative](value) {
328
- this._setPadding({ bottom: this.effectivePaddingBottom });
329
- }
330
- [paddingLeftProperty.getDefault]() {
331
- return { value: this._defaultPaddingLeft, unit: 'px' };
332
- }
333
- [paddingLeftProperty.setNative](value) {
334
- this._setPadding({ left: this.effectivePaddingLeft });
335
- }
336
- [orientationProperty.getDefault]() {
337
- return 'vertical';
338
- }
339
- [orientationProperty.setNative](value) {
340
- const layoutManager = this.layoutManager;
341
- if (!layoutManager || !layoutManager['setOrientation']) {
342
- return;
343
- }
344
- if (this.isHorizontal()) {
345
- layoutManager['setOrientation'](0);
346
- }
347
- else {
348
- layoutManager['setOrientation'](1);
349
- }
350
- this.updateScrollBarVisibility(this.scrollBarIndicatorVisible);
351
- }
352
- [isScrollEnabledProperty.setNative](value) {
353
- const layoutManager = this.layoutManager;
354
- if (layoutManager && layoutManager.setScrollEnabled) {
355
- layoutManager.setScrollEnabled(value);
356
- }
357
- }
358
- [reverseLayoutProperty.setNative](value) {
359
- const layoutManager = this.layoutManager;
360
- if (layoutManager && layoutManager.setReverseLayout) {
361
- layoutManager.setReverseLayout(value);
362
- }
363
- }
364
- [nestedScrollingEnabledProperty.setNative](value) {
365
- this.nativeViewProtected.setNestedScrollingEnabled(value);
366
- }
367
- [extraLayoutSpaceProperty.setNative](value) {
368
- const layoutManager = this.layoutManager;
369
- if (layoutManager && layoutManager['setExtraLayoutSpace']) {
370
- layoutManager['setExtraLayoutSpace'](value);
371
- }
372
- }
373
- [itemViewCacheSizeProperty.setNative](value) {
374
- this.nativeViewProtected.setItemViewCacheSize(value);
375
- }
376
- [scrollBarIndicatorVisibleProperty.getDefault]() {
377
- return true;
378
- }
379
- [scrollBarIndicatorVisibleProperty.setNative](value) {
380
- this.updateScrollBarVisibility(value);
381
- }
382
- updateScrollBarVisibility(value) {
383
- if (!this.nativeViewProtected) {
384
- return;
385
- }
386
- if (this.orientation === 'horizontal') {
387
- this.nativeViewProtected.setHorizontalScrollBarEnabled(value);
388
- }
389
- else {
390
- this.nativeViewProtected.setVerticalScrollBarEnabled(value);
391
- }
392
- }
393
- startDragging(index) {
394
- if (this.reorderEnabled && this._itemTouchHelper) {
395
- let viewHolder;
396
- this._viewHolders.some((v) => {
397
- if (v.getAdapterPosition() === index) {
398
- viewHolder = v;
399
- return true;
400
- }
401
- return false;
402
- });
403
- if (viewHolder) {
404
- this.startViewHolderDragging(index, viewHolder);
405
- }
406
- }
407
- }
408
- startViewHolderDragging(index, viewHolder) {
409
- if (!this.isDragging && this.shouldMoveItemAtIndex(index)) {
410
- this.isDragging = true;
411
- this._itemTouchHelper.startDrag(viewHolder);
412
- }
413
- }
414
- onReorderLongPress(motionEvent) {
415
- const collectionView = this.nativeViewProtected;
416
- if (!collectionView) {
417
- return;
418
- }
419
- const view = collectionView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
420
- const viewHolder = view != null ? collectionView.getChildViewHolder(view) : null;
421
- if (viewHolder) {
422
- this.startViewHolderDragging(viewHolder.getAdapterPosition(), viewHolder);
423
- }
424
- }
425
- _reorderItemInSource(oldPosition, newPosition) {
426
- const adapter = this._listViewAdapter;
427
- adapter.notifyItemMoved(oldPosition, newPosition);
428
- super._reorderItemInSource(oldPosition, newPosition, false);
429
- }
430
- [reorderLongPressEnabledProperty.setNative](value) {
431
- if (value) {
432
- if (!this._longPressGesture) {
433
- this._longPressGesture = new androidx.core.view.GestureDetectorCompat(this._context, new LongPressGestureListenerImpl(new WeakRef(this)));
434
- this._itemTouchListerner = new androidx.recyclerview.widget.RecyclerView.OnItemTouchListener({
435
- onInterceptTouchEvent: (view, event) => {
436
- if (this.reorderEnabled && this._longPressGesture) {
437
- this._longPressGesture.onTouchEvent(event);
438
- }
439
- return false;
440
- },
441
- onTouchEvent: (param0, param1) => { },
442
- onRequestDisallowInterceptTouchEvent: (disallowIntercept) => { }
443
- });
444
- }
445
- this.nativeViewProtected.addOnItemTouchListener(this._itemTouchListerner);
446
- }
447
- else {
448
- if (this._itemTouchListerner) {
449
- this.nativeViewProtected.removeOnItemTouchListener(this._itemTouchListerner);
450
- }
451
- }
452
- }
453
- [reorderingEnabledProperty.setNative](value) {
454
- if (value) {
455
- if (!this._simpleItemTouchCallback) {
456
- const ItemTouchHelper = androidx.recyclerview.widget.ItemTouchHelper;
457
- this._simpleItemTouchCallback = new SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END, 0);
458
- this._simpleItemTouchCallback.owner = new WeakRef(this);
459
- this._itemTouchHelper = new androidx.recyclerview.widget.ItemTouchHelper(this._simpleItemTouchCallback);
460
- this._itemTouchHelper.attachToRecyclerView(this.nativeViewProtected);
461
- }
462
- }
463
- }
464
- onItemViewLoaderChanged() {
465
- if (this.itemViewLoader) {
466
- this.refresh();
467
- }
468
- }
469
- onItemTemplateSelectorChanged(oldValue, newValue) {
470
- super.onItemTemplateSelectorChanged(oldValue, newValue);
471
- this.clearTemplateTypes();
472
- this.refresh();
473
- }
474
- onItemTemplateChanged(oldValue, newValue) {
475
- super.onItemTemplateChanged(oldValue, newValue);
476
- this.refresh();
477
- }
478
- onItemTemplatesChanged(oldValue, newValue) {
479
- super.onItemTemplatesChanged(oldValue, newValue);
480
- this.refresh();
481
- }
482
- setOnLayoutChangeListener() {
483
- if (this.nativeViewProtected) {
484
- const owner = this;
485
- this.layoutChangeListenerIsSet = true;
486
- this.layoutChangeListener =
487
- this.layoutChangeListener ||
488
- new android.view.View.OnLayoutChangeListener({
489
- onLayoutChange(v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) {
490
- if (left !== oldLeft || top !== oldTop || right !== oldRight || bottom !== oldBottom) {
491
- owner.onLayout(left, top, right, bottom);
492
- if (owner.hasListeners(View.layoutChangedEvent)) {
493
- owner._raiseLayoutChangedEvent();
494
- }
495
- }
496
- }
497
- });
498
- this.nativeViewProtected.addOnLayoutChangeListener(this.layoutChangeListener);
499
- }
500
- }
501
- _updateSpanCount() {
502
- if (this._layedOut && this.layoutManager && this.layoutManager['setSpanCount']) {
503
- this.layoutManager['setSpanCount'](this.computeSpanCount());
504
- }
505
- }
506
- _onColWidthPropertyChanged(oldValue, newValue) {
507
- this._updateSpanCount();
508
- super._onColWidthPropertyChanged(oldValue, newValue);
509
- }
510
- _onRowHeightPropertyChanged(oldValue, newValue) {
511
- this._updateSpanCount();
512
- super._onRowHeightPropertyChanged(oldValue, newValue);
513
- }
514
- onLayout(left, top, right, bottom) {
515
- this._layedOut = true;
516
- super.onLayout(left, top, right, bottom);
517
- const p = CollectionViewBase.plugins[this.layoutStyle];
518
- if (p && p.onLayout) {
519
- p.onLayout(this, left, top, right, bottom);
520
- }
521
- this.plugins.forEach((k) => {
522
- const p = CollectionViewBase.plugins[k];
523
- p.onLayout && p.onLayout(this, left, top, right, bottom);
524
- });
525
- this._updateSpanCount();
526
- const layoutKey = this._innerWidth + '_' + this._innerHeight;
527
- if (this._lastLayoutKey !== layoutKey) {
528
- setTimeout(() => this.refresh(), 0);
529
- }
530
- }
531
- onSourceCollectionChanged(event) {
532
- if (!this._listViewAdapter || this._dataUpdatesSuspended) {
533
- return;
534
- }
535
- if (Trace.isEnabled()) {
536
- CLog(CLogTypes.log, 'onItemsChanged', event.action, event.index, event.addedCount, event.removed, event.removed && event.removed.length);
537
- }
538
- switch (event.action) {
539
- case ChangeType.Delete: {
540
- this._listViewAdapter.notifyItemRangeRemoved(event.index, event.removed.length);
541
- return;
542
- }
543
- case ChangeType.Add: {
544
- if (event.addedCount > 0) {
545
- this._listViewAdapter.notifyItemRangeInserted(event.index, event.addedCount);
546
- }
547
- return;
548
- }
549
- case ChangeType.Update: {
550
- if (event.addedCount > 0) {
551
- this._listViewAdapter.notifyItemRangeChanged(event.index, event.addedCount);
552
- }
553
- return;
554
- }
555
- case ChangeType.Splice: {
556
- const added = event.addedCount;
557
- const removed = (event.removed && event.removed.length) || 0;
558
- if (added > 0 && added === removed) {
559
- if (!this.animateItemUpdate) {
560
- this._listViewAdapter.notifyItemRangeChanged(event.index, added);
561
- }
562
- else {
563
- this._listViewAdapter.notifyItemRangeRemoved(event.index, added);
564
- this._listViewAdapter.notifyItemRangeInserted(event.index, added);
565
- }
566
- }
567
- else {
568
- if (!this.animateItemUpdate) {
569
- if (added > removed) {
570
- if (removed > 0) {
571
- this._listViewAdapter.notifyItemRangeChanged(event.index, removed);
572
- }
573
- this._listViewAdapter.notifyItemRangeInserted(event.index + removed, added - removed);
574
- }
575
- else {
576
- if (added > 0) {
577
- this._listViewAdapter.notifyItemRangeChanged(event.index, added);
578
- }
579
- this._listViewAdapter.notifyItemRangeRemoved(event.index + added, removed - added);
580
- }
581
- }
582
- else {
583
- if (event.removed && event.removed.length > 0) {
584
- this._listViewAdapter.notifyItemRangeRemoved(event.index, event.removed.length);
585
- }
586
- if (event.addedCount > 0) {
587
- this._listViewAdapter.notifyItemRangeInserted(event.index, event.addedCount);
588
- }
589
- }
590
- }
591
- return;
592
- }
593
- }
594
- this._listViewAdapter.notifyDataSetChanged();
595
- }
596
- eachChild(callback) {
597
- this._viewHolders.forEach(({ view }, nativeView) => {
598
- if (view.parent instanceof CollectionView) {
599
- callback(view);
600
- }
601
- else {
602
- if (view.parent) {
603
- callback(view.parent);
604
- }
605
- }
606
- });
607
- }
608
- refreshVisibleItems() {
609
- const view = this.nativeViewProtected;
610
- if (!view) {
611
- return;
612
- }
613
- const ids = this._viewHolders
614
- .map((s) => s['position'])
615
- .filter((s) => s !== null)
616
- .sort((a, b) => a - b);
617
- this._listViewAdapter.notifyItemRangeChanged(ids[0], ids[ids.length - 1] - ids[0] + 1);
618
- }
619
- isItemAtIndexVisible(index) {
620
- const view = this.nativeViewProtected;
621
- if (!view) {
622
- return false;
623
- }
624
- const layoutManager = this.layoutManager;
625
- if (layoutManager['findFirstVisibleItemPosition']) {
626
- const first = layoutManager.findFirstVisibleItemPosition();
627
- const last = layoutManager.findLastVisibleItemPosition();
628
- return index >= first && index <= last;
629
- }
630
- return false;
631
- }
632
- refresh() {
633
- if (!this.nativeViewProtected) {
634
- return;
635
- }
636
- const view = this.nativeViewProtected;
637
- if (!this.isLoaded || !this._layedOut) {
638
- this._isDataDirty = true;
639
- return;
640
- }
641
- this._isDataDirty = false;
642
- this._lastLayoutKey = this._innerWidth + '_' + this._innerHeight;
643
- let adapter = this._listViewAdapter;
644
- if (!adapter) {
645
- adapter = this._listViewAdapter = this.createComposedAdapter(this.nativeViewProtected);
646
- adapter.setHasStableIds(!!this._itemIdGenerator);
647
- view.setAdapter(adapter);
648
- }
649
- else if (!view.getAdapter()) {
650
- view.setAdapter(adapter);
651
- }
652
- const layoutManager = view.getLayoutManager();
653
- if (layoutManager['setSpanCount']) {
654
- layoutManager['setSpanCount'](this.computeSpanCount());
655
- }
656
- adapter.notifyDataSetChanged();
657
- const args = {
658
- eventName: CollectionViewBase.dataPopulatedEvent,
659
- object: this
660
- };
661
- this.notify(args);
662
- }
663
- get scrollOffset() {
664
- const view = this.nativeViewProtected;
665
- if (!view) {
666
- return 0;
667
- }
668
- return (this.isHorizontal() ? view.computeHorizontalScrollOffset() : view.computeVerticalScrollOffset()) / layout.getDisplayDensity();
669
- }
670
- get verticalOffsetX() {
671
- const view = this.nativeViewProtected;
672
- if (!view) {
673
- return 0;
674
- }
675
- return view.computeHorizontalScrollOffset() / layout.getDisplayDensity();
676
- }
677
- get verticalOffsetY() {
678
- const view = this.nativeViewProtected;
679
- if (!view) {
680
- return 0;
681
- }
682
- return view.computeVerticalScrollOffset() / layout.getDisplayDensity();
683
- }
684
- scrollToIndex(index, animated = true) {
685
- if (!this.nativeViewProtected) {
686
- return;
687
- }
688
- if (animated) {
689
- this.nativeViewProtected.smoothScrollToPosition(index);
690
- }
691
- else {
692
- this.nativeViewProtected.scrollToPosition(index);
693
- }
694
- }
695
- _setPadding(newPadding) {
696
- const nativeView = this.nativeViewProtected;
697
- const padding = {
698
- top: nativeView.getPaddingTop(),
699
- right: nativeView.getPaddingRight(),
700
- bottom: nativeView.getPaddingBottom(),
701
- left: nativeView.getPaddingLeft()
702
- };
703
- const newValue = Object.assign(padding, newPadding);
704
- nativeView.setClipToPadding(false);
705
- nativeView.setPadding(newValue.left, newValue.top, newValue.right, newValue.bottom);
706
- this.updateInnerSize();
707
- }
708
- createComposedAdapter(recyclerView) {
709
- const adapter = new com.nativescript.collectionview.Adapter();
710
- adapter.adapterInterface = new com.nativescript.collectionview.AdapterInterface({
711
- getItemId: this.getItemId.bind(this),
712
- getItemViewType: this.getItemViewType.bind(this),
713
- getItemCount: this.getItemCount.bind(this),
714
- onCreateViewHolder: this.onCreateViewHolder.bind(this),
715
- onBindViewHolder: this.onBindViewHolder.bind(this),
716
- onViewRecycled: this.onViewRecycled.bind(this)
717
- });
718
- return adapter;
719
- }
720
- getItemCount() {
721
- return this.items ? this.items.length : 0;
722
- }
723
- getItem(i) {
724
- if (this.items && i < this.items.length) {
725
- return this.getItemAtIndex(i);
726
- }
727
- return null;
728
- }
729
- getItemId(i) {
730
- let id = -1;
731
- if (this._itemIdGenerator && this.items) {
732
- const item = this.getItemAtIndex(i);
733
- id = this._itemIdGenerator(item, i, this.items);
734
- }
735
- return long(id);
736
- }
737
- onItemIdGeneratorChanged(oldValue, newValue) {
738
- super.onItemIdGeneratorChanged(oldValue, newValue);
739
- if (this._listViewAdapter) {
740
- this._listViewAdapter.setHasStableIds(!!newValue);
741
- }
742
- }
743
- clearTemplateTypes() {
744
- this._currentNativeItemType = 0;
745
- this.templateTypeNumberString.clear();
746
- this.templateStringTypeNumber.clear();
747
- }
748
- getItemViewType(position) {
749
- let resultType = 0;
750
- let selectorType = 'default';
751
- if (this._itemTemplateSelector) {
752
- const selector = this._itemTemplateSelector;
753
- const dataItem = this.getItemAtIndex(position);
754
- if (dataItem) {
755
- selectorType = selector(dataItem, position, this.items);
756
- }
757
- }
758
- if (!this.templateTypeNumberString.has(selectorType)) {
759
- resultType = this._currentNativeItemType;
760
- this.templateTypeNumberString.set(selectorType, resultType);
761
- this.templateStringTypeNumber.set(resultType, selectorType);
762
- this._currentNativeItemType++;
763
- }
764
- else {
765
- resultType = this.templateTypeNumberString.get(selectorType);
766
- }
767
- return resultType;
768
- }
769
- disposeViewHolderViews() {
770
- this._viewHolders.forEach((v) => {
771
- v.view = null;
772
- v.clickListener = null;
773
- });
774
- this._viewHolders = new Array();
775
- this._viewHolderChildren.forEach((v) => this._removeViewCore(v));
776
- this._viewHolderChildren = new Array();
777
- }
778
- getKeyByValue(viewType) {
779
- return this.templateStringTypeNumber.get(viewType);
780
- }
781
- onCreateViewHolder(parent, viewType) {
782
- let view = this.getViewForViewType(ListViewViewTypes.ItemView, this.getKeyByValue(viewType));
783
- const isNonSync = view === undefined;
784
- if (isNonSync || view instanceof ProxyViewContainer) {
785
- const parentView = new ContentView();
786
- parentView.id = 'collectionViewHolder';
787
- view = parentView;
788
- }
789
- this._viewHolderChildren.push(view);
790
- this._addView(view);
791
- if (!CollectionViewCellHolder) {
792
- CollectionViewCellHolder = com.nativescript.collectionview.CollectionViewCellHolder;
793
- }
794
- const holder = new CollectionViewCellHolder(view.nativeView);
795
- const collectionView = this;
796
- const clickListener = new android.view.View.OnClickListener({
797
- onClick: () => {
798
- const position = holder.getAdapterPosition();
799
- collectionView.notify({
800
- eventName: CollectionViewBase.itemTapEvent,
801
- object: collectionView,
802
- index: position,
803
- item: collectionView.getItem(position),
804
- view: holder.view
805
- });
806
- }
807
- });
808
- view.nativeView.setOnClickListener(clickListener);
809
- holder.clickListener = clickListener;
810
- holder.view = view;
811
- const layoutParams = this._getViewLayoutParams();
812
- view.nativeView.setLayoutParams(layoutParams);
813
- if (isNonSync) {
814
- holder['defaultItemView'] = true;
815
- }
816
- this._viewHolders.push(holder);
817
- return holder;
818
- }
819
- onBindViewHolder(holder, position) {
820
- if (Trace.isEnabled()) {
821
- CLog(CLogTypes.log, 'onBindViewHolder', position);
822
- }
823
- let view = holder.view;
824
- const bindingContext = this._prepareItem(view, position);
825
- const isNonSync = holder['defaultItemView'] === true;
826
- holder['position'] = position;
827
- view = isNonSync ? view.content : view;
828
- const args = {
829
- eventName: CollectionViewBase.itemLoadingEvent,
830
- index: position,
831
- object: this,
832
- view,
833
- bindingContext,
834
- android: holder
835
- };
836
- this.notify(args);
837
- if (isNonSync && args.view !== view) {
838
- view = args.view;
839
- holder.view.content = args.view;
840
- }
841
- let width = this._effectiveColWidth;
842
- let height = this._effectiveRowHeight;
843
- if (this._getSpanSize) {
844
- const spanSize = this._getSpanSize(bindingContext, position);
845
- const horizontal = this.isHorizontal();
846
- if (horizontal) {
847
- height *= spanSize;
848
- }
849
- else {
850
- width *= spanSize;
851
- }
852
- }
853
- if (width || !view.width) {
854
- view.width = layout.toDeviceIndependentPixels(width);
855
- }
856
- if (height || !view.height) {
857
- view.height = layout.toDeviceIndependentPixels(height);
858
- }
859
- if (Trace.isEnabled()) {
860
- CLog(CLogTypes.log, 'onBindViewHolder done ', position);
861
- }
862
- }
863
- onViewRecycled(holder) {
864
- holder['position'] = null;
865
- }
866
- }
867
- CollectionView.DEFAULT_TEMPLATE_VIEW_TYPE = 0;
868
- CollectionView.CUSTOM_TEMPLATE_ITEM_TYPE = 1;
869
- __decorate([
870
- profile
871
- ], CollectionView.prototype, "createNativeView", null);
872
- __decorate([
873
- profile
874
- ], CollectionView.prototype, "initNativeView", null);
875
- __decorate([
876
- profile
877
- ], CollectionView.prototype, "refresh", null);
878
- __decorate([
879
- profile
880
- ], CollectionView.prototype, "onCreateViewHolder", null);
881
- __decorate([
882
- profile
883
- ], CollectionView.prototype, "onBindViewHolder", null);
884
- let CollectionViewCellHolder;
885
- let CollectionViewRecyclerView;
886
- itemViewCacheSizeProperty.register(CollectionViewBase);
887
- extraLayoutSpaceProperty.register(CollectionViewBase);
888
- nestedScrollingEnabledProperty.register(CollectionViewBase);
889
- //# sourceMappingURL=collectionview.android.js.map