[NUI] Introduce CollectionView and related classes. (#2525)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / CollectionViewDemo / Group / CollectionViewLinearGroupSample.cs
1 using System;
2 using System.ComponentModel;
3 using System.Collections.Generic;
4 using System.Collections.ObjectModel;
5 using Tizen.NUI.BaseComponents;
6 using Tizen.NUI.Components;
7 using Tizen.NUI.Binding;
8
9 namespace Tizen.NUI.Samples
10 {
11     public class CollectionViewLinearGroupSample : IExample
12     {
13         CollectionView colView;
14         string selectedItem;
15         ItemSelectionMode selMode;
16         ObservableCollection<Album> groupSource;
17
18         public void Activate()
19         {
20             Window window = NUIApplication.GetDefaultWindow();
21
22             groupSource = new AlbumViewModel();
23             selMode = ItemSelectionMode.SingleSelection;
24             DefaultTitleItem myTitle = new DefaultTitleItem();
25             //To Bind the Count property changes, need to create custom property for count.
26             myTitle.Text = "Linear Sample Group["+ groupSource.Count+"]";
27             //Set Width Specification as MatchParent to fit the Item width with parent View.
28             myTitle.WidthSpecification = LayoutParamPolicies.MatchParent;
29
30             colView = new CollectionView()
31             {
32                 ItemsSource = groupSource,
33                 ItemsLayouter = new LinearLayouter(),
34                 ItemTemplate = new DataTemplate(() =>
35                 {
36                     var rand = new Random();
37                     RecyclerViewItem item = new RecyclerViewItem();
38                     item.WidthSpecification = LayoutParamPolicies.MatchParent;
39                     item.HeightSpecification = 100;
40                     item.BackgroundColor = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble(), 1);
41                     /*
42                     DefaultLinearItem item = new DefaultLinearItem();
43                     //Set Width Specification as MatchParent to fit the Item width with parent View.
44                     item.WidthSpecification = LayoutParamPolicies.MatchParent;
45                     //Decorate Label
46                     item.Label.SetBinding(TextLabel.TextProperty, "ViewLabel");
47                     item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
48                     //Decorate Icon
49                     item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl");
50                     item.Icon.WidthSpecification = 80;
51                     item.Icon.HeightSpecification = 80;
52                     //Decorate Extra RadioButton.
53                     //[NOTE] This is sample of RadioButton usage in CollectionView.
54                     // RadioButton change their selection by IsSelectedProperty bindings with
55                     // SelectionChanged event with SingleSelection ItemSelectionMode of CollectionView.
56                     // be aware of there are no RadioButtonGroup. 
57                     item.Extra = new RadioButton();
58                     //FIXME : SetBinding in RadioButton crashed as Sensitive Property is disposed.
59                     //item.Extra.SetBinding(RadioButton.IsSelectedProperty, "Selected");
60                     item.Extra.WidthSpecification = 80;
61                     item.Extra.HeightSpecification = 80;
62                     */
63                     return item;
64                 }),
65                 GroupHeaderTemplate = new DataTemplate(() =>
66                 {
67                     var rand = new Random();
68                     RecyclerViewItem item = new RecyclerViewItem();
69                     item.WidthSpecification = LayoutParamPolicies.MatchParent;
70                     item.HeightSpecification = 50;
71                     item.BackgroundColor = new Color(0, 0, 0, 1);
72                     /*
73                     DefaultTitleItem group = new DefaultTitleItem();
74                     //Set Width Specification as MatchParent to fit the Item width with parent View.
75                     group.WidthSpecification = LayoutParamPolicies.MatchParent;
76
77                     group.Label.SetBinding(TextLabel.TextProperty, "Date");
78                     group.Label.HorizontalAlignment = HorizontalAlignment.Begin;
79                     */
80                     return item;
81                 }),
82                 Header = myTitle,
83                 IsGrouped = true,
84                 ScrollingDirection = ScrollableBase.Direction.Vertical,
85                 WidthSpecification = LayoutParamPolicies.MatchParent,
86                 HeightSpecification = LayoutParamPolicies.MatchParent,
87                                 SelectionMode = selMode
88             };
89             colView.SelectionChanged += SelectionEvt;
90
91             window.Add(colView);
92
93         }
94
95         public void SelectionEvt(object sender, SelectionChangedEventArgs ev)
96         {
97             //Tizen.Log.Debug("NUI", "LSH :: SelectionEvt called");
98
99             //SingleSelection Only have 1 or nil object in the list.
100             foreach (object item in ev.PreviousSelection)
101             {
102                 if (item == null) break;
103                 Gallery unselItem = (Gallery)item;
104
105                 unselItem.Selected = false;
106                 selectedItem = null;
107                 //Tizen.Log.Debug("NUI", "LSH :: Unselected: {0}", unselItem.ViewLabel);
108             }
109             foreach (object item in ev.CurrentSelection)
110             {
111                 if (item == null) break;
112                 Gallery selItem = (Gallery)item;
113                 selItem.Selected = true;
114                 selectedItem = selItem.Name;
115                 //Tizen.Log.Debug("NUI", "LSH :: Selected: {0}", selItem.ViewLabel);
116             }
117             if (colView.Header != null && colView.Header is DefaultTitleItem)
118             {
119                 DefaultTitleItem title = (DefaultTitleItem)colView.Header;
120                 title.Text = "Linear Sample Count[" + groupSource + (selectedItem != null ? "] Selected [" + selectedItem + "]" : "]");
121             }
122         }
123         public void Deactivate()
124         {
125             if (colView != null)
126             {
127                 colView.Dispose();
128             }
129         }
130     }
131 }