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.
17 using System.ComponentModel;
18 using Tizen.NUI.BaseComponents;
19 using Tizen.NUI.Binding;
20 using Tizen.NUI.Components.Extension;
21 using Tizen.NUI.Accessibility;
23 namespace Tizen.NUI.Components
26 /// This class provides a basic item for CollectionView.
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public partial class RecyclerViewItem : Control
32 /// Property of boolean Enable flag.
34 [EditorBrowsable(EditorBrowsableState.Never)]
35 public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(RecyclerViewItem), true, propertyChanged: (bindable, oldValue, newValue) =>
37 var instance = (RecyclerViewItem)bindable;
40 bool newEnabled = (bool)newValue;
41 if (instance.isEnabled != newEnabled)
43 instance.isEnabled = newEnabled;
44 if (instance.ItemStyle != null)
46 instance.ItemStyle.IsEnabled = newEnabled;
48 instance.UpdateState();
52 defaultValueCreator: (bindable) => ((RecyclerViewItem)bindable).isEnabled);
55 /// Property of boolean Selected flag.
57 [EditorBrowsable(EditorBrowsableState.Never)]
58 public static readonly BindableProperty IsSelectedProperty = BindableProperty.Create(nameof(IsSelected), typeof(bool), typeof(RecyclerViewItem), true, propertyChanged: (bindable, oldValue, newValue) =>
60 var instance = (RecyclerViewItem)bindable;
63 bool newSelected = (bool)newValue;
64 if (instance.isSelected != newSelected)
66 instance.isSelected = newSelected;
68 if (instance.ItemStyle != null)
70 instance.ItemStyle.IsSelected = newSelected;
73 if (instance.isSelectable)
75 instance.UpdateState();
80 defaultValueCreator: (bindable) =>
82 var instance = (RecyclerViewItem)bindable;
83 return instance.isSelectable && instance.isSelected;
87 /// Property of boolean Selectable flag.
89 [EditorBrowsable(EditorBrowsableState.Never)]
90 public static readonly BindableProperty IsSelectableProperty = BindableProperty.Create(nameof(IsSelectable), typeof(bool), typeof(RecyclerViewItem), true, propertyChanged: (bindable, oldValue, newValue) =>
92 var instance = (RecyclerViewItem)bindable;
95 bool newSelectable = (bool)newValue;
96 if (instance.isSelectable != newSelectable)
98 instance.isSelectable = newSelectable;
100 if (instance.ItemStyle != null)
102 instance.ItemStyle.IsSelectable = newSelectable;
105 instance.UpdateState();
109 defaultValueCreator: (bindable) => ((RecyclerViewItem)bindable).isSelectable);
111 private bool isSelected = false;
112 private bool isSelectable = true;
113 private bool isEnabled = true;
114 private RecyclerViewItemStyle ItemStyle => ViewStyle as RecyclerViewItemStyle;
117 /// Return a copied Style instance of Toast
120 /// It returns copied Style instance and changing it does not effect to the Toast.
121 /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
123 [EditorBrowsable(EditorBrowsableState.Never)]
124 public new RecyclerViewItemStyle Style
128 var result = new RecyclerViewItemStyle(ItemStyle);
129 result.CopyPropertiesFromView(this);
134 static RecyclerViewItem() { }
137 /// Creates a new instance of RecyclerViewItem.
139 [EditorBrowsable(EditorBrowsableState.Never)]
140 public RecyclerViewItem() : base()
146 /// Creates a new instance of RecyclerViewItem with style.
148 /// <param name="style">Create RecyclerViewItem by special style defined in UX.</param>
149 [EditorBrowsable(EditorBrowsableState.Never)]
150 public RecyclerViewItem(string style) : base(style)
156 /// Creates a new instance of a RecyclerViewItem with style.
158 /// <param name="itemStyle">Create RecyclerViewItem by style customized by user.</param>
159 [EditorBrowsable(EditorBrowsableState.Never)]
160 public RecyclerViewItem(RecyclerViewItemStyle itemStyle) : base(itemStyle)
166 /// An event for the RecyclerViewItem clicked signal which can be used to subscribe or unsubscribe the event handler provided by the user.
168 [EditorBrowsable(EditorBrowsableState.Never)]
169 public event EventHandler<ClickedEventArgs> Clicked;
172 /// Flag to decide RecyclerViewItem can be selected or not.
174 [EditorBrowsable(EditorBrowsableState.Never)]
175 public bool IsSelectable
177 get => (bool)GetValue(IsSelectableProperty);
178 set => SetValue(IsSelectableProperty, value);
182 /// Flag to decide selected state in RecyclerViewItem.
184 [EditorBrowsable(EditorBrowsableState.Never)]
185 public bool IsSelected
187 get => (bool)GetValue(IsSelectedProperty);
188 set => SetValue(IsSelectedProperty, value);
192 /// Flag to decide enable or disable in RecyclerViewItem.
194 [EditorBrowsable(EditorBrowsableState.Never)]
195 public bool IsEnabled
197 get => (bool)GetValue(IsEnabledProperty);
198 set => SetValue(IsEnabledProperty, value);
202 /// Data index which is binded to item.
203 /// Can access to data using this index.
205 [EditorBrowsable(EditorBrowsableState.Never)]
206 public int Index { get; internal set; } = 0;
209 /// DataTemplate of this view object
211 [EditorBrowsable(EditorBrowsableState.Never)]
212 public DataTemplate Template { get; internal set; }
215 /// State of Realization
217 [EditorBrowsable(EditorBrowsableState.Never)]
218 public bool IsRealized { get; internal set; }
219 internal bool IsHeader { get; set; }
220 internal bool IsFooter { get; set; }
221 internal bool IsPressed { get; set; } = false;
224 /// Called after a key event is received by the view that has had its focus set.
226 /// <param name="key">The key event.</param>
227 /// <returns>True if the key event should be consumed.</returns>
228 [EditorBrowsable(EditorBrowsableState.Never)]
229 public override bool OnKey(Key key)
231 if (!IsEnabled || null == key)
236 if (key.State == Key.StateType.Down)
238 if (key.KeyPressedName == "Return")
244 else if (key.State == Key.StateType.Up)
246 if (key.KeyPressedName == "Return")
248 bool clicked = IsPressed && IsEnabled;
254 // Extension : Extension?.SetTouchInfo(touch);
255 if (ParentItemsView as CollectionView)
257 CollectionView colView = ParentItemsView as CollectionView;
258 switch (colView.SelectionMode)
260 case ItemSelectionMode.SingleSelection:
261 colView.SelectedItem = IsSelected ? null : BindingContext;
263 case ItemSelectionMode.MultipleSelections:
264 var selectedItems = colView.SelectedItems;
265 if (selectedItems.Contains(BindingContext)) selectedItems.Remove(BindingContext);
266 else selectedItems.Add(BindingContext);
268 case ItemSelectionMode.None:
280 ClickedEventArgs eventArgs = new ClickedEventArgs();
281 OnClickedInternal(eventArgs);
285 return base.OnKey(key);
289 /// Called when the control gain key input focus. Should be overridden by derived classes if they need to customize what happens when the focus is gained.
291 [EditorBrowsable(EditorBrowsableState.Never)]
292 public override void OnFocusGained()
294 base.OnFocusGained();
299 /// Called when the control loses key input focus.
300 /// Should be overridden by derived classes if they need to customize
301 /// what happens when the focus is lost.
303 [EditorBrowsable(EditorBrowsableState.Never)]
304 public override void OnFocusLost()
311 /// Apply style to RecyclerViewItem.
313 /// <param name="viewStyle">The style to apply.</param>
314 [EditorBrowsable(EditorBrowsableState.Never)]
315 public override void ApplyStyle(ViewStyle viewStyle)
317 styleApplied = false;
319 base.ApplyStyle(viewStyle);
320 if (viewStyle != null)
322 //Extension = RecyclerViewItemStyle.CreateExtension();