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 /// Binding Property of items data source.
106 [EditorBrowsable(EditorBrowsableState.Never)]
107 public static readonly BindableProperty ItemsSourceProperty =
108 BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(CollectionView), null,
109 propertyChanged: (bindable, oldValue, newValue) =>
111 var colView = (CollectionView)bindable;
112 oldValue = colView.itemsSource;
114 if (oldValue != null)
116 // Clearing old data!
117 if (oldValue is INotifyCollectionChanged prevNotifyCollectionChanged)
119 prevNotifyCollectionChanged.CollectionChanged -= colView.CollectionChanged;
121 if (colView.selectedItem != null) colView.selectedItem = null;
122 colView.selectedItems?.Clear();
125 colView.itemsSource = (IEnumerable)newValue;
127 if (newValue == null)
129 colView.InternalItemSource?.Dispose();
130 colView.InternalItemSource = null;
131 colView.itemsLayouter?.Clear();
132 colView.ClearCache();
135 if (newValue is INotifyCollectionChanged newNotifyCollectionChanged)
137 newNotifyCollectionChanged.CollectionChanged += colView.CollectionChanged;
140 colView.InternalItemSource?.Dispose();
141 colView.InternalItemSource = ItemsSourceFactory.Create(colView);
143 if (colView.itemsLayouter == null) return;
145 colView.needInitalizeLayouter = true;
148 defaultValueCreator: (bindable) =>
150 var colView = (CollectionView)bindable;
151 return colView.itemsSource;
155 private static readonly IList<object> selectEmpty = new List<object>(0);
156 private DataTemplate itemTemplate = null;
157 private IEnumerable itemsSource = null;
158 private ItemsLayouter itemsLayouter = null;
159 private DataTemplate groupHeaderTemplate;
160 private DataTemplate groupFooterTemplate;
161 private bool isGrouped;
162 private bool wasRelayouted = false;
163 private bool needInitalizeLayouter = false;
164 private object selectedItem;
165 private SelectionList selectedItems;
166 private bool suppressSelectionChangeNotification;
167 private ItemSelectionMode selectionMode = ItemSelectionMode.None;
168 private RecyclerViewItem header;
169 private RecyclerViewItem footer;
170 private View focusedView;
171 private int prevFocusedDataIndex = 0;
172 private List<RecyclerViewItem> recycleGroupHeaderCache { get; } = new List<RecyclerViewItem>();
173 private List<RecyclerViewItem> recycleGroupFooterCache { get; } = new List<RecyclerViewItem>();
174 private bool delayedScrollTo;
175 private (float position, bool anim) delayedScrollToParam;
177 private bool delayedIndexScrollTo;
178 private (int index, bool anim, ItemScrollTo scrollTo) delayedIndexScrollToParam;
181 /// Base constructor.
183 /// <since_tizen> 9 </since_tizen>
184 public CollectionView() : base()
187 SetKeyboardNavigationSupport(true);
191 /// Base constructor with ItemsSource
193 /// <param name="itemsSource">item's data source</param>
194 /// <since_tizen> 9 </since_tizen>
195 public CollectionView(IEnumerable itemsSource) : this()
197 ItemsSource = itemsSource;
201 /// Base constructor with ItemsSource, ItemsLayouter and ItemTemplate
203 /// <param name="itemsSource">item's data source</param>
204 /// <param name="layouter">item's layout manager</param>
205 /// <param name="template">item's view template with data bindings</param>
206 [EditorBrowsable(EditorBrowsableState.Never)]
207 public CollectionView(IEnumerable itemsSource, ItemsLayouter layouter, DataTemplate template) : this()
209 ItemsSource = itemsSource;
210 ItemTemplate = template;
211 ItemsLayouter = layouter;
215 /// Event of Selection changed.
216 /// previous selection list and current selection will be provided.
218 /// <since_tizen> 9 </since_tizen>
219 public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
222 /// Align item in the viewport when ScrollTo() calls.
224 /// <since_tizen> 9 </since_tizen>
225 public enum ItemScrollTo
228 /// Scroll to show item in nearest viewport on scroll direction.
229 /// item is above the scroll viewport, item will be came into front,
230 /// item is under the scroll viewport, item will be came into end,
231 /// item is in the scroll viewport, no scroll.
233 /// <since_tizen> 9 </since_tizen>
236 /// Scroll to show item in start of the viewport.
238 /// <since_tizen> 9 </since_tizen>
241 /// Scroll to show item in center of the viewport.
243 /// <since_tizen> 9 </since_tizen>
246 /// Scroll to show item in end of the viewport.
248 /// <since_tizen> 9 </since_tizen>
253 /// Item's source data in IEnumerable.
255 /// <since_tizen> 9 </since_tizen>
256 public override IEnumerable ItemsSource
258 get => (IEnumerable)GetValue(ItemsSourceProperty);
259 set => SetValue(ItemsSourceProperty, value);
263 /// DataTemplate for items.
264 /// Create visual contents and binding properties.
265 /// return object type is restricted RecyclerViewItem.
266 /// <seealso cref="Tizen.NUI.Binding.DataTemplate" />
268 /// <since_tizen> 9 </since_tizen>
269 public override DataTemplate ItemTemplate
273 return GetValue(ItemTemplateProperty) as DataTemplate;
277 SetValue(ItemTemplateProperty, value);
278 NotifyPropertyChanged();
281 private DataTemplate InternalItemTemplate
289 itemTemplate = value;
295 needInitalizeLayouter = true;
302 /// Layouting items on the scroll ContentContainer.
303 /// <seealso cref="ItemsLayouter" />
304 /// <seealso cref="LinearLayouter" />
305 /// <seealso cref="GridLayouter" />
307 /// <since_tizen> 9 </since_tizen>
308 public virtual ItemsLayouter ItemsLayouter
312 return GetValue(ItemsLayouterProperty) as ItemsLayouter;
316 SetValue(ItemsLayouterProperty, value);
317 NotifyPropertyChanged();
320 private ItemsLayouter InternalItemsLayouter
324 return itemsLayouter;
328 itemsLayouter?.Clear();
331 itemsLayouter = value;
332 base.InternalItemsLayouter = itemsLayouter;
335 needInitalizeLayouter = false;
339 needInitalizeLayouter = true;
341 var styleName = "Tizen.NUI.Components." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
342 ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
343 if (layouterStyle != null)
345 itemsLayouter.Padding = new Extents(layouterStyle.Padding);
352 /// Scrolling direction to display items layout.
354 /// <since_tizen> 9 </since_tizen>
355 public new Direction ScrollingDirection
359 return (Direction)GetValue(ScrollingDirectionProperty);
363 SetValue(ScrollingDirectionProperty, value);
364 NotifyPropertyChanged();
367 private Direction InternalScrollingDirection
371 return base.ScrollingDirection;
375 if (base.ScrollingDirection != value)
377 base.ScrollingDirection = value;
378 needInitalizeLayouter = true;
385 /// Selected item in single selection.
387 /// <since_tizen> 9 </since_tizen>
388 public object SelectedItem
390 get => GetValue(SelectedItemProperty);
391 set => SetValue(SelectedItemProperty, value);
395 /// Selected items list in multiple selection.
397 /// <since_tizen> 9 </since_tizen>
398 public IList<object> SelectedItems
400 get => (IList<object>)GetValue(SelectedItemsProperty);
401 // set => SetValue(SelectedItemsProperty, new SelectionList(this, value));
405 /// Selection mode to handle items selection. See ItemSelectionMode for details.
407 /// <since_tizen> 9 </since_tizen>
408 public ItemSelectionMode SelectionMode
410 get => (ItemSelectionMode)GetValue(SelectionModeProperty);
411 set => SetValue(SelectionModeProperty, value);
415 /// Command of selection changed.
417 [EditorBrowsable(EditorBrowsableState.Never)]
418 public ICommand SelectionChangedCommand
422 return GetValue(SelectionChangedCommandProperty) as ICommand;
426 SetValue(SelectionChangedCommandProperty, value);
427 NotifyPropertyChanged();
430 private ICommand InternalSelectionChangedCommand { set; get; }
433 /// Command parameter of selection changed.
435 [EditorBrowsable(EditorBrowsableState.Never)]
436 public object SelectionChangedCommandParameter
440 return GetValue(SelectionChangedCommandParameterProperty);
444 SetValue(SelectionChangedCommandParameterProperty, value);
445 NotifyPropertyChanged();
448 private object InternalSelectionChangedCommandParameter { set; get; }
451 /// Header item placed in top-most position.
453 /// <remarks>Please note that, internal index will be increased by header.</remarks>
454 /// <since_tizen> 9 </since_tizen>
455 public RecyclerViewItem Header
459 return GetValue(HeaderProperty) as RecyclerViewItem;
463 SetValue(HeaderProperty, value);
464 NotifyPropertyChanged();
467 private RecyclerViewItem InternalHeader
474 //ContentContainer.Remove(header);
475 Utility.Dispose(header);
480 value.ParentItemsView = this;
481 value.IsHeader = true;
482 ContentContainer.Add(value);
485 if (InternalItemSource != null)
487 InternalItemSource.HasHeader = (value != null);
489 needInitalizeLayouter = true;
495 /// Footer item placed in bottom-most position.
497 /// <remarks>Please note that, internal index will be increased by footer.</remarks>
498 /// <since_tizen> 9 </since_tizen>
499 public RecyclerViewItem Footer
503 return GetValue(FooterProperty) as RecyclerViewItem;
507 SetValue(FooterProperty, value);
508 NotifyPropertyChanged();
511 private RecyclerViewItem InternalFooter
518 //ContentContainer.Remove(footer);
519 Utility.Dispose(footer);
523 value.Index = InternalItemSource?.Count ?? 0;
524 value.ParentItemsView = this;
525 value.IsFooter = true;
526 ContentContainer.Add(value);
529 if (InternalItemSource != null)
531 InternalItemSource.HasFooter = (value != null);
533 needInitalizeLayouter = true;
539 /// Enable groupable view.
541 [EditorBrowsable(EditorBrowsableState.Never)]
542 public bool IsGrouped
546 return (bool)GetValue(IsGroupedProperty);
550 SetValue(IsGroupedProperty, value);
551 NotifyPropertyChanged();
554 private bool InternalIsGrouped
560 needInitalizeLayouter = true;
561 //Need to re-intialize Internal Item Source.
562 if (InternalItemSource != null)
564 InternalItemSource.Dispose();
565 InternalItemSource = null;
567 if (ItemsSource != null)
568 InternalItemSource = ItemsSourceFactory.Create(this);
574 /// DataTemplate of group header.
576 /// <remarks>Please note that, internal index will be increased by group header.
577 /// GroupHeaderTemplate is essential for groupable view.</remarks>
578 [EditorBrowsable(EditorBrowsableState.Never)]
579 public DataTemplate GroupHeaderTemplate
583 return GetValue(GroupHeaderTemplateProperty) as DataTemplate;
587 SetValue(GroupHeaderTemplateProperty, value);
588 NotifyPropertyChanged();
591 private DataTemplate InternalGroupHeaderTemplate
595 return groupHeaderTemplate;
599 groupHeaderTemplate = value;
600 needInitalizeLayouter = true;
601 //Need to re-intialize Internal Item Source.
602 if (InternalItemSource != null)
604 InternalItemSource.Dispose();
605 InternalItemSource = null;
607 if (ItemsSource != null)
608 InternalItemSource = ItemsSourceFactory.Create(this);
614 /// DataTemplate of group footer. Group feature is not supported yet.
616 /// <remarks>Please note that, internal index will be increased by group footer.</remarks>
617 [EditorBrowsable(EditorBrowsableState.Never)]
618 public DataTemplate GroupFooterTemplate
622 return GetValue(GroupFooterTemplateProperty) as DataTemplate;
626 SetValue(GroupFooterTemplateProperty, value);
627 NotifyPropertyChanged();
630 private DataTemplate InternalGroupFooterTemplate
634 return groupFooterTemplate;
638 groupFooterTemplate = value;
639 needInitalizeLayouter = true;
640 //Need to re-intialize Internal Item Source.
641 if (InternalItemSource != null)
643 InternalItemSource.Dispose();
644 InternalItemSource = null;
646 if (ItemsSource != null)
647 InternalItemSource = ItemsSourceFactory.Create(this);
653 /// Internal encapsulated items data source.
655 internal new IGroupableItemSource InternalItemSource
659 return (base.InternalItemSource as IGroupableItemSource);
663 base.InternalItemSource = value;
668 /// Size strategy of measuring scroll content. see details in ItemSizingStrategy.
670 [EditorBrowsable(EditorBrowsableState.Never)]
671 internal ItemSizingStrategy SizingStrategy { get; set; }
674 /// <since_tizen> 9 </since_tizen>
675 public override void OnRelayout(Vector2 size, RelayoutContainer container)
677 base.OnRelayout(size, container);
679 wasRelayouted = true;
680 if (needInitalizeLayouter) Init();
684 [EditorBrowsable(EditorBrowsableState.Never)]
685 public override void NotifyDataSetChanged()
687 if (selectedItem != null)
691 if (selectedItems != null)
693 selectedItems.Clear();
696 base.NotifyDataSetChanged();
700 /// Update selected items list in multiple selection.
702 /// <param name="newSelection">updated selection list by user</param>
703 /// <since_tizen> 9 </since_tizen>
704 public void UpdateSelectedItems(IList<object> newSelection)
706 var oldSelection = new List<object>(SelectedItems);
708 suppressSelectionChangeNotification = true;
710 SelectedItems.Clear();
712 if (newSelection?.Count > 0)
714 for (int n = 0; n < newSelection.Count; n++)
716 SelectedItems.Add(newSelection[n]);
720 suppressSelectionChangeNotification = false;
722 SelectedItemsPropertyChanged(oldSelection, newSelection);
726 /// Scroll to specific position with or without animation.
728 /// <param name="position">Destination.</param>
729 /// <param name="animate">Scroll with or without animation</param>
730 /// <since_tizen> 9 </since_tizen>
731 public new void ScrollTo(float position, bool animate)
733 if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
734 if ((InternalItemSource == null) || needInitalizeLayouter)
736 delayedScrollTo = true;
737 delayedScrollToParam = (position, animate);
741 base.ScrollTo(position, animate);
745 /// Scrolls to the item at the specified index.
747 /// <param name="index">Index of item.</param>
748 [EditorBrowsable(EditorBrowsableState.Never)]
749 public new void ScrollToIndex(int index)
751 ScrollTo(index, true, ItemScrollTo.Start);
755 /// Scroll to specific item's aligned position with or without animation.
757 /// <param name="index">Target item index of dataset.</param>
758 /// <param name="animate">Boolean flag of animation.</param>
759 /// <param name="align">Align state of item. See details in <see cref="ItemScrollTo"/>.</param>
760 /// <since_tizen> 9 </since_tizen>
761 public virtual void ScrollTo(int index, bool animate = false, ItemScrollTo align = ItemScrollTo.Nearest)
763 if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
764 if ((InternalItemSource == null) || needInitalizeLayouter)
766 delayedIndexScrollTo = true;
767 delayedIndexScrollToParam = (index, animate, align);
770 if (index < 0 || index >= InternalItemSource.Count)
772 throw new Exception("index is out of boundary. index should be a value between (0, " + InternalItemSource.Count.ToString() + ").");
775 float scrollPos, curPos, curSize, curItemSize;
776 (float x, float y) = ItemsLayouter.GetItemPosition(index);
777 (float width, float height) = ItemsLayouter.GetItemSize(index);
778 if (ScrollingDirection == Direction.Horizontal)
781 curPos = ScrollPosition.X;
782 curSize = Size.Width;
788 curPos = ScrollPosition.Y;
789 curSize = Size.Height;
790 curItemSize = height;
793 //Console.WriteLine("[NUI] ScrollTo [{0}:{1}], curPos{2}, itemPos{3}, curSize{4}, itemSize{5}", InternalItemSource.GetPosition(item), align, curPos, scrollPos, curSize, curItemSize);
796 case ItemScrollTo.Start:
799 case ItemScrollTo.Center:
800 scrollPos = scrollPos - (curSize / 2) + (curItemSize / 2);
802 case ItemScrollTo.End:
803 scrollPos = scrollPos - curSize + curItemSize;
805 case ItemScrollTo.Nearest:
806 if (scrollPos < curPos - curItemSize)
808 // item is placed before the current screen. scrollTo.Top
810 else if (scrollPos >= curPos + curSize + curItemSize)
812 // item is placed after the current screen. scrollTo.End
813 scrollPos = scrollPos - curSize + curItemSize;
817 // item is in the scroller. ScrollTo() is ignored.
823 //Console.WriteLine("[NUI] ScrollTo [{0}]-------------------", scrollPos);
824 base.ScrollTo(scrollPos, animate);
828 /// Apply style to CollectionView
830 /// <param name="viewStyle">The style to apply.</param>
831 [EditorBrowsable(EditorBrowsableState.Never)]
832 public override void ApplyStyle(ViewStyle viewStyle)
834 base.ApplyStyle(viewStyle);
835 if (viewStyle != null)
837 //Extension = RecyclerViewItemStyle.CreateExtension();
839 if (itemsLayouter != null)
841 string styleName = "Tizen.NUI.Compoenents." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
842 ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
843 if (layouterStyle != null)
844 itemsLayouter.Padding = new Extents(layouterStyle.Padding);
849 /// Initialize AT-SPI object.
851 [EditorBrowsable(EditorBrowsableState.Never)]
852 public override void OnInitialize()
855 SetAccessibilityConstructor(Role.List);
859 /// Scroll to specified item
862 /// Make sure that the item that is about to receive the accessibility highlight is visible.
864 [EditorBrowsable(EditorBrowsableState.Never)]
865 protected override bool AccessibilityScrollToChild(View child)
867 if (ScrollingDirection == Direction.Horizontal)
869 if (child.ScreenPosition.X + child.Size.Width <= this.ScreenPosition.X)
871 ScrollTo((float)(child.ScreenPosition.X - ContentContainer.ScreenPosition.X), false);
873 else if (child.ScreenPosition.X >= this.ScreenPosition.X + this.Size.Width)
875 ScrollTo((float)(child.ScreenPosition.X + child.Size.Width - ContentContainer.ScreenPosition.X - this.Size.Width), false);
880 if (child.ScreenPosition.Y + child.Size.Height <= this.ScreenPosition.Y)
882 ScrollTo((float)(child.ScreenPosition.Y - ContentContainer.ScreenPosition.Y), false);
884 else if (child.ScreenPosition.Y >= this.ScreenPosition.Y + this.Size.Height)
886 ScrollTo((float)(child.ScreenPosition.Y + child.Size.Height - ContentContainer.ScreenPosition.Y - this.Size.Height), false);
892 // Realize and Decorate the item.
893 internal override RecyclerViewItem RealizeItem(int index)
895 RecyclerViewItem item;
896 if (index == 0 && Header != null)
902 if (index == InternalItemSource.Count - 1 && Footer != null)
910 var context = InternalItemSource.GetItem(index);
911 if (InternalItemSource.IsGroupHeader(index))
913 DataTemplate templ = (groupHeaderTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupHeaderTemplate;
915 RecyclerViewItem groupHeader = PopRecycleGroupCache(templ, true);
916 if (groupHeader == null)
918 groupHeader = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupHeaderTemplate, context, this);
920 groupHeader.Template = templ;
921 groupHeader.isGroupHeader = true;
922 groupHeader.isGroupFooter = false;
923 ContentContainer.Add(groupHeader);
926 if (groupHeader != null)
928 groupHeader.ParentItemsView = this;
929 groupHeader.Index = index;
930 groupHeader.ParentGroup = context;
931 groupHeader.BindingContext = context;
936 else if (InternalItemSource.IsGroupFooter(index))
938 DataTemplate templ = (groupFooterTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupFooterTemplate;
940 RecyclerViewItem groupFooter = PopRecycleGroupCache(templ, false);
941 if (groupFooter == null)
943 groupFooter = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupFooterTemplate, context, this);
945 groupFooter.Template = templ;
946 groupFooter.isGroupHeader = false;
947 groupFooter.isGroupFooter = true;
948 ContentContainer.Add(groupFooter);
951 if (groupFooter != null)
953 groupFooter.ParentItemsView = this;
954 groupFooter.Index = index;
955 groupFooter.ParentGroup = context;
956 groupFooter.BindingContext = context;
963 item = base.RealizeItem(index);
965 throw new Exception("Item realize failed by Null content return.");
966 item.ParentGroup = InternalItemSource.GetGroupParent(index);
971 item = base.RealizeItem(index);
975 throw new Exception("Item realize failed by Null content return.");
977 switch (SelectionMode)
979 case ItemSelectionMode.Single:
980 case ItemSelectionMode.SingleAlways:
981 if (item.BindingContext != null && item.BindingContext == SelectedItem)
983 item.IsSelected = true;
987 case ItemSelectionMode.Multiple:
988 if ((item.BindingContext != null) && (SelectedItems?.Contains(item.BindingContext) ?? false))
990 item.IsSelected = true;
993 case ItemSelectionMode.None:
994 item.IsSelectable = false;
1000 // Unrealize and caching the item.
1001 internal override void UnrealizeItem(RecyclerViewItem item, bool recycle = true)
1003 if (item == null) return;
1014 if (item.isGroupHeader || item.isGroupFooter)
1017 item.ParentItemsView = null;
1018 item.BindingContext = null;
1019 item.IsPressed = false;
1020 item.IsSelected = false;
1021 item.IsEnabled = true;
1023 //item.Relayout -= OnItemRelayout;
1024 if (!recycle || !PushRecycleGroupCache(item))
1025 Utility.Dispose(item);
1029 base.UnrealizeItem(item, recycle);
1032 internal void SelectedItemsPropertyChanged(IList<object> oldSelection, IList<object> newSelection)
1034 if (suppressSelectionChangeNotification)
1039 foreach (RecyclerViewItem item in ContentContainer.Children.Where((item) => item is RecyclerViewItem))
1041 if (item.BindingContext == null) continue;
1042 if (newSelection.Contains(item.BindingContext))
1044 if (!item.IsSelected) item.IsSelected = true;
1048 if (item.IsSelected) item.IsSelected = false;
1051 SelectionPropertyChanged(this, new SelectionChangedEventArgs(oldSelection, newSelection));
1053 OnPropertyChanged(SelectedItemsProperty.PropertyName);
1057 /// Internal selection callback.
1059 /// <since_tizen> 9 </since_tizen>
1060 protected virtual void OnSelectionChanged(SelectionChangedEventArgs args)
1062 //Selection Callback
1066 /// Adjust scrolling position by own scrolling rules.
1067 /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
1069 /// <param name="position">Scroll position which is calculated by ScrollableBase</param>
1070 /// <returns>Adjusted scroll destination</returns>
1071 [EditorBrowsable(EditorBrowsableState.Never)]
1072 protected override float AdjustTargetPositionOfScrollAnimation(float position)
1074 // Destination is depending on implementation of layout manager.
1075 // Get destination from layout manager.
1076 return ItemsLayouter?.CalculateCandidateScrollPosition(position) ?? position;
1080 [EditorBrowsable(EditorBrowsableState.Never)]
1081 protected override void ClearCache()
1083 foreach (RecyclerViewItem item in recycleGroupHeaderCache)
1085 Utility.Dispose(item);
1087 recycleGroupHeaderCache.Clear();
1088 foreach (RecyclerViewItem item in recycleGroupFooterCache)
1090 Utility.Dispose(item);
1092 recycleGroupFooterCache.Clear();
1098 /// OnScroll event callback. Requesting layout to the layouter with given scrollPosition.
1100 /// <param name="source">Scroll source object</param>
1101 /// <param name="args">Scroll event argument</param>
1102 /// <since_tizen> 9 </since_tizen>
1103 protected override void OnScrolling(object source, ScrollEventArgs args)
1105 if (disposed) return;
1107 if (needInitalizeLayouter && (ItemsLayouter != null))
1109 ItemsLayouter.Initialize(this);
1110 needInitalizeLayouter = false;
1113 base.OnScrolling(source, args);
1117 /// Dispose ItemsView and all children on it.
1119 /// <param name="type">Dispose type.</param>
1120 /// <since_tizen> 9 </since_tizen>
1121 protected override void Dispose(DisposeTypes type)
1128 if (type == DisposeTypes.Explicit)
1130 // From now on, no need to use this properties,
1131 // so remove reference, to push it into garbage collector.
1133 // Arugable to disposing user-created members.
1137 Utility.Dispose(Header);
1142 Utility.Dispose(Footer);
1147 groupHeaderTemplate = null;
1148 groupFooterTemplate = null;
1150 if (selectedItem != null)
1152 selectedItem = null;
1154 if (selectedItems != null)
1156 selectedItems.Clear();
1157 selectedItems = null;
1159 if (InternalItemSource != null)
1161 InternalItemSource.Dispose();
1162 InternalItemSource = null;
1169 private static void SelectionPropertyChanged(CollectionView colView, SelectionChangedEventArgs args)
1171 var command = colView.SelectionChangedCommand;
1173 if (command != null)
1175 var commandParameter = colView.SelectionChangedCommandParameter;
1177 if (command.CanExecute(commandParameter))
1179 command.Execute(commandParameter);
1182 colView.SelectionChanged?.Invoke(colView, args);
1183 colView.OnSelectionChanged(args);
1186 private static object CoerceSelectedItems(BindableObject bindable, object value)
1190 return new SelectionList((CollectionView)bindable);
1193 if (value is SelectionList)
1198 return new SelectionList((CollectionView)bindable, value as IList<object>);
1201 private static void SelectionModePropertyChanged(BindableObject bindable, object oldValue, object newValue)
1203 var colView = (CollectionView)bindable;
1205 var oldMode = (ItemSelectionMode)oldValue;
1206 var newMode = (ItemSelectionMode)newValue;
1208 IList<object> previousSelection = new List<object>();
1209 IList<object> newSelection = new List<object>();
1213 case ItemSelectionMode.None:
1215 case ItemSelectionMode.Single:
1216 if (colView.SelectedItem != null)
1218 previousSelection.Add(colView.SelectedItem);
1221 case ItemSelectionMode.Multiple:
1222 previousSelection = colView.SelectedItems;
1228 case ItemSelectionMode.None:
1230 case ItemSelectionMode.Single:
1231 if (colView.SelectedItem != null)
1233 newSelection.Add(colView.SelectedItem);
1236 case ItemSelectionMode.Multiple:
1237 newSelection = colView.SelectedItems;
1241 if (previousSelection.Count == newSelection.Count)
1243 if (previousSelection.Count == 0 || (previousSelection[0] == newSelection[0]))
1245 // Both selections are empty or have the same single item; no reason to signal a change
1250 var args = new SelectionChangedEventArgs(previousSelection, newSelection);
1251 SelectionPropertyChanged(colView, args);
1256 if (ItemsSource == null) return;
1257 if (ItemsLayouter == null) return;
1258 if (ItemTemplate == null) return;
1260 if (disposed) return;
1261 if (needInitalizeLayouter)
1263 if (InternalItemSource == null) return;
1265 InternalItemSource.HasHeader = (header != null);
1266 InternalItemSource.HasFooter = (footer != null);
1269 if (!wasRelayouted) return;
1271 if (needInitalizeLayouter)
1273 itemsLayouter.Clear();
1276 ItemsLayouter.Initialize(this);
1277 needInitalizeLayouter = false;
1279 ItemsLayouter.RequestLayout(0.0f, true);
1281 if (delayedScrollTo)
1283 delayedScrollTo = false;
1284 ScrollTo(delayedScrollToParam.position, delayedScrollToParam.anim);
1287 if (delayedIndexScrollTo)
1289 delayedIndexScrollTo = false;
1290 ScrollTo(delayedIndexScrollToParam.index, delayedIndexScrollToParam.anim, delayedIndexScrollToParam.scrollTo);
1293 if (ScrollingDirection == Direction.Horizontal)
1295 ContentContainer.SizeWidth = ItemsLayouter.CalculateLayoutOrientationSize();
1299 ContentContainer.SizeHeight = ItemsLayouter.CalculateLayoutOrientationSize();
1303 private bool PushRecycleGroupCache(RecyclerViewItem item)
1305 if (item == null) throw new ArgumentNullException(nameof(item));
1306 if (RecycleCache.Count >= 20) return false;
1307 if (item.Template == null) return false;
1308 if (item.isGroupHeader)
1310 recycleGroupHeaderCache.Add(item);
1312 else if (item.isGroupFooter)
1314 recycleGroupFooterCache.Add(item);
1322 private RecyclerViewItem PopRecycleGroupCache(DataTemplate Template, bool isHeader)
1324 RecyclerViewItem viewItem = null;
1326 var Cache = (isHeader ? recycleGroupHeaderCache : recycleGroupFooterCache);
1327 for (int i = 0; i < Cache.Count; i++)
1329 viewItem = Cache[i];
1330 if (Template == viewItem.Template) break;
1333 if (viewItem != null)
1335 Cache.Remove(viewItem);
1340 private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
1342 switch (args.Action)
1344 case NotifyCollectionChangedAction.Add:
1346 case NotifyCollectionChangedAction.Remove:
1347 // Clear removed items.
1348 if (args.OldItems != null)
1350 if (args.OldItems.Contains(selectedItem))
1352 selectedItem = null;
1355 if (selectedItems != null)
1357 foreach (object removed in args.OldItems)
1359 if (selectedItems.Contains(removed))
1361 selectedItems.Remove(removed);
1367 case NotifyCollectionChangedAction.Replace:
1369 case NotifyCollectionChangedAction.Move:
1371 case NotifyCollectionChangedAction.Reset:
1374 throw new ArgumentOutOfRangeException(nameof(args));