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 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 View focusedView;
120 private int prevFocusedDataIndex = 0;
121 private List<RecyclerViewItem> recycleGroupHeaderCache { get; } = new List<RecyclerViewItem>();
122 private List<RecyclerViewItem> recycleGroupFooterCache { get; } = new List<RecyclerViewItem>();
123 private bool delayedScrollTo;
124 private (float position, bool anim) delayedScrollToParam;
126 private bool delayedIndexScrollTo;
127 private (int index, bool anim, ItemScrollTo scrollTo) delayedIndexScrollToParam;
130 /// Base constructor.
132 /// <since_tizen> 9 </since_tizen>
133 public CollectionView() : base()
136 SetKeyboardNavigationSupport(true);
140 /// Base constructor with ItemsSource
142 /// <param name="itemsSource">item's data source</param>
143 /// <since_tizen> 9 </since_tizen>
144 public CollectionView(IEnumerable itemsSource) : this()
146 ItemsSource = itemsSource;
150 /// Base constructor with ItemsSource, ItemsLayouter and ItemTemplate
152 /// <param name="itemsSource">item's data source</param>
153 /// <param name="layouter">item's layout manager</param>
154 /// <param name="template">item's view template with data bindings</param>
155 [EditorBrowsable(EditorBrowsableState.Never)]
156 public CollectionView(IEnumerable itemsSource, ItemsLayouter layouter, DataTemplate template) : this()
158 ItemsSource = itemsSource;
159 ItemTemplate = template;
160 ItemsLayouter = layouter;
164 /// Event of Selection changed.
165 /// previous selection list and current selection will be provided.
167 /// <since_tizen> 9 </since_tizen>
168 public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
171 /// Align item in the viewport when ScrollTo() calls.
173 /// <since_tizen> 9 </since_tizen>
174 public enum ItemScrollTo
177 /// Scroll to show item in nearest viewport on scroll direction.
178 /// item is above the scroll viewport, item will be came into front,
179 /// item is under the scroll viewport, item will be came into end,
180 /// item is in the scroll viewport, no scroll.
182 /// <since_tizen> 9 </since_tizen>
185 /// Scroll to show item in start of the viewport.
187 /// <since_tizen> 9 </since_tizen>
190 /// Scroll to show item in center of the viewport.
192 /// <since_tizen> 9 </since_tizen>
195 /// Scroll to show item in end of the viewport.
197 /// <since_tizen> 9 </since_tizen>
202 /// Item's source data in IEnumerable.
204 /// <since_tizen> 9 </since_tizen>
205 public override IEnumerable ItemsSource
213 if (itemsSource != null)
215 // Clearing old data!
216 if (itemsSource is INotifyCollectionChanged prevNotifyCollectionChanged)
218 prevNotifyCollectionChanged.CollectionChanged -= CollectionChanged;
220 itemsLayouter?.Clear();
221 if (selectedItem != null) selectedItem = null;
222 selectedItems?.Clear();
228 InternalItemSource?.Dispose();
229 InternalItemSource = null;
233 if (itemsSource is INotifyCollectionChanged newNotifyCollectionChanged)
235 newNotifyCollectionChanged.CollectionChanged += CollectionChanged;
238 InternalItemSource?.Dispose();
239 InternalItemSource = ItemsSourceFactory.Create(this);
241 if (itemsLayouter == null) return;
243 needInitalizeLayouter = true;
249 /// DataTemplate for items.
250 /// Create visual contents and binding properties.
251 /// return object type is restricted RecyclerViewItem.
252 /// <seealso cref="Tizen.NUI.Binding.DataTemplate" />
254 /// <since_tizen> 9 </since_tizen>
255 public override DataTemplate ItemTemplate
263 itemTemplate = value;
270 needInitalizeLayouter = true;
277 /// Layouting items on the scroll ContentContainer.
278 /// <seealso cref="ItemsLayouter" />
279 /// <seealso cref="LinearLayouter" />
280 /// <seealso cref="GridLayouter" />
282 /// <since_tizen> 9 </since_tizen>
283 public virtual ItemsLayouter ItemsLayouter
287 return itemsLayouter;
291 itemsLayouter = value;
292 base.InternalItemsLayouter = ItemsLayouter;
295 needInitalizeLayouter = false;
299 needInitalizeLayouter = true;
301 var styleName = "Tizen.NUI.Components." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
302 ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
303 if (layouterStyle != null)
305 itemsLayouter.Padding = new Extents(layouterStyle.Padding);
312 /// Scrolling direction to display items layout.
314 /// <since_tizen> 9 </since_tizen>
315 public new Direction ScrollingDirection
319 return base.ScrollingDirection;
323 if (base.ScrollingDirection != value)
325 base.ScrollingDirection = value;
326 needInitalizeLayouter = true;
333 /// Selected item in single selection.
335 /// <since_tizen> 9 </since_tizen>
336 public object SelectedItem
338 get => GetValue(SelectedItemProperty);
339 set => SetValue(SelectedItemProperty, value);
343 /// Selected items list in multiple selection.
345 /// <since_tizen> 9 </since_tizen>
346 public IList<object> SelectedItems
348 get => (IList<object>)GetValue(SelectedItemsProperty);
349 // set => SetValue(SelectedItemsProperty, new SelectionList(this, value));
353 /// Selection mode to handle items selection. See ItemSelectionMode for details.
355 /// <since_tizen> 9 </since_tizen>
356 public ItemSelectionMode SelectionMode
358 get => (ItemSelectionMode)GetValue(SelectionModeProperty);
359 set => SetValue(SelectionModeProperty, value);
363 /// Command of selection changed.
365 [EditorBrowsable(EditorBrowsableState.Never)]
366 public ICommand SelectionChangedCommand { set; get; }
369 /// Command parameter of selection changed.
371 [EditorBrowsable(EditorBrowsableState.Never)]
372 public object SelectionChangedCommandParameter { set; get; }
375 /// Header item placed in top-most position.
377 /// <remarks>Please note that, internal index will be increased by header.</remarks>
378 /// <since_tizen> 9 </since_tizen>
379 public RecyclerViewItem Header
386 //ContentContainer.Remove(header);
387 Utility.Dispose(header);
392 value.ParentItemsView = this;
393 value.IsHeader = true;
394 ContentContainer.Add(value);
397 needInitalizeLayouter = true;
403 /// Footer item placed in bottom-most position.
405 /// <remarks>Please note that, internal index will be increased by footer.</remarks>
406 /// <since_tizen> 9 </since_tizen>
407 public RecyclerViewItem Footer
414 //ContentContainer.Remove(footer);
415 Utility.Dispose(footer);
419 value.Index = InternalItemSource?.Count ?? 0;
420 value.ParentItemsView = this;
421 value.IsFooter = true;
422 ContentContainer.Add(value);
425 needInitalizeLayouter = true;
431 /// Enable groupable view.
433 [EditorBrowsable(EditorBrowsableState.Never)]
434 public bool IsGrouped
440 needInitalizeLayouter = true;
441 //Need to re-intialize Internal Item Source.
442 if (InternalItemSource != null)
444 InternalItemSource.Dispose();
445 InternalItemSource = null;
447 if (ItemsSource != null)
448 InternalItemSource = ItemsSourceFactory.Create(this);
454 /// DataTemplate of group header.
456 /// <remarks>Please note that, internal index will be increased by group header.
457 /// GroupHeaderTemplate is essential for groupable view.</remarks>
458 [EditorBrowsable(EditorBrowsableState.Never)]
459 public DataTemplate GroupHeaderTemplate
463 return groupHeaderTemplate;
467 groupHeaderTemplate = value;
468 needInitalizeLayouter = true;
474 /// DataTemplate of group footer. Group feature is not supported yet.
476 /// <remarks>Please note that, internal index will be increased by group footer.</remarks>
477 [EditorBrowsable(EditorBrowsableState.Never)]
478 public DataTemplate GroupFooterTemplate
482 return groupFooterTemplate;
486 groupFooterTemplate = value;
487 needInitalizeLayouter = true;
493 /// Internal encapsulated items data source.
495 internal new IGroupableItemSource InternalItemSource
499 return (base.InternalItemSource as IGroupableItemSource);
503 base.InternalItemSource = value;
508 /// Size strategy of measuring scroll content. see details in ItemSizingStrategy.
510 [EditorBrowsable(EditorBrowsableState.Never)]
511 internal ItemSizingStrategy SizingStrategy { get; set; }
514 /// <since_tizen> 9 </since_tizen>
515 public override void OnRelayout(Vector2 size, RelayoutContainer container)
517 base.OnRelayout(size, container);
519 wasRelayouted = true;
520 if (needInitalizeLayouter) Init();
524 [EditorBrowsable(EditorBrowsableState.Never)]
525 public override void NotifyDataSetChanged()
527 if (selectedItem != null)
531 if (selectedItems != null)
533 selectedItems.Clear();
536 base.NotifyDataSetChanged();
540 [EditorBrowsable(EditorBrowsableState.Never)]
541 public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
543 View nextFocusedView = null;
545 if (focusedView == null)
547 // If focusedView is null, find child which has previous data index
548 if (ContentContainer.Children.Count > 0 && InternalItemSource.Count > 0)
550 for (int i = 0; i < ContentContainer.Children.Count; i++)
552 RecyclerViewItem item = Children[i] as RecyclerViewItem;
553 if (item?.Index == prevFocusedDataIndex)
555 nextFocusedView = item;
563 // If this is not first focus, request next focus to Layouter
564 nextFocusedView = ItemsLayouter.RequestNextFocusableView(currentFocusedView, direction, loopEnabled);
567 if (nextFocusedView != null)
569 // Check next focused view is inside of visible area.
570 // If it is not, move scroll position to make it visible.
571 Position scrollPosition = ContentContainer.CurrentPosition;
572 float targetPosition = -(ScrollingDirection == Direction.Horizontal ? scrollPosition.X : scrollPosition.Y);
574 float left = nextFocusedView.Position.X;
575 float right = nextFocusedView.Position.X + nextFocusedView.Size.Width;
576 float top = nextFocusedView.Position.Y;
577 float bottom = nextFocusedView.Position.Y + nextFocusedView.Size.Height;
579 float visibleRectangleLeft = -scrollPosition.X;
580 float visibleRectangleRight = -scrollPosition.X + Size.Width;
581 float visibleRectangleTop = -scrollPosition.Y;
582 float visibleRectangleBottom = -scrollPosition.Y + Size.Height;
584 if (ScrollingDirection == Direction.Horizontal)
586 if ((direction == View.FocusDirection.Left || direction == View.FocusDirection.Up) && left < visibleRectangleLeft)
588 targetPosition = left;
590 else if ((direction == View.FocusDirection.Right || direction == View.FocusDirection.Down) && right > visibleRectangleRight)
592 targetPosition = right - Size.Width;
597 if ((direction == View.FocusDirection.Up || direction == View.FocusDirection.Left) && top < visibleRectangleTop)
599 targetPosition = top;
601 else if ((direction == View.FocusDirection.Down || direction == View.FocusDirection.Right) && bottom > visibleRectangleBottom)
603 targetPosition = bottom - Size.Height;
607 focusedView = nextFocusedView;
608 prevFocusedDataIndex = (nextFocusedView as RecyclerViewItem)?.Index ?? -1;
610 ScrollTo(targetPosition, true);
614 // If nextView is null, it means that we should move focus to outside of Control.
615 // Return FocusableView depending on direction.
618 case View.FocusDirection.Left:
620 nextFocusedView = LeftFocusableView;
623 case View.FocusDirection.Right:
625 nextFocusedView = RightFocusableView;
628 case View.FocusDirection.Up:
630 nextFocusedView = UpFocusableView;
633 case View.FocusDirection.Down:
635 nextFocusedView = DownFocusableView;
640 if (nextFocusedView != null)
646 //If FocusableView doesn't exist, not move focus.
647 nextFocusedView = focusedView;
651 return nextFocusedView;
655 /// Update selected items list in multiple selection.
657 /// <param name="newSelection">updated selection list by user</param>
658 /// <since_tizen> 9 </since_tizen>
659 public void UpdateSelectedItems(IList<object> newSelection)
661 var oldSelection = new List<object>(SelectedItems);
663 suppressSelectionChangeNotification = true;
665 SelectedItems.Clear();
667 if (newSelection?.Count > 0)
669 for (int n = 0; n < newSelection.Count; n++)
671 SelectedItems.Add(newSelection[n]);
675 suppressSelectionChangeNotification = false;
677 SelectedItemsPropertyChanged(oldSelection, newSelection);
681 /// Scroll to specific position with or without animation.
683 /// <param name="position">Destination.</param>
684 /// <param name="animate">Scroll with or without animation</param>
685 /// <since_tizen> 9 </since_tizen>
686 public new void ScrollTo(float position, bool animate)
688 if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
689 if ((InternalItemSource == null) || needInitalizeLayouter)
691 delayedScrollTo = true;
692 delayedScrollToParam = (position, animate);
696 base.ScrollTo(position, animate);
700 /// Scrolls to the item at the specified index.
702 /// <param name="index">Index of item.</param>
703 [EditorBrowsable(EditorBrowsableState.Never)]
704 public new void ScrollToIndex(int index)
706 ScrollTo(index, true, ItemScrollTo.Start);
710 /// Scroll to specific item's aligned position with or without animation.
712 /// <param name="index">Target item index of dataset.</param>
713 /// <param name="animate">Boolean flag of animation.</param>
714 /// <param name="align">Align state of item. See details in <see cref="ItemScrollTo"/>.</param>
715 /// <since_tizen> 9 </since_tizen>
716 public virtual void ScrollTo(int index, bool animate = false, ItemScrollTo align = ItemScrollTo.Nearest)
718 if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
719 if ((InternalItemSource == null) || needInitalizeLayouter)
721 delayedIndexScrollTo = true;
722 delayedIndexScrollToParam = (index, animate, align);
725 if (index < 0 || index >= InternalItemSource.Count)
727 throw new Exception("index is out of boundary. index should be a value between (0, " + InternalItemSource.Count.ToString() + ").");
730 float scrollPos, curPos, curSize, curItemSize;
731 (float x, float y) = ItemsLayouter.GetItemPosition(index);
732 (float width, float height) = ItemsLayouter.GetItemSize(index);
733 if (ScrollingDirection == Direction.Horizontal)
736 curPos = ScrollPosition.X;
737 curSize = Size.Width;
743 curPos = ScrollPosition.Y;
744 curSize = Size.Height;
745 curItemSize = height;
748 //Console.WriteLine("[NUI] ScrollTo [{0}:{1}], curPos{2}, itemPos{3}, curSize{4}, itemSize{5}", InternalItemSource.GetPosition(item), align, curPos, scrollPos, curSize, curItemSize);
751 case ItemScrollTo.Start:
754 case ItemScrollTo.Center:
755 scrollPos = scrollPos - (curSize / 2) + (curItemSize / 2);
757 case ItemScrollTo.End:
758 scrollPos = scrollPos - curSize + curItemSize;
760 case ItemScrollTo.Nearest:
761 if (scrollPos < curPos - curItemSize)
763 // item is placed before the current screen. scrollTo.Top
765 else if (scrollPos >= curPos + curSize + curItemSize)
767 // item is placed after the current screen. scrollTo.End
768 scrollPos = scrollPos - curSize + curItemSize;
772 // item is in the scroller. ScrollTo() is ignored.
778 //Console.WriteLine("[NUI] ScrollTo [{0}]-------------------", scrollPos);
779 base.ScrollTo(scrollPos, animate);
783 /// Apply style to CollectionView
785 /// <param name="viewStyle">The style to apply.</param>
786 [EditorBrowsable(EditorBrowsableState.Never)]
787 public override void ApplyStyle(ViewStyle viewStyle)
789 base.ApplyStyle(viewStyle);
790 if (viewStyle != null)
792 //Extension = RecyclerViewItemStyle.CreateExtension();
794 if (itemsLayouter != null)
796 string styleName = "Tizen.NUI.Compoenents." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
797 ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
798 if (layouterStyle != null)
799 itemsLayouter.Padding = new Extents(layouterStyle.Padding);
803 // Realize and Decorate the item.
804 internal override RecyclerViewItem RealizeItem(int index)
806 RecyclerViewItem item;
807 if (index == 0 && Header != null)
813 if (index == InternalItemSource.Count - 1 && Footer != null)
821 var context = InternalItemSource.GetItem(index);
822 if (InternalItemSource.IsGroupHeader(index))
824 DataTemplate templ = (groupHeaderTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupHeaderTemplate;
826 RecyclerViewItem groupHeader = PopRecycleGroupCache(templ, true);
827 if (groupHeader == null)
829 groupHeader = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupHeaderTemplate, context, this);
831 groupHeader.Template = templ;
832 groupHeader.isGroupHeader = true;
833 groupHeader.isGroupFooter = false;
834 ContentContainer.Add(groupHeader);
836 groupHeader.ParentItemsView = this;
837 groupHeader.Index = index;
838 groupHeader.ParentGroup = context;
839 groupHeader.BindingContext = context;
843 else if (InternalItemSource.IsGroupFooter(index))
845 DataTemplate templ = (groupFooterTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupFooterTemplate;
847 RecyclerViewItem groupFooter = PopRecycleGroupCache(templ, false);
848 if (groupFooter == null)
850 groupFooter = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupFooterTemplate, context, this);
852 groupFooter.Template = templ;
853 groupFooter.isGroupHeader = false;
854 groupFooter.isGroupFooter = true;
855 ContentContainer.Add(groupFooter);
857 groupFooter.ParentItemsView = this;
858 groupFooter.Index = index;
859 groupFooter.ParentGroup = context;
860 groupFooter.BindingContext = context;
867 item = base.RealizeItem(index);
868 item.ParentGroup = InternalItemSource.GetGroupParent(index);
873 item = base.RealizeItem(index);
876 switch (SelectionMode)
878 case ItemSelectionMode.Single:
879 case ItemSelectionMode.SingleAlways:
880 if (item.BindingContext != null && item.BindingContext == SelectedItem)
882 item.IsSelected = true;
886 case ItemSelectionMode.Multiple:
887 if ((item.BindingContext != null) && (SelectedItems?.Contains(item.BindingContext) ?? false))
889 item.IsSelected = true;
892 case ItemSelectionMode.None:
893 item.IsSelectable = false;
899 // Unrealize and caching the item.
900 internal override void UnrealizeItem(RecyclerViewItem item, bool recycle = true)
902 if (item == null) return;
913 if (item.isGroupHeader || item.isGroupFooter)
916 item.ParentItemsView = null;
917 item.BindingContext = null;
918 item.IsPressed = false;
919 item.IsSelected = false;
920 item.IsEnabled = true;
922 //item.Relayout -= OnItemRelayout;
923 if (!recycle || !PushRecycleGroupCache(item))
924 Utility.Dispose(item);
928 base.UnrealizeItem(item, recycle);
931 internal void SelectedItemsPropertyChanged(IList<object> oldSelection, IList<object> newSelection)
933 if (suppressSelectionChangeNotification)
938 foreach (RecyclerViewItem item in ContentContainer.Children.Where((item) => item is RecyclerViewItem))
940 if (item.BindingContext == null) continue;
941 if (newSelection.Contains(item.BindingContext))
943 if (!item.IsSelected) item.IsSelected = true;
947 if (item.IsSelected) item.IsSelected = false;
950 SelectionPropertyChanged(this, new SelectionChangedEventArgs(oldSelection, newSelection));
952 OnPropertyChanged(SelectedItemsProperty.PropertyName);
956 /// Internal selection callback.
958 /// <since_tizen> 9 </since_tizen>
959 protected virtual void OnSelectionChanged(SelectionChangedEventArgs args)
965 /// Adjust scrolling position by own scrolling rules.
966 /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
968 /// <param name="position">Scroll position which is calculated by ScrollableBase</param>
969 /// <returns>Adjusted scroll destination</returns>
970 [EditorBrowsable(EditorBrowsableState.Never)]
971 protected override float AdjustTargetPositionOfScrollAnimation(float position)
973 // Destination is depending on implementation of layout manager.
974 // Get destination from layout manager.
975 return ItemsLayouter?.CalculateCandidateScrollPosition(position) ?? position;
979 /// OnScroll event callback. Requesting layout to the layouter with given scrollPosition.
981 /// <param name="source">Scroll source object</param>
982 /// <param name="args">Scroll event argument</param>
983 /// <since_tizen> 9 </since_tizen>
984 protected override void OnScrolling(object source, ScrollEventArgs args)
986 if (disposed) return;
988 if (needInitalizeLayouter && (ItemsLayouter != null))
990 ItemsLayouter.Initialize(this);
991 needInitalizeLayouter = false;
994 base.OnScrolling(source, args);
998 /// Dispose ItemsView and all children on it.
1000 /// <param name="type">Dispose type.</param>
1001 /// <since_tizen> 9 </since_tizen>
1002 protected override void Dispose(DisposeTypes type)
1009 if (type == DisposeTypes.Explicit)
1011 // From now on, no need to use this properties,
1012 // so remove reference, to push it into garbage collector.
1014 // Arugable to disposing user-created members.
1018 Utility.Dispose(Header);
1023 Utility.Dispose(Footer);
1028 groupHeaderTemplate = null;
1029 groupFooterTemplate = null;
1031 if (selectedItem != null)
1033 selectedItem = null;
1035 if (selectedItems != null)
1037 selectedItems.Clear();
1038 selectedItems = null;
1040 if (InternalItemSource != null)
1042 InternalItemSource.Dispose();
1043 InternalItemSource = null;
1045 if (recycleGroupHeaderCache != null)
1047 foreach(RecyclerViewItem item in recycleGroupHeaderCache)
1049 UnrealizeItem(item, false);
1051 recycleGroupHeaderCache.Clear();
1053 if (recycleGroupFooterCache != null)
1055 foreach(RecyclerViewItem item in recycleGroupFooterCache)
1057 UnrealizeItem(item, false);
1059 recycleGroupFooterCache.Clear();
1066 private static void SelectionPropertyChanged(CollectionView colView, SelectionChangedEventArgs args)
1068 var command = colView.SelectionChangedCommand;
1070 if (command != null)
1072 var commandParameter = colView.SelectionChangedCommandParameter;
1074 if (command.CanExecute(commandParameter))
1076 command.Execute(commandParameter);
1079 colView.SelectionChanged?.Invoke(colView, args);
1080 colView.OnSelectionChanged(args);
1083 private static object CoerceSelectedItems(BindableObject bindable, object value)
1087 return new SelectionList((CollectionView)bindable);
1090 if (value is SelectionList)
1095 return new SelectionList((CollectionView)bindable, value as IList<object>);
1098 private static void SelectionModePropertyChanged(BindableObject bindable, object oldValue, object newValue)
1100 var colView = (CollectionView)bindable;
1102 var oldMode = (ItemSelectionMode)oldValue;
1103 var newMode = (ItemSelectionMode)newValue;
1105 IList<object> previousSelection = new List<object>();
1106 IList<object> newSelection = new List<object>();
1110 case ItemSelectionMode.None:
1112 case ItemSelectionMode.Single:
1113 if (colView.SelectedItem != null)
1115 previousSelection.Add(colView.SelectedItem);
1118 case ItemSelectionMode.Multiple:
1119 previousSelection = colView.SelectedItems;
1125 case ItemSelectionMode.None:
1127 case ItemSelectionMode.Single:
1128 if (colView.SelectedItem != null)
1130 newSelection.Add(colView.SelectedItem);
1133 case ItemSelectionMode.Multiple:
1134 newSelection = colView.SelectedItems;
1138 if (previousSelection.Count == newSelection.Count)
1140 if (previousSelection.Count == 0 || (previousSelection[0] == newSelection[0]))
1142 // Both selections are empty or have the same single item; no reason to signal a change
1147 var args = new SelectionChangedEventArgs(previousSelection, newSelection);
1148 SelectionPropertyChanged(colView, args);
1153 if (ItemsSource == null) return;
1154 if (ItemsLayouter == null) return;
1155 if (ItemTemplate == null) return;
1157 if (disposed) return;
1158 if (needInitalizeLayouter)
1160 if (InternalItemSource == null) return;
1162 InternalItemSource.HasHeader = (header != null);
1163 InternalItemSource.HasFooter = (footer != null);
1166 if (!wasRelayouted) return;
1168 if (needInitalizeLayouter)
1170 ItemsLayouter.Initialize(this);
1171 needInitalizeLayouter = false;
1173 ItemsLayouter.RequestLayout(0.0f, true);
1175 if (delayedScrollTo)
1177 delayedScrollTo = false;
1178 ScrollTo(delayedScrollToParam.position, delayedScrollToParam.anim);
1181 if (delayedIndexScrollTo)
1183 delayedIndexScrollTo = false;
1184 ScrollTo(delayedIndexScrollToParam.index, delayedIndexScrollToParam.anim, delayedIndexScrollToParam.scrollTo);
1187 if (ScrollingDirection == Direction.Horizontal)
1189 ContentContainer.SizeWidth = ItemsLayouter.CalculateLayoutOrientationSize();
1193 ContentContainer.SizeHeight = ItemsLayouter.CalculateLayoutOrientationSize();
1197 private bool PushRecycleGroupCache(RecyclerViewItem item)
1199 if (item == null) throw new ArgumentNullException(nameof(item));
1200 if (RecycleCache.Count >= 20) return false;
1201 if (item.Template == null) return false;
1202 if (item.isGroupHeader)
1204 recycleGroupHeaderCache.Add(item);
1206 else if (item.isGroupFooter)
1208 recycleGroupFooterCache.Add(item);
1216 private RecyclerViewItem PopRecycleGroupCache(DataTemplate Template, bool isHeader)
1218 RecyclerViewItem viewItem = null;
1220 var Cache = (isHeader ? recycleGroupHeaderCache : recycleGroupFooterCache);
1221 for (int i = 0; i < Cache.Count; i++)
1223 viewItem = Cache[i];
1224 if (Template == viewItem.Template) break;
1227 if (viewItem != null)
1229 Cache.Remove(viewItem);
1234 private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
1236 switch (args.Action)
1238 case NotifyCollectionChangedAction.Add:
1240 case NotifyCollectionChangedAction.Remove:
1241 // Clear removed items.
1242 if (args.OldItems != null)
1244 if (args.OldItems.Contains(selectedItem))
1246 selectedItem = null;
1249 if (selectedItems != null)
1251 foreach (object removed in args.OldItems)
1253 if (selectedItems.Contains(removed))
1255 selectedItems.Remove(removed);
1261 case NotifyCollectionChangedAction.Replace:
1263 case NotifyCollectionChangedAction.Move:
1265 case NotifyCollectionChangedAction.Reset:
1268 throw new ArgumentOutOfRangeException(nameof(args));