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 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
277 itemTemplate = value;
283 needInitalizeLayouter = true;
290 /// Layouting items on the scroll ContentContainer.
291 /// <seealso cref="ItemsLayouter" />
292 /// <seealso cref="LinearLayouter" />
293 /// <seealso cref="GridLayouter" />
295 /// <since_tizen> 9 </since_tizen>
296 public virtual ItemsLayouter ItemsLayouter
300 return itemsLayouter;
304 itemsLayouter?.Clear();
307 itemsLayouter = value;
308 base.InternalItemsLayouter = itemsLayouter;
311 needInitalizeLayouter = false;
315 needInitalizeLayouter = true;
317 var styleName = "Tizen.NUI.Components." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
318 ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
319 if (layouterStyle != null)
321 itemsLayouter.Padding = new Extents(layouterStyle.Padding);
328 /// Scrolling direction to display items layout.
330 /// <since_tizen> 9 </since_tizen>
331 public new Direction ScrollingDirection
335 return base.ScrollingDirection;
339 if (base.ScrollingDirection != value)
341 base.ScrollingDirection = value;
342 needInitalizeLayouter = true;
349 /// Selected item in single selection.
351 /// <since_tizen> 9 </since_tizen>
352 public object SelectedItem
354 get => GetValue(SelectedItemProperty);
355 set => SetValue(SelectedItemProperty, value);
359 /// Selected items list in multiple selection.
361 /// <since_tizen> 9 </since_tizen>
362 public IList<object> SelectedItems
364 get => (IList<object>)GetValue(SelectedItemsProperty);
365 // set => SetValue(SelectedItemsProperty, new SelectionList(this, value));
369 /// Selection mode to handle items selection. See ItemSelectionMode for details.
371 /// <since_tizen> 9 </since_tizen>
372 public ItemSelectionMode SelectionMode
374 get => (ItemSelectionMode)GetValue(SelectionModeProperty);
375 set => SetValue(SelectionModeProperty, value);
379 /// Command of selection changed.
381 [EditorBrowsable(EditorBrowsableState.Never)]
382 public ICommand SelectionChangedCommand { set; get; }
385 /// Command parameter of selection changed.
387 [EditorBrowsable(EditorBrowsableState.Never)]
388 public object SelectionChangedCommandParameter { set; get; }
391 /// Header item placed in top-most position.
393 /// <remarks>Please note that, internal index will be increased by header.</remarks>
394 /// <since_tizen> 9 </since_tizen>
395 public RecyclerViewItem Header
402 //ContentContainer.Remove(header);
403 Utility.Dispose(header);
408 value.ParentItemsView = this;
409 value.IsHeader = true;
410 ContentContainer.Add(value);
413 if (InternalItemSource != null)
415 InternalItemSource.HasHeader = (value != null);
417 needInitalizeLayouter = true;
423 /// Footer item placed in bottom-most position.
425 /// <remarks>Please note that, internal index will be increased by footer.</remarks>
426 /// <since_tizen> 9 </since_tizen>
427 public RecyclerViewItem Footer
434 //ContentContainer.Remove(footer);
435 Utility.Dispose(footer);
439 value.Index = InternalItemSource?.Count ?? 0;
440 value.ParentItemsView = this;
441 value.IsFooter = true;
442 ContentContainer.Add(value);
445 if (InternalItemSource != null)
447 InternalItemSource.HasFooter = (value != null);
449 needInitalizeLayouter = true;
455 /// Enable groupable view.
457 [EditorBrowsable(EditorBrowsableState.Never)]
458 public bool IsGrouped
464 needInitalizeLayouter = true;
465 //Need to re-intialize Internal Item Source.
466 if (InternalItemSource != null)
468 InternalItemSource.Dispose();
469 InternalItemSource = null;
471 if (ItemsSource != null)
472 InternalItemSource = ItemsSourceFactory.Create(this);
478 /// DataTemplate of group header.
480 /// <remarks>Please note that, internal index will be increased by group header.
481 /// GroupHeaderTemplate is essential for groupable view.</remarks>
482 [EditorBrowsable(EditorBrowsableState.Never)]
483 public DataTemplate GroupHeaderTemplate
487 return groupHeaderTemplate;
491 groupHeaderTemplate = value;
492 needInitalizeLayouter = true;
493 //Need to re-intialize Internal Item Source.
494 if (InternalItemSource != null)
496 InternalItemSource.Dispose();
497 InternalItemSource = null;
499 if (ItemsSource != null)
500 InternalItemSource = ItemsSourceFactory.Create(this);
506 /// DataTemplate of group footer. Group feature is not supported yet.
508 /// <remarks>Please note that, internal index will be increased by group footer.</remarks>
509 [EditorBrowsable(EditorBrowsableState.Never)]
510 public DataTemplate GroupFooterTemplate
514 return groupFooterTemplate;
518 groupFooterTemplate = value;
519 needInitalizeLayouter = true;
520 //Need to re-intialize Internal Item Source.
521 if (InternalItemSource != null)
523 InternalItemSource.Dispose();
524 InternalItemSource = null;
526 if (ItemsSource != null)
527 InternalItemSource = ItemsSourceFactory.Create(this);
533 /// Internal encapsulated items data source.
535 internal new IGroupableItemSource InternalItemSource
539 return (base.InternalItemSource as IGroupableItemSource);
543 base.InternalItemSource = value;
548 /// Size strategy of measuring scroll content. see details in ItemSizingStrategy.
550 [EditorBrowsable(EditorBrowsableState.Never)]
551 internal ItemSizingStrategy SizingStrategy { get; set; }
554 /// <since_tizen> 9 </since_tizen>
555 public override void OnRelayout(Vector2 size, RelayoutContainer container)
557 base.OnRelayout(size, container);
559 wasRelayouted = true;
560 if (needInitalizeLayouter) Init();
564 [EditorBrowsable(EditorBrowsableState.Never)]
565 public override void NotifyDataSetChanged()
567 if (selectedItem != null)
571 if (selectedItems != null)
573 selectedItems.Clear();
576 base.NotifyDataSetChanged();
580 [EditorBrowsable(EditorBrowsableState.Never)]
581 public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
583 View nextFocusedView = null;
585 if (focusedView == null)
587 // If focusedView is null, find child which has previous data index
588 if (ContentContainer.Children.Count > 0 && InternalItemSource.Count > 0)
590 for (int i = 0; i < ContentContainer.Children.Count; i++)
592 RecyclerViewItem item = Children[i] as RecyclerViewItem;
593 if (item?.Index == prevFocusedDataIndex)
595 nextFocusedView = item;
603 // If this is not first focus, request next focus to Layouter
604 nextFocusedView = ItemsLayouter.RequestNextFocusableView(currentFocusedView, direction, loopEnabled);
607 if (nextFocusedView != null)
609 // Check next focused view is inside of visible area.
610 // If it is not, move scroll position to make it visible.
611 Position scrollPosition = ContentContainer.CurrentPosition;
612 float targetPosition = -(ScrollingDirection == Direction.Horizontal ? scrollPosition.X : scrollPosition.Y);
614 float left = nextFocusedView.Position.X;
615 float right = nextFocusedView.Position.X + nextFocusedView.Size.Width;
616 float top = nextFocusedView.Position.Y;
617 float bottom = nextFocusedView.Position.Y + nextFocusedView.Size.Height;
619 float visibleRectangleLeft = -scrollPosition.X;
620 float visibleRectangleRight = -scrollPosition.X + Size.Width;
621 float visibleRectangleTop = -scrollPosition.Y;
622 float visibleRectangleBottom = -scrollPosition.Y + Size.Height;
624 if (ScrollingDirection == Direction.Horizontal)
626 if ((direction == View.FocusDirection.Left || direction == View.FocusDirection.Up) && left < visibleRectangleLeft)
628 targetPosition = left;
630 else if ((direction == View.FocusDirection.Right || direction == View.FocusDirection.Down) && right > visibleRectangleRight)
632 targetPosition = right - Size.Width;
637 if ((direction == View.FocusDirection.Up || direction == View.FocusDirection.Left) && top < visibleRectangleTop)
639 targetPosition = top;
641 else if ((direction == View.FocusDirection.Down || direction == View.FocusDirection.Right) && bottom > visibleRectangleBottom)
643 targetPosition = bottom - Size.Height;
647 focusedView = nextFocusedView;
648 prevFocusedDataIndex = (nextFocusedView as RecyclerViewItem)?.Index ?? -1;
650 ScrollTo(targetPosition, true);
654 // If nextView is null, it means that we should move focus to outside of Control.
655 // Return FocusableView depending on direction.
658 case View.FocusDirection.Left:
660 nextFocusedView = LeftFocusableView;
663 case View.FocusDirection.Right:
665 nextFocusedView = RightFocusableView;
668 case View.FocusDirection.Up:
670 nextFocusedView = UpFocusableView;
673 case View.FocusDirection.Down:
675 nextFocusedView = DownFocusableView;
680 if (nextFocusedView != null)
686 //If FocusableView doesn't exist, not move focus.
687 nextFocusedView = focusedView;
691 return nextFocusedView;
695 /// Update selected items list in multiple selection.
697 /// <param name="newSelection">updated selection list by user</param>
698 /// <since_tizen> 9 </since_tizen>
699 public void UpdateSelectedItems(IList<object> newSelection)
701 var oldSelection = new List<object>(SelectedItems);
703 suppressSelectionChangeNotification = true;
705 SelectedItems.Clear();
707 if (newSelection?.Count > 0)
709 for (int n = 0; n < newSelection.Count; n++)
711 SelectedItems.Add(newSelection[n]);
715 suppressSelectionChangeNotification = false;
717 SelectedItemsPropertyChanged(oldSelection, newSelection);
721 /// Scroll to specific position with or without animation.
723 /// <param name="position">Destination.</param>
724 /// <param name="animate">Scroll with or without animation</param>
725 /// <since_tizen> 9 </since_tizen>
726 public new void ScrollTo(float position, bool animate)
728 if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
729 if ((InternalItemSource == null) || needInitalizeLayouter)
731 delayedScrollTo = true;
732 delayedScrollToParam = (position, animate);
736 base.ScrollTo(position, animate);
740 /// Scrolls to the item at the specified index.
742 /// <param name="index">Index of item.</param>
743 [EditorBrowsable(EditorBrowsableState.Never)]
744 public new void ScrollToIndex(int index)
746 ScrollTo(index, true, ItemScrollTo.Start);
750 /// Scroll to specific item's aligned position with or without animation.
752 /// <param name="index">Target item index of dataset.</param>
753 /// <param name="animate">Boolean flag of animation.</param>
754 /// <param name="align">Align state of item. See details in <see cref="ItemScrollTo"/>.</param>
755 /// <since_tizen> 9 </since_tizen>
756 public virtual void ScrollTo(int index, bool animate = false, ItemScrollTo align = ItemScrollTo.Nearest)
758 if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
759 if ((InternalItemSource == null) || needInitalizeLayouter)
761 delayedIndexScrollTo = true;
762 delayedIndexScrollToParam = (index, animate, align);
765 if (index < 0 || index >= InternalItemSource.Count)
767 throw new Exception("index is out of boundary. index should be a value between (0, " + InternalItemSource.Count.ToString() + ").");
770 float scrollPos, curPos, curSize, curItemSize;
771 (float x, float y) = ItemsLayouter.GetItemPosition(index);
772 (float width, float height) = ItemsLayouter.GetItemSize(index);
773 if (ScrollingDirection == Direction.Horizontal)
776 curPos = ScrollPosition.X;
777 curSize = Size.Width;
783 curPos = ScrollPosition.Y;
784 curSize = Size.Height;
785 curItemSize = height;
788 //Console.WriteLine("[NUI] ScrollTo [{0}:{1}], curPos{2}, itemPos{3}, curSize{4}, itemSize{5}", InternalItemSource.GetPosition(item), align, curPos, scrollPos, curSize, curItemSize);
791 case ItemScrollTo.Start:
794 case ItemScrollTo.Center:
795 scrollPos = scrollPos - (curSize / 2) + (curItemSize / 2);
797 case ItemScrollTo.End:
798 scrollPos = scrollPos - curSize + curItemSize;
800 case ItemScrollTo.Nearest:
801 if (scrollPos < curPos - curItemSize)
803 // item is placed before the current screen. scrollTo.Top
805 else if (scrollPos >= curPos + curSize + curItemSize)
807 // item is placed after the current screen. scrollTo.End
808 scrollPos = scrollPos - curSize + curItemSize;
812 // item is in the scroller. ScrollTo() is ignored.
818 //Console.WriteLine("[NUI] ScrollTo [{0}]-------------------", scrollPos);
819 base.ScrollTo(scrollPos, animate);
823 /// Apply style to CollectionView
825 /// <param name="viewStyle">The style to apply.</param>
826 [EditorBrowsable(EditorBrowsableState.Never)]
827 public override void ApplyStyle(ViewStyle viewStyle)
829 base.ApplyStyle(viewStyle);
830 if (viewStyle != null)
832 //Extension = RecyclerViewItemStyle.CreateExtension();
834 if (itemsLayouter != null)
836 string styleName = "Tizen.NUI.Compoenents." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
837 ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
838 if (layouterStyle != null)
839 itemsLayouter.Padding = new Extents(layouterStyle.Padding);
845 /// Scroll to specified item
848 /// Make sure that the item that is about to receive the accessibility highlight is visible.
850 [EditorBrowsable(EditorBrowsableState.Never)]
851 protected override bool AccessibilityScrollToChild(View child)
853 foreach (RecyclerViewItem item in ContentContainer.Children.Where((item) => item is RecyclerViewItem))
857 ScrollToIndex(item.Index);
865 // Realize and Decorate the item.
866 internal override RecyclerViewItem RealizeItem(int index)
868 RecyclerViewItem item;
869 if (index == 0 && Header != null)
875 if (index == InternalItemSource.Count - 1 && Footer != null)
883 var context = InternalItemSource.GetItem(index);
884 if (InternalItemSource.IsGroupHeader(index))
886 DataTemplate templ = (groupHeaderTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupHeaderTemplate;
888 RecyclerViewItem groupHeader = PopRecycleGroupCache(templ, true);
889 if (groupHeader == null)
891 groupHeader = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupHeaderTemplate, context, this);
893 groupHeader.Template = templ;
894 groupHeader.isGroupHeader = true;
895 groupHeader.isGroupFooter = false;
896 ContentContainer.Add(groupHeader);
899 if (groupHeader != null)
901 groupHeader.ParentItemsView = this;
902 groupHeader.Index = index;
903 groupHeader.ParentGroup = context;
904 groupHeader.BindingContext = context;
909 else if (InternalItemSource.IsGroupFooter(index))
911 DataTemplate templ = (groupFooterTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupFooterTemplate;
913 RecyclerViewItem groupFooter = PopRecycleGroupCache(templ, false);
914 if (groupFooter == null)
916 groupFooter = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupFooterTemplate, context, this);
918 groupFooter.Template = templ;
919 groupFooter.isGroupHeader = false;
920 groupFooter.isGroupFooter = true;
921 ContentContainer.Add(groupFooter);
924 if (groupFooter != null)
926 groupFooter.ParentItemsView = this;
927 groupFooter.Index = index;
928 groupFooter.ParentGroup = context;
929 groupFooter.BindingContext = context;
936 item = base.RealizeItem(index);
937 item.ParentGroup = InternalItemSource.GetGroupParent(index);
942 item = base.RealizeItem(index);
945 switch (SelectionMode)
947 case ItemSelectionMode.Single:
948 case ItemSelectionMode.SingleAlways:
949 if (item.BindingContext != null && item.BindingContext == SelectedItem)
951 item.IsSelected = true;
955 case ItemSelectionMode.Multiple:
956 if ((item.BindingContext != null) && (SelectedItems?.Contains(item.BindingContext) ?? false))
958 item.IsSelected = true;
961 case ItemSelectionMode.None:
962 item.IsSelectable = false;
968 // Unrealize and caching the item.
969 internal override void UnrealizeItem(RecyclerViewItem item, bool recycle = true)
971 if (item == null) return;
982 if (item.isGroupHeader || item.isGroupFooter)
985 item.ParentItemsView = null;
986 item.BindingContext = null;
987 item.IsPressed = false;
988 item.IsSelected = false;
989 item.IsEnabled = true;
991 //item.Relayout -= OnItemRelayout;
992 if (!recycle || !PushRecycleGroupCache(item))
993 Utility.Dispose(item);
997 base.UnrealizeItem(item, recycle);
1000 internal void SelectedItemsPropertyChanged(IList<object> oldSelection, IList<object> newSelection)
1002 if (suppressSelectionChangeNotification)
1007 foreach (RecyclerViewItem item in ContentContainer.Children.Where((item) => item is RecyclerViewItem))
1009 if (item.BindingContext == null) continue;
1010 if (newSelection.Contains(item.BindingContext))
1012 if (!item.IsSelected) item.IsSelected = true;
1016 if (item.IsSelected) item.IsSelected = false;
1019 SelectionPropertyChanged(this, new SelectionChangedEventArgs(oldSelection, newSelection));
1021 OnPropertyChanged(SelectedItemsProperty.PropertyName);
1025 /// Internal selection callback.
1027 /// <since_tizen> 9 </since_tizen>
1028 protected virtual void OnSelectionChanged(SelectionChangedEventArgs args)
1030 //Selection Callback
1034 /// Adjust scrolling position by own scrolling rules.
1035 /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
1037 /// <param name="position">Scroll position which is calculated by ScrollableBase</param>
1038 /// <returns>Adjusted scroll destination</returns>
1039 [EditorBrowsable(EditorBrowsableState.Never)]
1040 protected override float AdjustTargetPositionOfScrollAnimation(float position)
1042 // Destination is depending on implementation of layout manager.
1043 // Get destination from layout manager.
1044 return ItemsLayouter?.CalculateCandidateScrollPosition(position) ?? position;
1048 [EditorBrowsable(EditorBrowsableState.Never)]
1049 protected override void ClearCache()
1051 foreach (RecyclerViewItem item in recycleGroupHeaderCache)
1053 Utility.Dispose(item);
1055 recycleGroupHeaderCache.Clear();
1056 foreach (RecyclerViewItem item in recycleGroupFooterCache)
1058 Utility.Dispose(item);
1060 recycleGroupFooterCache.Clear();
1066 /// OnScroll event callback. Requesting layout to the layouter with given scrollPosition.
1068 /// <param name="source">Scroll source object</param>
1069 /// <param name="args">Scroll event argument</param>
1070 /// <since_tizen> 9 </since_tizen>
1071 protected override void OnScrolling(object source, ScrollEventArgs args)
1073 if (disposed) return;
1075 if (needInitalizeLayouter && (ItemsLayouter != null))
1077 ItemsLayouter.Initialize(this);
1078 needInitalizeLayouter = false;
1081 base.OnScrolling(source, args);
1085 /// Dispose ItemsView and all children on it.
1087 /// <param name="type">Dispose type.</param>
1088 /// <since_tizen> 9 </since_tizen>
1089 protected override void Dispose(DisposeTypes type)
1096 if (type == DisposeTypes.Explicit)
1098 // From now on, no need to use this properties,
1099 // so remove reference, to push it into garbage collector.
1101 // Arugable to disposing user-created members.
1105 Utility.Dispose(Header);
1110 Utility.Dispose(Footer);
1115 groupHeaderTemplate = null;
1116 groupFooterTemplate = null;
1118 if (selectedItem != null)
1120 selectedItem = null;
1122 if (selectedItems != null)
1124 selectedItems.Clear();
1125 selectedItems = null;
1127 if (InternalItemSource != null)
1129 InternalItemSource.Dispose();
1130 InternalItemSource = null;
1137 private static void SelectionPropertyChanged(CollectionView colView, SelectionChangedEventArgs args)
1139 var command = colView.SelectionChangedCommand;
1141 if (command != null)
1143 var commandParameter = colView.SelectionChangedCommandParameter;
1145 if (command.CanExecute(commandParameter))
1147 command.Execute(commandParameter);
1150 colView.SelectionChanged?.Invoke(colView, args);
1151 colView.OnSelectionChanged(args);
1154 private static object CoerceSelectedItems(BindableObject bindable, object value)
1158 return new SelectionList((CollectionView)bindable);
1161 if (value is SelectionList)
1166 return new SelectionList((CollectionView)bindable, value as IList<object>);
1169 private static void SelectionModePropertyChanged(BindableObject bindable, object oldValue, object newValue)
1171 var colView = (CollectionView)bindable;
1173 var oldMode = (ItemSelectionMode)oldValue;
1174 var newMode = (ItemSelectionMode)newValue;
1176 IList<object> previousSelection = new List<object>();
1177 IList<object> newSelection = new List<object>();
1181 case ItemSelectionMode.None:
1183 case ItemSelectionMode.Single:
1184 if (colView.SelectedItem != null)
1186 previousSelection.Add(colView.SelectedItem);
1189 case ItemSelectionMode.Multiple:
1190 previousSelection = colView.SelectedItems;
1196 case ItemSelectionMode.None:
1198 case ItemSelectionMode.Single:
1199 if (colView.SelectedItem != null)
1201 newSelection.Add(colView.SelectedItem);
1204 case ItemSelectionMode.Multiple:
1205 newSelection = colView.SelectedItems;
1209 if (previousSelection.Count == newSelection.Count)
1211 if (previousSelection.Count == 0 || (previousSelection[0] == newSelection[0]))
1213 // Both selections are empty or have the same single item; no reason to signal a change
1218 var args = new SelectionChangedEventArgs(previousSelection, newSelection);
1219 SelectionPropertyChanged(colView, args);
1224 if (ItemsSource == null) return;
1225 if (ItemsLayouter == null) return;
1226 if (ItemTemplate == null) return;
1228 if (disposed) return;
1229 if (needInitalizeLayouter)
1231 if (InternalItemSource == null) return;
1233 InternalItemSource.HasHeader = (header != null);
1234 InternalItemSource.HasFooter = (footer != null);
1237 if (!wasRelayouted) return;
1239 if (needInitalizeLayouter)
1241 itemsLayouter.Clear();
1244 ItemsLayouter.Initialize(this);
1245 needInitalizeLayouter = false;
1247 ItemsLayouter.RequestLayout(0.0f, true);
1249 if (delayedScrollTo)
1251 delayedScrollTo = false;
1252 ScrollTo(delayedScrollToParam.position, delayedScrollToParam.anim);
1255 if (delayedIndexScrollTo)
1257 delayedIndexScrollTo = false;
1258 ScrollTo(delayedIndexScrollToParam.index, delayedIndexScrollToParam.anim, delayedIndexScrollToParam.scrollTo);
1261 if (ScrollingDirection == Direction.Horizontal)
1263 ContentContainer.SizeWidth = ItemsLayouter.CalculateLayoutOrientationSize();
1267 ContentContainer.SizeHeight = ItemsLayouter.CalculateLayoutOrientationSize();
1271 private bool PushRecycleGroupCache(RecyclerViewItem item)
1273 if (item == null) throw new ArgumentNullException(nameof(item));
1274 if (RecycleCache.Count >= 20) return false;
1275 if (item.Template == null) return false;
1276 if (item.isGroupHeader)
1278 recycleGroupHeaderCache.Add(item);
1280 else if (item.isGroupFooter)
1282 recycleGroupFooterCache.Add(item);
1290 private RecyclerViewItem PopRecycleGroupCache(DataTemplate Template, bool isHeader)
1292 RecyclerViewItem viewItem = null;
1294 var Cache = (isHeader ? recycleGroupHeaderCache : recycleGroupFooterCache);
1295 for (int i = 0; i < Cache.Count; i++)
1297 viewItem = Cache[i];
1298 if (Template == viewItem.Template) break;
1301 if (viewItem != null)
1303 Cache.Remove(viewItem);
1308 private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
1310 switch (args.Action)
1312 case NotifyCollectionChangedAction.Add:
1314 case NotifyCollectionChangedAction.Remove:
1315 // Clear removed items.
1316 if (args.OldItems != null)
1318 if (args.OldItems.Contains(selectedItem))
1320 selectedItem = null;
1323 if (selectedItems != null)
1325 foreach (object removed in args.OldItems)
1327 if (selectedItems.Contains(removed))
1329 selectedItems.Remove(removed);
1335 case NotifyCollectionChangedAction.Replace:
1337 case NotifyCollectionChangedAction.Move:
1339 case NotifyCollectionChangedAction.Reset:
1342 throw new ArgumentOutOfRangeException(nameof(args));