1 /* Copyright (c) 2021 Samsung Electronics Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Collections.Specialized;
21 using System.Windows.Input;
22 using System.ComponentModel;
23 using Tizen.NUI.BaseComponents;
24 using Tizen.NUI.Binding;
26 namespace Tizen.NUI.Components
29 /// Selectable RecyclerView that presenting a collection of items with variable layouters.
31 /// <since_tizen> 9 </since_tizen>
32 public partial class CollectionView : RecyclerView
35 /// Binding Property of selected item in single selection.
37 /// <since_tizen> 9 </since_tizen>
38 public static readonly BindableProperty SelectedItemProperty =
39 BindableProperty.Create(nameof(SelectedItem), typeof(object), typeof(CollectionView), null,
40 propertyChanged: (bindable, oldValue, newValue) =>
42 var colView = (CollectionView)bindable;
43 oldValue = colView.selectedItem;
44 colView.selectedItem = newValue;
45 var args = new SelectionChangedEventArgs(oldValue, newValue);
47 foreach (RecyclerViewItem item in colView.ContentContainer.Children.Where((item) => item is RecyclerViewItem))
49 if (item.BindingContext == null) continue;
50 if (item.BindingContext == oldValue) item.IsSelected = false;
51 else if (item.BindingContext == newValue) item.IsSelected = true;
54 SelectionPropertyChanged(colView, args);
56 defaultValueCreator: (bindable) =>
58 var colView = (CollectionView)bindable;
59 return colView.selectedItem;
63 /// Binding Property of selected items list in multiple selection.
65 /// <since_tizen> 9 </since_tizen>
66 public static readonly BindableProperty SelectedItemsProperty =
67 BindableProperty.Create(nameof(SelectedItems), typeof(IList<object>), typeof(CollectionView), null,
68 propertyChanged: (bindable, oldValue, newValue) =>
70 var colView = (CollectionView)bindable;
71 var oldSelection = colView.selectedItems ?? selectEmpty;
72 //FIXME : CoerceSelectedItems calls only isCreatedByXaml
73 var newSelection = (SelectionList)CoerceSelectedItems(colView, newValue);
74 colView.selectedItems = newSelection;
75 colView.SelectedItemsPropertyChanged(oldSelection, newSelection);
77 defaultValueCreator: (bindable) =>
79 var colView = (CollectionView)bindable;
80 colView.selectedItems = colView.selectedItems ?? new SelectionList(colView);
81 return colView.selectedItems;
85 /// Binding Property of selected items list in multiple selection.
87 /// <since_tizen> 9 </since_tizen>
88 public static readonly BindableProperty SelectionModeProperty =
89 BindableProperty.Create(nameof(SelectionMode), typeof(ItemSelectionMode), typeof(CollectionView), ItemSelectionMode.None,
90 propertyChanged: (bindable, oldValue, newValue) =>
92 var colView = (CollectionView)bindable;
93 oldValue = colView.selectionMode;
94 colView.selectionMode = (ItemSelectionMode)newValue;
95 SelectionModePropertyChanged(colView, oldValue, newValue);
97 defaultValueCreator: (bindable) =>
99 var colView = (CollectionView)bindable;
100 return colView.selectionMode;
104 private static readonly IList<object> selectEmpty = new List<object>(0);
105 private DataTemplate itemTemplate = null;
106 private IEnumerable itemsSource = null;
107 private ItemsLayouter itemsLayouter = null;
108 private DataTemplate groupHeaderTemplate;
109 private DataTemplate groupFooterTemplate;
110 private bool isGrouped;
111 private bool wasRelayouted = false;
112 private bool needInitalizeLayouter = false;
113 private object selectedItem;
114 private SelectionList selectedItems;
115 private bool suppressSelectionChangeNotification;
116 private ItemSelectionMode selectionMode = ItemSelectionMode.None;
117 private RecyclerViewItem header;
118 private RecyclerViewItem footer;
119 private List<RecyclerViewItem> recycleGroupHeaderCache { get; } = new List<RecyclerViewItem>();
120 private List<RecyclerViewItem> recycleGroupFooterCache { get; } = new List<RecyclerViewItem>();
121 private bool delayedScrollTo;
122 private (float position, bool anim) delayedScrollToParam;
124 private bool delayedIndexScrollTo;
125 private (int index, bool anim, ItemScrollTo scrollTo) delayedIndexScrollToParam;
128 /// Base constructor.
130 /// <since_tizen> 9 </since_tizen>
131 public CollectionView() : base()
134 SetKeyboardNavigationSupport(true);
138 /// Base constructor with ItemsSource
140 /// <param name="itemsSource">item's data source</param>
141 /// <since_tizen> 9 </since_tizen>
142 public CollectionView(IEnumerable itemsSource) : this()
144 ItemsSource = itemsSource;
148 /// Base constructor with ItemsSource, ItemsLayouter and ItemTemplate
150 /// <param name="itemsSource">item's data source</param>
151 /// <param name="layouter">item's layout manager</param>
152 /// <param name="template">item's view template with data bindings</param>
153 [EditorBrowsable(EditorBrowsableState.Never)]
154 public CollectionView(IEnumerable itemsSource, ItemsLayouter layouter, DataTemplate template) : this()
156 ItemsSource = itemsSource;
157 ItemTemplate = template;
158 ItemsLayouter = layouter;
162 /// Event of Selection changed.
163 /// previous selection list and current selection will be provided.
165 /// <since_tizen> 9 </since_tizen>
166 public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
169 /// Align item in the viewport when ScrollTo() calls.
171 /// <since_tizen> 9 </since_tizen>
172 public enum ItemScrollTo
175 /// Scroll to show item in nearest viewport on scroll direction.
176 /// item is above the scroll viewport, item will be came into front,
177 /// item is under the scroll viewport, item will be came into end,
178 /// item is in the scroll viewport, no scroll.
180 /// <since_tizen> 9 </since_tizen>
183 /// Scroll to show item in start of the viewport.
185 /// <since_tizen> 9 </since_tizen>
188 /// Scroll to show item in center of the viewport.
190 /// <since_tizen> 9 </since_tizen>
193 /// Scroll to show item in end of the viewport.
195 /// <since_tizen> 9 </since_tizen>
200 /// Item's source data in IEnumerable.
202 /// <since_tizen> 9 </since_tizen>
203 public override IEnumerable ItemsSource
205 get => (IEnumerable)GetValue(RecyclerView.ItemsSourceProperty);
206 set => SetValue(RecyclerView.ItemsSourceProperty, value);
209 internal override IEnumerable InternalItemsSource
217 if (itemsSource != null)
219 // Clearing old data!
220 if (itemsSource is INotifyCollectionChanged prevNotifyCollectionChanged)
222 prevNotifyCollectionChanged.CollectionChanged -= CollectionChanged;
224 if (selectedItem != null) selectedItem = null;
225 selectedItems?.Clear();
228 itemsSource = (IEnumerable)value;
230 if (itemsSource == null)
232 InternalItemSource?.Dispose();
233 InternalItemSource = null;
234 itemsLayouter?.Clear();
238 if (itemsSource is INotifyCollectionChanged newNotifyCollectionChanged)
240 newNotifyCollectionChanged.CollectionChanged += CollectionChanged;
243 InternalItemSource?.Dispose();
244 InternalItemSource = ItemsSourceFactory.Create(this);
246 if (itemsLayouter == null) return;
248 needInitalizeLayouter = true;
255 /// DataTemplate for items.
256 /// Create visual contents and binding properties.
257 /// return object type is restricted RecyclerViewItem.
258 /// <seealso cref="Tizen.NUI.Binding.DataTemplate" />
260 /// <since_tizen> 9 </since_tizen>
261 public override DataTemplate ItemTemplate
265 return GetValue(ItemTemplateProperty) as DataTemplate;
269 SetValue(ItemTemplateProperty, value);
270 NotifyPropertyChanged();
273 internal override DataTemplate InternalItemTemplate
281 itemTemplate = value;
287 needInitalizeLayouter = true;
294 /// Layouting items on the scroll ContentContainer.
295 /// <seealso cref="ItemsLayouter" />
296 /// <seealso cref="LinearLayouter" />
297 /// <seealso cref="GridLayouter" />
299 /// <since_tizen> 9 </since_tizen>
300 public virtual ItemsLayouter ItemsLayouter
304 return GetValue(ItemsLayouterProperty) as ItemsLayouter;
308 SetValue(ItemsLayouterProperty, value);
309 NotifyPropertyChanged();
315 [EditorBrowsable(EditorBrowsableState.Never)]
316 protected override ItemsLayouter InternalItemsLayouter
320 return itemsLayouter;
324 itemsLayouter?.Clear();
327 itemsLayouter = value;
328 base.InternalItemsLayouter = itemsLayouter;
331 needInitalizeLayouter = false;
335 needInitalizeLayouter = true;
337 var styleName = "Tizen.NUI.Components." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
338 ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
339 if (layouterStyle != null)
341 itemsLayouter.Padding = new Extents(layouterStyle.Padding);
348 /// Scrolling direction to display items layout.
350 /// <since_tizen> 9 </since_tizen>
351 public new Direction ScrollingDirection
355 return (Direction)GetValue(ScrollingDirectionProperty);
359 SetValue(ScrollingDirectionProperty, value);
360 NotifyPropertyChanged();
363 private Direction InternalScrollingDirection
367 return base.ScrollingDirection;
371 if (base.ScrollingDirection != value)
373 base.ScrollingDirection = value;
374 needInitalizeLayouter = true;
381 /// Selected item in single selection.
383 /// <since_tizen> 9 </since_tizen>
384 public object SelectedItem
386 get => GetValue(SelectedItemProperty);
387 set => SetValue(SelectedItemProperty, value);
391 /// Selected items list in multiple selection.
393 /// <since_tizen> 9 </since_tizen>
394 public IList<object> SelectedItems
396 get => (IList<object>)GetValue(SelectedItemsProperty);
397 // set => SetValue(SelectedItemsProperty, new SelectionList(this, value));
401 /// Selection mode to handle items selection. See ItemSelectionMode for details.
403 /// <since_tizen> 9 </since_tizen>
404 public ItemSelectionMode SelectionMode
406 get => (ItemSelectionMode)GetValue(SelectionModeProperty);
407 set => SetValue(SelectionModeProperty, value);
411 /// Command of selection changed.
413 [EditorBrowsable(EditorBrowsableState.Never)]
414 public ICommand SelectionChangedCommand
418 return GetValue(SelectionChangedCommandProperty) as ICommand;
422 SetValue(SelectionChangedCommandProperty, value);
423 NotifyPropertyChanged();
426 private ICommand InternalSelectionChangedCommand { set; get; }
429 /// Command parameter of selection changed.
431 [EditorBrowsable(EditorBrowsableState.Never)]
432 public object SelectionChangedCommandParameter
436 return GetValue(SelectionChangedCommandParameterProperty);
440 SetValue(SelectionChangedCommandParameterProperty, value);
441 NotifyPropertyChanged();
444 private object InternalSelectionChangedCommandParameter { set; get; }
447 /// Header item placed in top-most position.
449 /// <remarks>Please note that, internal index will be increased by header.</remarks>
450 /// <since_tizen> 9 </since_tizen>
451 public RecyclerViewItem Header
455 return GetValue(HeaderProperty) as RecyclerViewItem;
459 SetValue(HeaderProperty, value);
460 NotifyPropertyChanged();
463 private RecyclerViewItem InternalHeader
470 //ContentContainer.Remove(header);
471 Utility.Dispose(header);
476 value.ParentItemsView = this;
477 value.IsHeader = true;
478 ContentContainer.Add(value);
481 if (InternalItemSource != null)
483 InternalItemSource.HasHeader = (value != null);
485 needInitalizeLayouter = true;
491 /// Footer item placed in bottom-most position.
493 /// <remarks>Please note that, internal index will be increased by footer.</remarks>
494 /// <since_tizen> 9 </since_tizen>
495 public RecyclerViewItem Footer
499 return GetValue(FooterProperty) as RecyclerViewItem;
503 SetValue(FooterProperty, value);
504 NotifyPropertyChanged();
507 private RecyclerViewItem InternalFooter
514 //ContentContainer.Remove(footer);
515 Utility.Dispose(footer);
519 value.Index = InternalItemSource?.Count ?? 0;
520 value.ParentItemsView = this;
521 value.IsFooter = true;
522 ContentContainer.Add(value);
525 if (InternalItemSource != null)
527 InternalItemSource.HasFooter = (value != null);
529 needInitalizeLayouter = true;
535 /// Enable groupable view.
537 [EditorBrowsable(EditorBrowsableState.Never)]
538 public bool IsGrouped
542 return (bool)GetValue(IsGroupedProperty);
546 SetValue(IsGroupedProperty, value);
547 NotifyPropertyChanged();
550 private bool InternalIsGrouped
556 needInitalizeLayouter = true;
557 //Need to re-intialize Internal Item Source.
558 if (InternalItemSource != null)
560 InternalItemSource.Dispose();
561 InternalItemSource = null;
563 if (ItemsSource != null)
564 InternalItemSource = ItemsSourceFactory.Create(this);
570 /// DataTemplate of group header.
572 /// <remarks>Please note that, internal index will be increased by group header.
573 /// GroupHeaderTemplate is essential for groupable view.</remarks>
574 [EditorBrowsable(EditorBrowsableState.Never)]
575 public DataTemplate GroupHeaderTemplate
579 return GetValue(GroupHeaderTemplateProperty) as DataTemplate;
583 SetValue(GroupHeaderTemplateProperty, value);
584 NotifyPropertyChanged();
587 private DataTemplate InternalGroupHeaderTemplate
591 return groupHeaderTemplate;
595 groupHeaderTemplate = value;
596 needInitalizeLayouter = true;
597 //Need to re-intialize Internal Item Source.
598 if (InternalItemSource != null)
600 InternalItemSource.Dispose();
601 InternalItemSource = null;
603 if (ItemsSource != null)
604 InternalItemSource = ItemsSourceFactory.Create(this);
610 /// DataTemplate of group footer. Group feature is not supported yet.
612 /// <remarks>Please note that, internal index will be increased by group footer.</remarks>
613 [EditorBrowsable(EditorBrowsableState.Never)]
614 public DataTemplate GroupFooterTemplate
618 return GetValue(GroupFooterTemplateProperty) as DataTemplate;
622 SetValue(GroupFooterTemplateProperty, value);
623 NotifyPropertyChanged();
626 private DataTemplate InternalGroupFooterTemplate
630 return groupFooterTemplate;
634 groupFooterTemplate = value;
635 needInitalizeLayouter = true;
636 //Need to re-intialize Internal Item Source.
637 if (InternalItemSource != null)
639 InternalItemSource.Dispose();
640 InternalItemSource = null;
642 if (ItemsSource != null)
643 InternalItemSource = ItemsSourceFactory.Create(this);
649 /// Internal encapsulated items data source.
651 internal new IGroupableItemSource InternalItemSource
655 return (base.InternalItemSource as IGroupableItemSource);
659 base.InternalItemSource = value;
664 /// Size strategy of measuring scroll content. see details in ItemSizingStrategy.
666 [EditorBrowsable(EditorBrowsableState.Never)]
667 internal ItemSizingStrategy SizingStrategy { get; set; }
670 /// <since_tizen> 9 </since_tizen>
671 public override void OnRelayout(Vector2 size, RelayoutContainer container)
673 base.OnRelayout(size, container);
675 wasRelayouted = true;
676 if (needInitalizeLayouter) Init();
680 [EditorBrowsable(EditorBrowsableState.Never)]
681 public override void NotifyDataSetChanged()
683 if (selectedItem != null)
687 if (selectedItems != null)
689 selectedItems.Clear();
692 base.NotifyDataSetChanged();
696 /// Update selected items list in multiple selection.
698 /// <param name="newSelection">updated selection list by user</param>
699 /// <since_tizen> 9 </since_tizen>
700 public void UpdateSelectedItems(IList<object> newSelection)
702 var oldSelection = new List<object>(SelectedItems);
704 suppressSelectionChangeNotification = true;
706 SelectedItems.Clear();
708 if (newSelection?.Count > 0)
710 for (int n = 0; n < newSelection.Count; n++)
712 SelectedItems.Add(newSelection[n]);
716 suppressSelectionChangeNotification = false;
718 SelectedItemsPropertyChanged(oldSelection, newSelection);
722 /// Scroll to specific position with or without animation.
724 /// <param name="position">Destination.</param>
725 /// <param name="animate">Scroll with or without animation</param>
726 /// <since_tizen> 9 </since_tizen>
727 public new void ScrollTo(float position, bool animate)
729 if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
730 if ((InternalItemSource == null) || needInitalizeLayouter)
732 delayedScrollTo = true;
733 delayedScrollToParam = (position, animate);
737 base.ScrollTo(position, animate);
741 /// Scrolls to the item at the specified index.
743 /// <param name="index">Index of item.</param>
744 [EditorBrowsable(EditorBrowsableState.Never)]
745 public new void ScrollToIndex(int index)
747 ScrollTo(index, true, ItemScrollTo.Start);
751 /// Scroll to specific item's aligned position with or without animation.
753 /// <param name="index">Target item index of dataset.</param>
754 /// <param name="animate">Boolean flag of animation.</param>
755 /// <param name="align">Align state of item. See details in <see cref="ItemScrollTo"/>.</param>
756 /// <since_tizen> 9 </since_tizen>
757 public virtual void ScrollTo(int index, bool animate = false, ItemScrollTo align = ItemScrollTo.Nearest)
759 if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
760 if ((InternalItemSource == null) || needInitalizeLayouter)
762 delayedIndexScrollTo = true;
763 delayedIndexScrollToParam = (index, animate, align);
766 if (index < 0 || index >= InternalItemSource.Count)
768 throw new Exception("index is out of boundary. index should be a value between (0, " + InternalItemSource.Count.ToString() + ").");
771 float scrollPos, curPos, curSize, curItemSize;
772 (float x, float y) = ItemsLayouter.GetItemPosition(index);
773 (float width, float height) = ItemsLayouter.GetItemSize(index);
774 if (ScrollingDirection == Direction.Horizontal)
777 curPos = ScrollPosition.X;
778 curSize = Size.Width;
784 curPos = ScrollPosition.Y;
785 curSize = Size.Height;
786 curItemSize = height;
789 //Console.WriteLine("[NUI] ScrollTo [{0}:{1}], curPos{2}, itemPos{3}, curSize{4}, itemSize{5}", InternalItemSource.GetPosition(item), align, curPos, scrollPos, curSize, curItemSize);
792 case ItemScrollTo.Start:
795 case ItemScrollTo.Center:
796 scrollPos = scrollPos - (curSize / 2) + (curItemSize / 2);
798 case ItemScrollTo.End:
799 scrollPos = scrollPos - curSize + curItemSize;
801 case ItemScrollTo.Nearest:
802 if (scrollPos < curPos - curItemSize)
804 // item is placed before the current screen. scrollTo.Top
806 else if (scrollPos >= curPos + curSize + curItemSize)
808 // item is placed after the current screen. scrollTo.End
809 scrollPos = scrollPos - curSize + curItemSize;
813 // item is in the scroller. ScrollTo() is ignored.
819 //Console.WriteLine("[NUI] ScrollTo [{0}]-------------------", scrollPos);
820 base.ScrollTo(scrollPos, animate);
824 /// Apply style to CollectionView
826 /// <param name="viewStyle">The style to apply.</param>
827 [EditorBrowsable(EditorBrowsableState.Never)]
828 public override void ApplyStyle(ViewStyle viewStyle)
830 base.ApplyStyle(viewStyle);
831 if (viewStyle != null)
833 //Extension = RecyclerViewItemStyle.CreateExtension();
835 if (itemsLayouter != null)
837 string styleName = "Tizen.NUI.Compoenents." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
838 ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
839 if (layouterStyle != null)
840 itemsLayouter.Padding = new Extents(layouterStyle.Padding);
845 /// Initialize AT-SPI object.
847 [EditorBrowsable(EditorBrowsableState.Never)]
848 public override void OnInitialize()
851 SetAccessibilityConstructor(Role.List);
855 /// Scroll to specified item
858 /// Make sure that the item that is about to receive the accessibility highlight is visible.
860 [EditorBrowsable(EditorBrowsableState.Never)]
861 protected override bool AccessibilityScrollToChild(View child)
863 if (ScrollingDirection == Direction.Horizontal)
865 if (child.ScreenPosition.X + child.Size.Width <= this.ScreenPosition.X)
867 ScrollTo((float)(child.ScreenPosition.X - ContentContainer.ScreenPosition.X), false);
869 else if (child.ScreenPosition.X >= this.ScreenPosition.X + this.Size.Width)
871 ScrollTo((float)(child.ScreenPosition.X + child.Size.Width - ContentContainer.ScreenPosition.X - this.Size.Width), false);
876 if (child.ScreenPosition.Y + child.Size.Height <= this.ScreenPosition.Y)
878 ScrollTo((float)(child.ScreenPosition.Y - ContentContainer.ScreenPosition.Y), false);
880 else if (child.ScreenPosition.Y >= this.ScreenPosition.Y + this.Size.Height)
882 ScrollTo((float)(child.ScreenPosition.Y + child.Size.Height - ContentContainer.ScreenPosition.Y - this.Size.Height), false);
888 // Realize and Decorate the item.
889 internal override RecyclerViewItem RealizeItem(int index)
891 RecyclerViewItem item;
892 if (index == 0 && Header != null)
898 if (index == InternalItemSource.Count - 1 && Footer != null)
906 var context = InternalItemSource.GetItem(index);
907 if (InternalItemSource.IsGroupHeader(index))
909 DataTemplate templ = (groupHeaderTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupHeaderTemplate;
911 RecyclerViewItem groupHeader = PopRecycleGroupCache(templ, true);
912 if (groupHeader == null)
914 groupHeader = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupHeaderTemplate, context, this);
916 groupHeader.Template = templ;
917 groupHeader.isGroupHeader = true;
918 groupHeader.isGroupFooter = false;
919 ContentContainer.Add(groupHeader);
922 if (groupHeader != null)
924 groupHeader.ParentItemsView = this;
925 groupHeader.Index = index;
926 groupHeader.ParentGroup = context;
927 groupHeader.BindingContext = context;
932 else if (InternalItemSource.IsGroupFooter(index))
934 DataTemplate templ = (groupFooterTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupFooterTemplate;
936 RecyclerViewItem groupFooter = PopRecycleGroupCache(templ, false);
937 if (groupFooter == null)
939 groupFooter = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupFooterTemplate, context, this);
941 groupFooter.Template = templ;
942 groupFooter.isGroupHeader = false;
943 groupFooter.isGroupFooter = true;
944 ContentContainer.Add(groupFooter);
947 if (groupFooter != null)
949 groupFooter.ParentItemsView = this;
950 groupFooter.Index = index;
951 groupFooter.ParentGroup = context;
952 groupFooter.BindingContext = context;
959 item = base.RealizeItem(index);
961 throw new Exception("Item realize failed by Null content return.");
962 item.ParentGroup = InternalItemSource.GetGroupParent(index);
967 item = base.RealizeItem(index);
971 throw new Exception("Item realize failed by Null content return.");
973 switch (SelectionMode)
975 case ItemSelectionMode.Single:
976 case ItemSelectionMode.SingleAlways:
977 if (item.BindingContext != null && item.BindingContext == SelectedItem)
979 item.IsSelected = true;
983 case ItemSelectionMode.Multiple:
984 if ((item.BindingContext != null) && (SelectedItems?.Contains(item.BindingContext) ?? false))
986 item.IsSelected = true;
989 case ItemSelectionMode.None:
990 item.IsSelectable = false;
996 // Unrealize and caching the item.
997 internal override void UnrealizeItem(RecyclerViewItem item, bool recycle = true)
999 if (item == null) return;
1010 if (item.isGroupHeader || item.isGroupFooter)
1013 item.ParentItemsView = null;
1014 item.BindingContext = null;
1015 item.IsPressed = false;
1016 item.IsSelected = false;
1017 item.IsEnabled = true;
1019 //item.Relayout -= OnItemRelayout;
1020 if (!recycle || !PushRecycleGroupCache(item))
1021 Utility.Dispose(item);
1025 base.UnrealizeItem(item, recycle);
1028 internal void SelectedItemsPropertyChanged(IList<object> oldSelection, IList<object> newSelection)
1030 if (suppressSelectionChangeNotification)
1035 foreach (RecyclerViewItem item in ContentContainer.Children.Where((item) => item is RecyclerViewItem))
1037 if (item.BindingContext == null) continue;
1038 if (newSelection.Contains(item.BindingContext))
1040 if (!item.IsSelected) item.IsSelected = true;
1044 if (item.IsSelected) item.IsSelected = false;
1047 SelectionPropertyChanged(this, new SelectionChangedEventArgs(oldSelection, newSelection));
1049 OnPropertyChanged(SelectedItemsProperty.PropertyName);
1053 /// Internal selection callback.
1055 /// <since_tizen> 9 </since_tizen>
1056 protected virtual void OnSelectionChanged(SelectionChangedEventArgs args)
1058 //Selection Callback
1062 /// Adjust scrolling position by own scrolling rules.
1063 /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
1065 /// <param name="position">Scroll position which is calculated by ScrollableBase</param>
1066 /// <returns>Adjusted scroll destination</returns>
1067 [EditorBrowsable(EditorBrowsableState.Never)]
1068 protected override float AdjustTargetPositionOfScrollAnimation(float position)
1070 // Destination is depending on implementation of layout manager.
1071 // Get destination from layout manager.
1072 return ItemsLayouter?.CalculateCandidateScrollPosition(position) ?? position;
1076 [EditorBrowsable(EditorBrowsableState.Never)]
1077 protected override void ClearCache()
1079 foreach (RecyclerViewItem item in recycleGroupHeaderCache)
1081 Utility.Dispose(item);
1083 recycleGroupHeaderCache.Clear();
1084 foreach (RecyclerViewItem item in recycleGroupFooterCache)
1086 Utility.Dispose(item);
1088 recycleGroupFooterCache.Clear();
1094 /// OnScroll event callback. Requesting layout to the layouter with given scrollPosition.
1096 /// <param name="source">Scroll source object</param>
1097 /// <param name="args">Scroll event argument</param>
1098 /// <since_tizen> 9 </since_tizen>
1099 protected override void OnScrolling(object source, ScrollEventArgs args)
1101 if (disposed) return;
1103 if (needInitalizeLayouter && (ItemsLayouter != null))
1105 ItemsLayouter.Initialize(this);
1106 needInitalizeLayouter = false;
1109 base.OnScrolling(source, args);
1113 /// Dispose ItemsView and all children on it.
1115 /// <param name="type">Dispose type.</param>
1116 /// <since_tizen> 9 </since_tizen>
1117 protected override void Dispose(DisposeTypes type)
1124 if (type == DisposeTypes.Explicit)
1126 // From now on, no need to use this properties,
1127 // so remove reference, to push it into garbage collector.
1129 // Arugable to disposing user-created members.
1133 Utility.Dispose(Header);
1138 Utility.Dispose(Footer);
1143 groupHeaderTemplate = null;
1144 groupFooterTemplate = null;
1146 if (selectedItem != null)
1148 selectedItem = null;
1150 if (selectedItems != null)
1152 selectedItems.Clear();
1153 selectedItems = null;
1155 if (InternalItemSource != null)
1157 InternalItemSource.Dispose();
1158 InternalItemSource = null;
1165 private static void SelectionPropertyChanged(CollectionView colView, SelectionChangedEventArgs args)
1167 var command = colView.SelectionChangedCommand;
1169 if (command != null)
1171 var commandParameter = colView.SelectionChangedCommandParameter;
1173 if (command.CanExecute(commandParameter))
1175 command.Execute(commandParameter);
1178 colView.SelectionChanged?.Invoke(colView, args);
1179 colView.OnSelectionChanged(args);
1182 private static object CoerceSelectedItems(BindableObject bindable, object value)
1186 return new SelectionList((CollectionView)bindable);
1189 if (value is SelectionList)
1194 return new SelectionList((CollectionView)bindable, value as IList<object>);
1197 private static void SelectionModePropertyChanged(BindableObject bindable, object oldValue, object newValue)
1199 var colView = (CollectionView)bindable;
1201 var oldMode = (ItemSelectionMode)oldValue;
1202 var newMode = (ItemSelectionMode)newValue;
1204 IList<object> previousSelection = new List<object>();
1205 IList<object> newSelection = new List<object>();
1209 case ItemSelectionMode.None:
1211 case ItemSelectionMode.Single:
1212 if (colView.SelectedItem != null)
1214 previousSelection.Add(colView.SelectedItem);
1217 case ItemSelectionMode.Multiple:
1218 previousSelection = colView.SelectedItems;
1224 case ItemSelectionMode.None:
1226 case ItemSelectionMode.Single:
1227 if (colView.SelectedItem != null)
1229 newSelection.Add(colView.SelectedItem);
1232 case ItemSelectionMode.Multiple:
1233 newSelection = colView.SelectedItems;
1237 if (previousSelection.Count == newSelection.Count)
1239 if (previousSelection.Count == 0 || (previousSelection[0] == newSelection[0]))
1241 // Both selections are empty or have the same single item; no reason to signal a change
1246 var args = new SelectionChangedEventArgs(previousSelection, newSelection);
1247 SelectionPropertyChanged(colView, args);
1252 if (ItemsSource == null) return;
1253 if (ItemsLayouter == null) return;
1254 if (ItemTemplate == null) return;
1256 if (disposed) return;
1257 if (needInitalizeLayouter)
1259 if (InternalItemSource == null) return;
1261 InternalItemSource.HasHeader = (header != null);
1262 InternalItemSource.HasFooter = (footer != null);
1265 if (!wasRelayouted) return;
1267 if (needInitalizeLayouter)
1269 itemsLayouter.Clear();
1272 ItemsLayouter.Initialize(this);
1273 needInitalizeLayouter = false;
1275 ItemsLayouter.RequestLayout(0.0f, true);
1277 if (delayedScrollTo)
1279 delayedScrollTo = false;
1280 ScrollTo(delayedScrollToParam.position, delayedScrollToParam.anim);
1283 if (delayedIndexScrollTo)
1285 delayedIndexScrollTo = false;
1286 ScrollTo(delayedIndexScrollToParam.index, delayedIndexScrollToParam.anim, delayedIndexScrollToParam.scrollTo);
1289 if (ScrollingDirection == Direction.Horizontal)
1291 ContentContainer.SizeWidth = ItemsLayouter.CalculateLayoutOrientationSize();
1295 ContentContainer.SizeHeight = ItemsLayouter.CalculateLayoutOrientationSize();
1299 private bool PushRecycleGroupCache(RecyclerViewItem item)
1301 if (item == null) throw new ArgumentNullException(nameof(item));
1302 if (RecycleCache.Count >= 20) return false;
1303 if (item.Template == null) return false;
1304 if (item.isGroupHeader)
1306 recycleGroupHeaderCache.Add(item);
1308 else if (item.isGroupFooter)
1310 recycleGroupFooterCache.Add(item);
1318 private RecyclerViewItem PopRecycleGroupCache(DataTemplate Template, bool isHeader)
1320 RecyclerViewItem viewItem = null;
1322 var Cache = (isHeader ? recycleGroupHeaderCache : recycleGroupFooterCache);
1323 for (int i = 0; i < Cache.Count; i++)
1325 viewItem = Cache[i];
1326 if (Template == viewItem.Template) break;
1329 if (viewItem != null)
1331 Cache.Remove(viewItem);
1336 private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
1338 switch (args.Action)
1340 case NotifyCollectionChangedAction.Add:
1342 case NotifyCollectionChangedAction.Remove:
1343 // Clear removed items.
1344 if (args.OldItems != null)
1346 if (args.OldItems.Contains(selectedItem))
1348 selectedItem = null;
1351 if (selectedItems != null)
1353 foreach (object removed in args.OldItems)
1355 if (selectedItems.Contains(removed))
1357 selectedItems.Remove(removed);
1363 case NotifyCollectionChangedAction.Replace:
1365 case NotifyCollectionChangedAction.Move:
1367 case NotifyCollectionChangedAction.Reset:
1370 throw new ArgumentOutOfRangeException(nameof(args));