/
CollectionView.cs
executable file
·1352 lines (1223 loc) · 48.9 KB
/
CollectionView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2021 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows.Input;
using System.ComponentModel;
using Tizen.NUI.BaseComponents;
using Tizen.NUI.Binding;
namespace Tizen.NUI.Components
{
/// <summary>
/// Selectable RecyclerView that presenting a collection of items with variable layouters.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public class CollectionView : RecyclerView
{
/// <summary>
/// Binding Property of selected item in single selection.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public static readonly BindableProperty SelectedItemProperty =
BindableProperty.Create(nameof(SelectedItem), typeof(object), typeof(CollectionView), null,
propertyChanged: (bindable, oldValue, newValue) =>
{
var colView = (CollectionView)bindable;
oldValue = colView.selectedItem;
colView.selectedItem = newValue;
var args = new SelectionChangedEventArgs(oldValue, newValue);
foreach (RecyclerViewItem item in colView.ContentContainer.Children.Where((item) => item is RecyclerViewItem))
{
if (item.BindingContext == null) continue;
if (item.BindingContext == oldValue) item.IsSelected = false;
else if (item.BindingContext == newValue) item.IsSelected = true;
}
SelectionPropertyChanged(colView, args);
},
defaultValueCreator: (bindable) =>
{
var colView = (CollectionView)bindable;
return colView.selectedItem;
});
/// <summary>
/// Binding Property of selected items list in multiple selection.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public static readonly BindableProperty SelectedItemsProperty =
BindableProperty.Create(nameof(SelectedItems), typeof(IList<object>), typeof(CollectionView), null,
propertyChanged: (bindable, oldValue, newValue) =>
{
var colView = (CollectionView)bindable;
var oldSelection = colView.selectedItems ?? selectEmpty;
//FIXME : CoerceSelectedItems calls only isCreatedByXaml
var newSelection = (SelectionList)CoerceSelectedItems(colView, newValue);
colView.selectedItems = newSelection;
colView.SelectedItemsPropertyChanged(oldSelection, newSelection);
},
defaultValueCreator: (bindable) =>
{
var colView = (CollectionView)bindable;
colView.selectedItems = colView.selectedItems ?? new SelectionList(colView);
return colView.selectedItems;
});
/// <summary>
/// Binding Property of selected items list in multiple selection.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public static readonly BindableProperty SelectionModeProperty =
BindableProperty.Create(nameof(SelectionMode), typeof(ItemSelectionMode), typeof(CollectionView), ItemSelectionMode.None,
propertyChanged: (bindable, oldValue, newValue) =>
{
var colView = (CollectionView)bindable;
oldValue = colView.selectionMode;
colView.selectionMode = (ItemSelectionMode)newValue;
SelectionModePropertyChanged(colView, oldValue, newValue);
},
defaultValueCreator: (bindable) =>
{
var colView = (CollectionView)bindable;
return colView.selectionMode;
});
/// <summary>
/// Binding Property of items data source.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(CollectionView), null,
propertyChanged: (bindable, oldValue, newValue) =>
{
var colView = (CollectionView)bindable;
oldValue = colView.itemsSource;
if (oldValue != null)
{
// Clearing old data!
if (oldValue is INotifyCollectionChanged prevNotifyCollectionChanged)
{
prevNotifyCollectionChanged.CollectionChanged -= colView.CollectionChanged;
}
if (colView.selectedItem != null) colView.selectedItem = null;
colView.selectedItems?.Clear();
}
colView.itemsSource = (IEnumerable)newValue;
if (newValue == null)
{
colView.InternalItemSource?.Dispose();
colView.InternalItemSource = null;
colView.itemsLayouter?.Clear();
colView.ClearCache();
return;
}
if (newValue is INotifyCollectionChanged newNotifyCollectionChanged)
{
newNotifyCollectionChanged.CollectionChanged += colView.CollectionChanged;
}
colView.InternalItemSource?.Dispose();
colView.InternalItemSource = ItemsSourceFactory.Create(colView);
if (colView.itemsLayouter == null) return;
colView.needInitalizeLayouter = true;
colView.Init();
},
defaultValueCreator: (bindable) =>
{
var colView = (CollectionView)bindable;
return colView.itemsSource;
});
private static readonly IList<object> selectEmpty = new List<object>(0);
private DataTemplate itemTemplate = null;
private IEnumerable itemsSource = null;
private ItemsLayouter itemsLayouter = null;
private DataTemplate groupHeaderTemplate;
private DataTemplate groupFooterTemplate;
private bool isGrouped;
private bool wasRelayouted = false;
private bool needInitalizeLayouter = false;
private object selectedItem;
private SelectionList selectedItems;
private bool suppressSelectionChangeNotification;
private ItemSelectionMode selectionMode = ItemSelectionMode.None;
private RecyclerViewItem header;
private RecyclerViewItem footer;
private View focusedView;
private int prevFocusedDataIndex = 0;
private List<RecyclerViewItem> recycleGroupHeaderCache { get; } = new List<RecyclerViewItem>();
private List<RecyclerViewItem> recycleGroupFooterCache { get; } = new List<RecyclerViewItem>();
private bool delayedScrollTo;
private (float position, bool anim) delayedScrollToParam;
private bool delayedIndexScrollTo;
private (int index, bool anim, ItemScrollTo scrollTo) delayedIndexScrollToParam;
/// <summary>
/// Base constructor.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public CollectionView() : base()
{
FocusGroup = true;
SetKeyboardNavigationSupport(true);
}
/// <summary>
/// Base constructor with ItemsSource
/// </summary>
/// <param name="itemsSource">item's data source</param>
/// <since_tizen> 9 </since_tizen>
public CollectionView(IEnumerable itemsSource) : this()
{
ItemsSource = itemsSource;
}
/// <summary>
/// Base constructor with ItemsSource, ItemsLayouter and ItemTemplate
/// </summary>
/// <param name="itemsSource">item's data source</param>
/// <param name="layouter">item's layout manager</param>
/// <param name="template">item's view template with data bindings</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public CollectionView(IEnumerable itemsSource, ItemsLayouter layouter, DataTemplate template) : this()
{
ItemsSource = itemsSource;
ItemTemplate = template;
ItemsLayouter = layouter;
}
/// <summary>
/// Event of Selection changed.
/// previous selection list and current selection will be provided.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
/// <summary>
/// Align item in the viewport when ScrollTo() calls.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public enum ItemScrollTo
{
/// <summary>
/// Scroll to show item in nearest viewport on scroll direction.
/// item is above the scroll viewport, item will be came into front,
/// item is under the scroll viewport, item will be came into end,
/// item is in the scroll viewport, no scroll.
/// </summary>
/// <since_tizen> 9 </since_tizen>
Nearest,
/// <summary>
/// Scroll to show item in start of the viewport.
/// </summary>
/// <since_tizen> 9 </since_tizen>
Start,
/// <summary>
/// Scroll to show item in center of the viewport.
/// </summary>
/// <since_tizen> 9 </since_tizen>
Center,
/// <summary>
/// Scroll to show item in end of the viewport.
/// </summary>
/// <since_tizen> 9 </since_tizen>
End,
}
/// <summary>
/// Item's source data in IEnumerable.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public override IEnumerable ItemsSource
{
get => (IEnumerable)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
/// <summary>
/// DataTemplate for items.
/// Create visual contents and binding properties.
/// return object type is restricted RecyclerViewItem.
/// <seealso cref="Tizen.NUI.Binding.DataTemplate" />
/// </summary>
/// <since_tizen> 9 </since_tizen>
public override DataTemplate ItemTemplate
{
get
{
return itemTemplate;
}
set
{
itemTemplate = value;
if (value == null)
{
return;
}
needInitalizeLayouter = true;
Init();
}
}
/// <summary>
/// Items Layouter.
/// Layouting items on the scroll ContentContainer.
/// <seealso cref="ItemsLayouter" />
/// <seealso cref="LinearLayouter" />
/// <seealso cref="GridLayouter" />
/// </summary>
/// <since_tizen> 9 </since_tizen>
public virtual ItemsLayouter ItemsLayouter
{
get
{
return itemsLayouter;
}
set
{
itemsLayouter?.Clear();
ClearCache();
itemsLayouter = value;
base.InternalItemsLayouter = itemsLayouter;
if (value == null)
{
needInitalizeLayouter = false;
return;
}
needInitalizeLayouter = true;
var styleName = "Tizen.NUI.Components." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
if (layouterStyle != null)
{
itemsLayouter.Padding = new Extents(layouterStyle.Padding);
}
Init();
}
}
/// <summary>
/// Scrolling direction to display items layout.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public new Direction ScrollingDirection
{
get
{
return base.ScrollingDirection;
}
set
{
if (base.ScrollingDirection != value)
{
base.ScrollingDirection = value;
needInitalizeLayouter = true;
Init();
}
}
}
/// <summary>
/// Selected item in single selection.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public object SelectedItem
{
get => GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
/// <summary>
/// Selected items list in multiple selection.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public IList<object> SelectedItems
{
get => (IList<object>)GetValue(SelectedItemsProperty);
// set => SetValue(SelectedItemsProperty, new SelectionList(this, value));
}
/// <summary>
/// Selection mode to handle items selection. See ItemSelectionMode for details.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public ItemSelectionMode SelectionMode
{
get => (ItemSelectionMode)GetValue(SelectionModeProperty);
set => SetValue(SelectionModeProperty, value);
}
/// <summary>
/// Command of selection changed.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public ICommand SelectionChangedCommand { set; get; }
/// <summary>
/// Command parameter of selection changed.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public object SelectionChangedCommandParameter { set; get; }
/// <summary>
/// Header item placed in top-most position.
/// </summary>
/// <remarks>Please note that, internal index will be increased by header.</remarks>
/// <since_tizen> 9 </since_tizen>
public RecyclerViewItem Header
{
get => header;
set
{
if (header != null)
{
//ContentContainer.Remove(header);
Utility.Dispose(header);
}
if (value != null)
{
value.Index = 0;
value.ParentItemsView = this;
value.IsHeader = true;
ContentContainer.Add(value);
}
header = value;
if (InternalItemSource != null)
{
InternalItemSource.HasHeader = (value != null);
}
needInitalizeLayouter = true;
Init();
}
}
/// <summary>
/// Footer item placed in bottom-most position.
/// </summary>
/// <remarks>Please note that, internal index will be increased by footer.</remarks>
/// <since_tizen> 9 </since_tizen>
public RecyclerViewItem Footer
{
get => footer;
set
{
if (footer != null)
{
//ContentContainer.Remove(footer);
Utility.Dispose(footer);
}
if (value != null)
{
value.Index = InternalItemSource?.Count ?? 0;
value.ParentItemsView = this;
value.IsFooter = true;
ContentContainer.Add(value);
}
footer = value;
if (InternalItemSource != null)
{
InternalItemSource.HasFooter = (value != null);
}
needInitalizeLayouter = true;
Init();
}
}
/// <summary>
/// Enable groupable view.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsGrouped
{
get => isGrouped;
set
{
isGrouped = value;
needInitalizeLayouter = true;
//Need to re-intialize Internal Item Source.
if (InternalItemSource != null)
{
InternalItemSource.Dispose();
InternalItemSource = null;
}
if (ItemsSource != null)
InternalItemSource = ItemsSourceFactory.Create(this);
Init();
}
}
/// <summary>
/// DataTemplate of group header.
/// </summary>
/// <remarks>Please note that, internal index will be increased by group header.
/// GroupHeaderTemplate is essential for groupable view.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public DataTemplate GroupHeaderTemplate
{
get
{
return groupHeaderTemplate;
}
set
{
groupHeaderTemplate = value;
needInitalizeLayouter = true;
//Need to re-intialize Internal Item Source.
if (InternalItemSource != null)
{
InternalItemSource.Dispose();
InternalItemSource = null;
}
if (ItemsSource != null)
InternalItemSource = ItemsSourceFactory.Create(this);
Init();
}
}
/// <summary>
/// DataTemplate of group footer. Group feature is not supported yet.
/// </summary>
/// <remarks>Please note that, internal index will be increased by group footer.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public DataTemplate GroupFooterTemplate
{
get
{
return groupFooterTemplate;
}
set
{
groupFooterTemplate = value;
needInitalizeLayouter = true;
//Need to re-intialize Internal Item Source.
if (InternalItemSource != null)
{
InternalItemSource.Dispose();
InternalItemSource = null;
}
if (ItemsSource != null)
InternalItemSource = ItemsSourceFactory.Create(this);
Init();
}
}
/// <summary>
/// Internal encapsulated items data source.
/// </summary>
internal new IGroupableItemSource InternalItemSource
{
get
{
return (base.InternalItemSource as IGroupableItemSource);
}
set
{
base.InternalItemSource = value;
}
}
/// <summary>
/// Size strategy of measuring scroll content. see details in ItemSizingStrategy.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal ItemSizingStrategy SizingStrategy { get; set; }
/// <inheritdoc/>
/// <since_tizen> 9 </since_tizen>
public override void OnRelayout(Vector2 size, RelayoutContainer container)
{
base.OnRelayout(size, container);
wasRelayouted = true;
if (needInitalizeLayouter) Init();
}
/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
public override void NotifyDataSetChanged()
{
if (selectedItem != null)
{
selectedItem = null;
}
if (selectedItems != null)
{
selectedItems.Clear();
}
base.NotifyDataSetChanged();
}
/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
{
View nextFocusedView = null;
if (focusedView == null)
{
// If focusedView is null, find child which has previous data index
if (ContentContainer.Children.Count > 0 && InternalItemSource.Count > 0)
{
for (int i = 0; i < ContentContainer.Children.Count; i++)
{
RecyclerViewItem item = Children[i] as RecyclerViewItem;
if (item?.Index == prevFocusedDataIndex)
{
nextFocusedView = item;
break;
}
}
}
}
else
{
// If this is not first focus, request next focus to Layouter
nextFocusedView = ItemsLayouter?.RequestNextFocusableView(currentFocusedView, direction, loopEnabled);
}
if (nextFocusedView != null)
{
// Check next focused view is inside of visible area.
// If it is not, move scroll position to make it visible.
Position scrollPosition = ContentContainer.CurrentPosition;
float targetPosition = -(ScrollingDirection == Direction.Horizontal ? scrollPosition.X : scrollPosition.Y);
float left = nextFocusedView.Position.X;
float right = nextFocusedView.Position.X + nextFocusedView.Size.Width;
float top = nextFocusedView.Position.Y;
float bottom = nextFocusedView.Position.Y + nextFocusedView.Size.Height;
float visibleRectangleLeft = -scrollPosition.X;
float visibleRectangleRight = -scrollPosition.X + Size.Width;
float visibleRectangleTop = -scrollPosition.Y;
float visibleRectangleBottom = -scrollPosition.Y + Size.Height;
if (ScrollingDirection == Direction.Horizontal)
{
if ((direction == View.FocusDirection.Left || direction == View.FocusDirection.Up) && left < visibleRectangleLeft)
{
targetPosition = left;
}
else if ((direction == View.FocusDirection.Right || direction == View.FocusDirection.Down) && right > visibleRectangleRight)
{
targetPosition = right - Size.Width;
}
}
else
{
if ((direction == View.FocusDirection.Up || direction == View.FocusDirection.Left) && top < visibleRectangleTop)
{
targetPosition = top;
}
else if ((direction == View.FocusDirection.Down || direction == View.FocusDirection.Right) && bottom > visibleRectangleBottom)
{
targetPosition = bottom - Size.Height;
}
}
focusedView = nextFocusedView;
prevFocusedDataIndex = (nextFocusedView as RecyclerViewItem)?.Index ?? -1;
ScrollTo(targetPosition, true);
}
else
{
// If nextView is null, it means that we should move focus to outside of Control.
// Return FocusableView depending on direction.
switch (direction)
{
case View.FocusDirection.Left:
{
nextFocusedView = LeftFocusableView;
break;
}
case View.FocusDirection.Right:
{
nextFocusedView = RightFocusableView;
break;
}
case View.FocusDirection.Up:
{
nextFocusedView = UpFocusableView;
break;
}
case View.FocusDirection.Down:
{
nextFocusedView = DownFocusableView;
break;
}
}
if (nextFocusedView != null)
{
focusedView = null;
}
else
{
//If FocusableView doesn't exist, not move focus.
nextFocusedView = focusedView;
}
}
return nextFocusedView;
}
/// <summary>
/// Update selected items list in multiple selection.
/// </summary>
/// <param name="newSelection">updated selection list by user</param>
/// <since_tizen> 9 </since_tizen>
public void UpdateSelectedItems(IList<object> newSelection)
{
var oldSelection = new List<object>(SelectedItems);
suppressSelectionChangeNotification = true;
SelectedItems.Clear();
if (newSelection?.Count > 0)
{
for (int n = 0; n < newSelection.Count; n++)
{
SelectedItems.Add(newSelection[n]);
}
}
suppressSelectionChangeNotification = false;
SelectedItemsPropertyChanged(oldSelection, newSelection);
}
/// <summary>
/// Scroll to specific position with or without animation.
/// </summary>
/// <param name="position">Destination.</param>
/// <param name="animate">Scroll with or without animation</param>
/// <since_tizen> 9 </since_tizen>
public new void ScrollTo(float position, bool animate)
{
if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
if ((InternalItemSource == null) || needInitalizeLayouter)
{
delayedScrollTo = true;
delayedScrollToParam = (position, animate);
return;
}
base.ScrollTo(position, animate);
}
/// <summary>
/// Scrolls to the item at the specified index.
/// </summary>
/// <param name="index">Index of item.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public new void ScrollToIndex(int index)
{
ScrollTo(index, true, ItemScrollTo.Start);
}
/// <summary>
/// Scroll to specific item's aligned position with or without animation.
/// </summary>
/// <param name="index">Target item index of dataset.</param>
/// <param name="animate">Boolean flag of animation.</param>
/// <param name="align">Align state of item. See details in <see cref="ItemScrollTo"/>.</param>
/// <since_tizen> 9 </since_tizen>
public virtual void ScrollTo(int index, bool animate = false, ItemScrollTo align = ItemScrollTo.Nearest)
{
if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
if ((InternalItemSource == null) || needInitalizeLayouter)
{
delayedIndexScrollTo = true;
delayedIndexScrollToParam = (index, animate, align);
return;
}
if (index < 0 || index >= InternalItemSource.Count)
{
throw new Exception("index is out of boundary. index should be a value between (0, " + InternalItemSource.Count.ToString() + ").");
}
float scrollPos, curPos, curSize, curItemSize;
(float x, float y) = ItemsLayouter.GetItemPosition(index);
(float width, float height) = ItemsLayouter.GetItemSize(index);
if (ScrollingDirection == Direction.Horizontal)
{
scrollPos = x;
curPos = ScrollPosition.X;
curSize = Size.Width;
curItemSize = width;
}
else
{
scrollPos = y;
curPos = ScrollPosition.Y;
curSize = Size.Height;
curItemSize = height;
}
//Console.WriteLine("[NUI] ScrollTo [{0}:{1}], curPos{2}, itemPos{3}, curSize{4}, itemSize{5}", InternalItemSource.GetPosition(item), align, curPos, scrollPos, curSize, curItemSize);
switch (align)
{
case ItemScrollTo.Start:
//nothing necessary.
break;
case ItemScrollTo.Center:
scrollPos = scrollPos - (curSize / 2) + (curItemSize / 2);
break;
case ItemScrollTo.End:
scrollPos = scrollPos - curSize + curItemSize;
break;
case ItemScrollTo.Nearest:
if (scrollPos < curPos - curItemSize)
{
// item is placed before the current screen. scrollTo.Top
}
else if (scrollPos >= curPos + curSize + curItemSize)
{
// item is placed after the current screen. scrollTo.End
scrollPos = scrollPos - curSize + curItemSize;
}
else
{
// item is in the scroller. ScrollTo() is ignored.
return;
}
break;
}
//Console.WriteLine("[NUI] ScrollTo [{0}]-------------------", scrollPos);
base.ScrollTo(scrollPos, animate);
}
/// <summary>
/// Apply style to CollectionView
/// </summary>
/// <param name="viewStyle">The style to apply.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public override void ApplyStyle(ViewStyle viewStyle)
{
base.ApplyStyle(viewStyle);
if (viewStyle != null)
{
//Extension = RecyclerViewItemStyle.CreateExtension();
}
if (itemsLayouter != null)
{
string styleName = "Tizen.NUI.Compoenents." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
if (layouterStyle != null)
itemsLayouter.Padding = new Extents(layouterStyle.Padding);
}
}
/// <summary>
/// Scroll to specified item
/// </summary>
/// <remarks>
/// Make sure that the item that is about to receive the accessibility highlight is visible.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
protected override bool AccessibilityScrollToChild(View child)
{
foreach (RecyclerViewItem item in ContentContainer.Children.Where((item) => item is RecyclerViewItem))
{
if (child == item)
{
ScrollToIndex(item.Index);
return true;
}
}
return false;
}
// Realize and Decorate the item.
internal override RecyclerViewItem RealizeItem(int index)
{
RecyclerViewItem item;
if (index == 0 && Header != null)
{
Header.Show();
return Header;
}
if (index == InternalItemSource.Count - 1 && Footer != null)
{
Footer.Show();
return Footer;
}
if (isGrouped)
{
var context = InternalItemSource.GetItem(index);
if (InternalItemSource.IsGroupHeader(index))
{
DataTemplate templ = (groupHeaderTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupHeaderTemplate;
RecyclerViewItem groupHeader = PopRecycleGroupCache(templ, true);
if (groupHeader == null)
{
groupHeader = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupHeaderTemplate, context, this);
groupHeader.Template = templ;
groupHeader.isGroupHeader = true;
groupHeader.isGroupFooter = false;
ContentContainer.Add(groupHeader);
}
if (groupHeader != null)
{
groupHeader.ParentItemsView = this;
groupHeader.Index = index;
groupHeader.ParentGroup = context;
groupHeader.BindingContext = context;
}
//group selection?
item = groupHeader;
}
else if (InternalItemSource.IsGroupFooter(index))
{
DataTemplate templ = (groupFooterTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupFooterTemplate;
RecyclerViewItem groupFooter = PopRecycleGroupCache(templ, false);
if (groupFooter == null)
{
groupFooter = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupFooterTemplate, context, this);
groupFooter.Template = templ;
groupFooter.isGroupHeader = false;
groupFooter.isGroupFooter = true;
ContentContainer.Add(groupFooter);
}
if (groupFooter != null)
{
groupFooter.ParentItemsView = this;
groupFooter.Index = index;
groupFooter.ParentGroup = context;
groupFooter.BindingContext = context;
}
//group selection?
item = groupFooter;
}
else
{
item = base.RealizeItem(index);
if (item == null)
throw new Exception("Item realize failed by Null content return.");
item.ParentGroup = InternalItemSource.GetGroupParent(index);
}
}
else
{
item = base.RealizeItem(index);
}
if (item == null)
throw new Exception("Item realize failed by Null content return.");
switch (SelectionMode)
{
case ItemSelectionMode.Single:
case ItemSelectionMode.SingleAlways:
if (item.BindingContext != null && item.BindingContext == SelectedItem)
{
item.IsSelected = true;
}
break;
case ItemSelectionMode.Multiple:
if ((item.BindingContext != null) && (SelectedItems?.Contains(item.BindingContext) ?? false))
{
item.IsSelected = true;
}
break;
case ItemSelectionMode.None:
item.IsSelectable = false;
break;
}
return item;
}
// Unrealize and caching the item.
internal override void UnrealizeItem(RecyclerViewItem item, bool recycle = true)
{
if (item == null) return;
if (item == Header)
{
item?.Hide();
return;
}
if (item == Footer)
{
item.Hide();
return;
}
if (item.isGroupHeader || item.isGroupFooter)
{
item.Index = -1;
item.ParentItemsView = null;
item.BindingContext = null;
item.IsPressed = false;
item.IsSelected = false;
item.IsEnabled = true;
item.UpdateState();
//item.Relayout -= OnItemRelayout;
if (!recycle || !PushRecycleGroupCache(item))
Utility.Dispose(item);
return;
}