BindableProperty.Create(nameof(SelectedItem), typeof(object), typeof(CollectionView), null,
propertyChanged: (bindable, oldValue, newValue) =>
{
- var colView = (CollectionView)bindable;
+ var colView = bindable as CollectionView;
+ if (colView == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
+
oldValue = colView.selectedItem;
colView.selectedItem = newValue;
var args = new SelectionChangedEventArgs(oldValue, newValue);
foreach (RecyclerViewItem item in colView.ContentContainer.Children.Where((item) => item is RecyclerViewItem))
{
- if (item.BindingContext == null) continue;
- if (item.BindingContext == oldValue) item.IsSelected = false;
- else if (item.BindingContext == newValue) item.IsSelected = true;
+ if (item.BindingContext == null)
+ {
+ continue;
+ }
+
+ if (item.BindingContext == oldValue)
+ {
+ item.IsSelected = false;
+ }
+ else if (item.BindingContext == newValue)
+ {
+ item.IsSelected = true;
+ }
}
SelectionPropertyChanged(colView, args);
},
defaultValueCreator: (bindable) =>
{
- var colView = (CollectionView)bindable;
+ var colView = bindable as CollectionView;
+ if (colView == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
+
return colView.selectedItem;
});
BindableProperty.Create(nameof(SelectedItems), typeof(IList<object>), typeof(CollectionView), null,
propertyChanged: (bindable, oldValue, newValue) =>
{
- var colView = (CollectionView)bindable;
+ var colView = bindable as CollectionView;
+ if (colView == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
+
var oldSelection = colView.selectedItems ?? selectEmpty;
//FIXME : CoerceSelectedItems calls only isCreatedByXaml
var newSelection = (SelectionList)CoerceSelectedItems(colView, newValue);
},
defaultValueCreator: (bindable) =>
{
- var colView = (CollectionView)bindable;
+ var colView = bindable as CollectionView;
+ if (colView == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
+
colView.selectedItems = colView.selectedItems ?? new SelectionList(colView);
return colView.selectedItems;
});
BindableProperty.Create(nameof(SelectionMode), typeof(ItemSelectionMode), typeof(CollectionView), ItemSelectionMode.None,
propertyChanged: (bindable, oldValue, newValue) =>
{
- var colView = (CollectionView)bindable;
+ var colView = bindable as CollectionView;
+ if (colView == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
+
oldValue = colView.selectionMode;
colView.selectionMode = (ItemSelectionMode)newValue;
SelectionModePropertyChanged(colView, oldValue, newValue);
},
defaultValueCreator: (bindable) =>
{
- var colView = (CollectionView)bindable;
+ var colView = bindable as CollectionView;
+ if (colView == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
+
return colView.selectionMode;
});
/// <since_tizen> 9 </since_tizen>
public override IEnumerable ItemsSource
{
- get => (IEnumerable)GetValue(RecyclerView.ItemsSourceProperty);
+ get => GetValue(RecyclerView.ItemsSourceProperty) as IEnumerable;
set => SetValue(RecyclerView.ItemsSourceProperty, value);
}
{
prevNotifyCollectionChanged.CollectionChanged -= CollectionChanged;
}
- if (selectedItem != null) selectedItem = null;
+ if (selectedItem != null)
+ {
+ selectedItem = null;
+ }
selectedItems?.Clear();
}
- itemsSource = (IEnumerable)value;
+ itemsSource = value as IEnumerable;
if (itemsSource == null)
{
if (itemsLayouter == null) return;
needInitalizeLayouter = true;
- Init();
+ ReinitializeLayout();
}
}
}
needInitalizeLayouter = true;
- Init();
+ ReinitializeLayout();
}
}
{
itemsLayouter.Padding = new Extents(layouterStyle.Padding);
}
- Init();
+ ReinitializeLayout();
}
}
{
base.ScrollingDirection = value;
needInitalizeLayouter = true;
- Init();
+ ReinitializeLayout();
}
}
}
/// <since_tizen> 9 </since_tizen>
public IList<object> SelectedItems
{
- get => (IList<object>)GetValue(SelectedItemsProperty);
+ get => GetValue(SelectedItemsProperty) as IList<object>;
// set => SetValue(SelectedItemsProperty, new SelectionList(this, value));
}
InternalItemSource.HasHeader = (value != null);
}
needInitalizeLayouter = true;
- Init();
+ ReinitializeLayout();
}
}
InternalItemSource.HasFooter = (value != null);
}
needInitalizeLayouter = true;
- Init();
+ ReinitializeLayout();
}
}
InternalItemSource = null;
}
if (ItemsSource != null)
+ {
InternalItemSource = ItemsSourceFactory.Create(this);
- Init();
+ }
+
+ ReinitializeLayout();
}
}
{
groupHeaderTemplate = value;
needInitalizeLayouter = true;
+
//Need to re-intialize Internal Item Source.
if (InternalItemSource != null)
{
InternalItemSource.Dispose();
InternalItemSource = null;
}
+
if (ItemsSource != null)
+ {
InternalItemSource = ItemsSourceFactory.Create(this);
- Init();
+ }
+
+ ReinitializeLayout();
}
}
{
groupFooterTemplate = value;
needInitalizeLayouter = true;
+
//Need to re-intialize Internal Item Source.
if (InternalItemSource != null)
{
InternalItemSource.Dispose();
InternalItemSource = null;
}
+
if (ItemsSource != null)
+ {
InternalItemSource = ItemsSourceFactory.Create(this);
- Init();
+ }
+
+ ReinitializeLayout();
}
}
base.OnRelayout(size, container);
wasRelayouted = true;
- if (needInitalizeLayouter) Init();
+ if (needInitalizeLayouter)
+ {
+ ReinitializeLayout();
+ }
}
/// <inheritdoc/>
/// <since_tizen> 9 </since_tizen>
public new void ScrollTo(float position, bool animate)
{
- if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
+ if (ItemsLayouter == null)
+ {
+ throw new Exception("Item Layouter must exist.");
+ }
+
if ((InternalItemSource == null) || needInitalizeLayouter)
{
delayedScrollTo = true;
/// <since_tizen> 9 </since_tizen>
public virtual void ScrollTo(int index, bool animate = false, ItemScrollTo align = ItemScrollTo.Nearest)
{
- if (ItemsLayouter == null) throw new Exception("Item Layouter must exist.");
+ if (ItemsLayouter == null)
+ {
+ throw new Exception("Item Layouter must exist.");
+ }
+
if ((InternalItemSource == null) || needInitalizeLayouter)
{
delayedIndexScrollTo = true;
delayedIndexScrollToParam = (index, animate, align);
return;
}
+
if (index < 0 || index >= InternalItemSource.Count)
{
throw new Exception("index is out of boundary. index should be a value between (0, " + InternalItemSource.Count.ToString() + ").");
float scrollPos, curPos, curSize, curItemSize;
(float x, float y) = ItemsLayouter.GetItemPosition(index);
(float width, float height) = ItemsLayouter.GetItemSize(index);
+
if (ScrollingDirection == Direction.Horizontal)
{
scrollPos = x;
string styleName = "Tizen.NUI.Compoenents." + (itemsLayouter is LinearLayouter? "LinearLayouter" : (itemsLayouter is GridLayouter ? "GridLayouter" : "ItemsLayouter"));
ViewStyle layouterStyle = ThemeManager.GetStyle(styleName);
if (layouterStyle != null)
+ {
itemsLayouter.Padding = new Extents(layouterStyle.Padding);
+ }
}
}
}
// Realize and Decorate the item.
+
internal override RecyclerViewItem RealizeItem(int index)
{
RecyclerViewItem item;
var context = InternalItemSource.GetItem(index);
if (InternalItemSource.IsGroupHeader(index))
{
- DataTemplate templ = (groupHeaderTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupHeaderTemplate;
-
- RecyclerViewItem groupHeader = PopRecycleGroupCache(templ, true);
- if (groupHeader == null)
- {
- groupHeader = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupHeaderTemplate, context, this);
-
- groupHeader.Template = templ;
- groupHeader.isGroupHeader = true;
- groupHeader.isGroupFooter = false;
- ContentContainer.Add(groupHeader);
- }
-
- if (groupHeader != null)
- {
- groupHeader.ParentItemsView = this;
- groupHeader.Index = index;
- groupHeader.ParentGroup = context;
- groupHeader.BindingContext = context;
- }
- //group selection?
- item = groupHeader;
+ item = RealizeGroupHeader(index, context);
}
else if (InternalItemSource.IsGroupFooter(index))
{
- DataTemplate templ = (groupFooterTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupFooterTemplate;
-
- RecyclerViewItem groupFooter = PopRecycleGroupCache(templ, false);
- if (groupFooter == null)
- {
- groupFooter = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupFooterTemplate, context, this);
- groupFooter.Template = templ;
- groupFooter.isGroupHeader = false;
- groupFooter.isGroupFooter = true;
- ContentContainer.Add(groupFooter);
- }
-
- if (groupFooter != null)
- {
- groupFooter.ParentItemsView = this;
- groupFooter.Index = index;
- groupFooter.ParentGroup = context;
- groupFooter.BindingContext = context;
- }
//group selection?
- item = groupFooter;
+ item = RealizeGroupFooter(index, context);
}
else
{
item = base.RealizeItem(index);
if (item == null)
+ {
throw new Exception("Item realize failed by Null content return.");
+ }
item.ParentGroup = InternalItemSource.GetGroupParent(index);
}
}
// Unrealize and caching the item.
internal override void UnrealizeItem(RecyclerViewItem item, bool recycle = true)
{
- if (item == null) return;
+ if (item == null)
+ {
+ return;
+ }
+
if (item == Header)
{
item?.Hide();
item.Hide();
return;
}
+
if (item.isGroupHeader || item.isGroupFooter)
{
item.Index = -1;
item.UpdateState();
//item.Relayout -= OnItemRelayout;
if (!recycle || !PushRecycleGroupCache(item))
+ {
Utility.Dispose(item);
+ }
return;
}
if (item.BindingContext == null) continue;
if (newSelection.Contains(item.BindingContext))
{
- if (!item.IsSelected) item.IsSelected = true;
+ if (!item.IsSelected)
+ {
+ item.IsSelected = true;
+ }
}
else
{
- if (item.IsSelected) item.IsSelected = false;
+ if (item.IsSelected)
+ {
+ item.IsSelected = false;
+ }
}
}
SelectionPropertyChanged(this, new SelectionChangedEventArgs(oldSelection, newSelection));
private static object CoerceSelectedItems(BindableObject bindable, object value)
{
+ var colView = bindable as CollectionView;
if (value == null)
{
- return new SelectionList((CollectionView)bindable);
+ return new SelectionList(colView);
}
if (value is SelectionList)
return value;
}
- return new SelectionList((CollectionView)bindable, value as IList<object>);
+ return new SelectionList(colView, value as IList<object>);
}
private static void SelectionModePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
- var colView = (CollectionView)bindable;
+ var colView = bindable as CollectionView;
var oldMode = (ItemSelectionMode)oldValue;
var newMode = (ItemSelectionMode)newValue;
SelectionPropertyChanged(colView, args);
}
- private void Init()
+ private void ReinitializeLayout()
{
- if (ItemsSource == null) return;
- if (ItemsLayouter == null) return;
- if (ItemTemplate == null) return;
-
- if (disposed) return;
- if (needInitalizeLayouter)
+ if (ItemsSource == null || ItemsLayouter == null || ItemTemplate == null)
{
- if (InternalItemSource == null) return;
+ return;
+ }
- InternalItemSource.HasHeader = (header != null);
- InternalItemSource.HasFooter = (footer != null);
+ if (disposed)
+ {
+ return;
}
- if (!wasRelayouted) return;
+ if (!wasRelayouted)
+ {
+ return;
+ }
if (needInitalizeLayouter)
{
+ if (InternalItemSource == null)
+ {
+ return;
+ }
+
+ InternalItemSource.HasHeader = (header != null);
+ InternalItemSource.HasFooter = (footer != null);
+
itemsLayouter.Clear();
ClearCache();
ItemsLayouter.Initialize(this);
needInitalizeLayouter = false;
}
+
ItemsLayouter.RequestLayout(0.0f, true);
if (delayedScrollTo)
private bool PushRecycleGroupCache(RecyclerViewItem item)
{
- if (item == null) throw new ArgumentNullException(nameof(item));
- if (RecycleCache.Count >= 20) return false;
- if (item.Template == null) return false;
+ if (item == null)
+ {
+ throw new ArgumentNullException(nameof(item));
+ }
+
+ if (item.Template == null || RecycleCache.Count >= 20)
+ {
+ return false;
+ }
+
if (item.isGroupHeader)
{
recycleGroupHeaderCache.Add(item);
{
recycleGroupFooterCache.Add(item);
}
- else return false;
+ else
+ {
+ return false;
+ }
+
item.Hide();
item.Index = -1;
+
return true;
}
for (int i = 0; i < Cache.Count; i++)
{
viewItem = Cache[i];
- if (Template == viewItem.Template) break;
+ if (Template == viewItem.Template)
+ {
+ break;
+ }
}
if (viewItem != null)
Cache.Remove(viewItem);
viewItem.Show();
}
+
return viewItem;
}
+
+ private RecyclerViewItem RealizeGroupHeader(int index, object context)
+ {
+ DataTemplate templ = (groupHeaderTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupHeaderTemplate;
+
+ RecyclerViewItem groupHeader = PopRecycleGroupCache(templ, true);
+
+ if (groupHeader == null)
+ {
+ groupHeader = (RecyclerViewItem)DataTemplateExtensions.CreateContent(groupHeaderTemplate, context, this);
+
+ groupHeader.Template = templ;
+ groupHeader.isGroupHeader = true;
+ groupHeader.isGroupFooter = false;
+ ContentContainer.Add(groupHeader);
+ }
+
+ if (groupHeader != null)
+ {
+ groupHeader.ParentItemsView = this;
+ groupHeader.Index = index;
+ groupHeader.ParentGroup = context;
+ groupHeader.BindingContext = context;
+
+ return groupHeader;
+ }
+
+ return null;
+ }
+
+ private RecyclerViewItem RealizeGroupFooter(int index, object context)
+ {
+ DataTemplate templ = (groupFooterTemplate as DataTemplateSelector)?.SelectDataTemplate(context, this) ?? groupFooterTemplate;
+
+ RecyclerViewItem groupFooter = PopRecycleGroupCache(templ, false);
+
+ if (groupFooter == null)
+ {
+ groupFooter = DataTemplateExtensions.CreateContent(groupFooterTemplate, context, this) as RecyclerViewItem;
+
+ groupFooter.Template = templ;
+ groupFooter.isGroupHeader = false;
+ groupFooter.isGroupFooter = true;
+ ContentContainer.Add(groupFooter);
+ }
+
+ if (groupFooter != null)
+ {
+ groupFooter.ParentItemsView = this;
+ groupFooter.Index = index;
+ groupFooter.ParentGroup = context;
+ groupFooter.BindingContext = context;
+ return groupFooter;
+ }
+
+ return null;
+ }
+
private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
+using System;
using System.ComponentModel;
using System.Windows.Input;
using Tizen.NUI.Binding;
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty ItemsLayouterProperty = BindableProperty.Create(nameof(ItemsLayouter), typeof(ItemsLayouter), typeof(CollectionView), null, propertyChanged: (bindable, oldValue, newValue) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
if (newValue != null)
{
instance.InternalItemsLayouter = newValue as ItemsLayouter;
},
defaultValueCreator: (bindable) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
return instance.InternalItemsLayouter;
});
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly new BindableProperty ScrollingDirectionProperty = BindableProperty.Create(nameof(ScrollingDirection), typeof(Tizen.NUI.Components.ScrollableBase.Direction), typeof(CollectionView), default(Direction), propertyChanged: (bindable, oldValue, newValue) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
if (newValue != null)
{
instance.InternalScrollingDirection = (Direction)newValue;
},
defaultValueCreator: (bindable) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
return instance.InternalScrollingDirection;
});
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty SelectionChangedCommandProperty = BindableProperty.Create(nameof(SelectionChangedCommand), typeof(ICommand), typeof(CollectionView), null, propertyChanged: (bindable, oldValue, newValue) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
if (newValue != null)
{
instance.InternalSelectionChangedCommand = newValue as ICommand;
},
defaultValueCreator: (bindable) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
return instance.InternalSelectionChangedCommand;
});
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty SelectionChangedCommandParameterProperty = BindableProperty.Create(nameof(SelectionChangedCommandParameter), typeof(object), typeof(CollectionView), null, propertyChanged: (bindable, oldValue, newValue) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
if (newValue != null)
{
instance.InternalSelectionChangedCommandParameter = newValue;
},
defaultValueCreator: (bindable) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
return instance.InternalSelectionChangedCommandParameter;
});
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty HeaderProperty = BindableProperty.Create(nameof(Header), typeof(RecyclerViewItem), typeof(CollectionView), null, propertyChanged: (bindable, oldValue, newValue) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
if (newValue != null)
{
instance.InternalHeader = newValue as RecyclerViewItem;
},
defaultValueCreator: (bindable) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
return instance.InternalHeader;
});
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty FooterProperty = BindableProperty.Create(nameof(Footer), typeof(RecyclerViewItem), typeof(CollectionView), null, propertyChanged: (bindable, oldValue, newValue) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
if (newValue != null)
{
instance.InternalFooter = newValue as RecyclerViewItem;
},
defaultValueCreator: (bindable) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
return instance.InternalFooter;
});
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty IsGroupedProperty = BindableProperty.Create(nameof(IsGrouped), typeof(bool), typeof(CollectionView), default(bool), propertyChanged: (bindable, oldValue, newValue) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
if (newValue != null)
{
instance.InternalIsGrouped = (bool)newValue;
},
defaultValueCreator: (bindable) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
return instance.InternalIsGrouped;
});
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty GroupHeaderTemplateProperty = BindableProperty.Create(nameof(GroupHeaderTemplate), typeof(DataTemplate), typeof(CollectionView), null, propertyChanged: (bindable, oldValue, newValue) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
if (newValue != null)
{
instance.InternalGroupHeaderTemplate = newValue as DataTemplate;
},
defaultValueCreator: (bindable) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
return instance.InternalGroupHeaderTemplate;
});
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty GroupFooterTemplateProperty = BindableProperty.Create(nameof(GroupFooterTemplate), typeof(DataTemplate), typeof(CollectionView), null, propertyChanged: (bindable, oldValue, newValue) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
if (newValue != null)
{
instance.InternalGroupFooterTemplate = newValue as DataTemplate;
},
defaultValueCreator: (bindable) =>
{
- var instance = (CollectionView)bindable;
+ var instance = bindable as CollectionView;
+ if (instance == null)
+ {
+ throw new Exception("Bindable object is not CollectionView.");
+ }
return instance.InternalGroupFooterTemplate;
});
}