[NUI] Introduce CollectionView and related classes. (#2525)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / CollectionViewDemo / Group / CollectionViewGridGroupSample.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 CollectionViewGridGroupSample : IExample
12     {
13         CollectionView colView;
14         int selectedCount;
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.MultipleSelections;
24             DefaultTitleItem myTitle = new DefaultTitleItem();
25             myTitle.Text = "Grid Sample Count["+ groupSource.Count+"] Selected["+selectedCount+"]";
26             //Set Width Specification as MatchParent to fit the Item width with parent View.
27             myTitle.WidthSpecification = LayoutParamPolicies.MatchParent;
28
29             colView = new CollectionView()
30             {
31                 ItemsSource = groupSource,
32                 ItemsLayouter = new GridLayouter(),
33                 ItemTemplate = new DataTemplate(() =>
34                 {
35                     DefaultGridItem item = new DefaultGridItem();
36                     item.WidthSpecification = 180;
37                     item.HeightSpecification = 240;
38                     //Decorate Label
39                     item.Caption.SetBinding(TextLabel.TextProperty, "ViewLabel");
40                     item.Caption.HorizontalAlignment = HorizontalAlignment.Center;
41                     //Decorate Image
42                     item.Image.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl");
43                     item.Image.WidthSpecification = 170;
44                     item.Image.HeightSpecification = 170;
45                     //Decorate Badge checkbox.
46                     //[NOTE] This is sample of CheckBox usage in CollectionView.
47                     // Checkbox change their selection by IsSelectedProperty bindings with
48                     // SelectionChanged event with MulitpleSelections ItemSelectionMode of CollectionView.
49                     item.Badge = new CheckBox();
50                     //FIXME : SetBinding in RadioButton crashed as Sensitive Property is disposed.
51                     //item.Badge.SetBinding(CheckBox.IsSelectedProperty, "Selected");
52                     item.Badge.WidthSpecification = 30;
53                     item.Badge.HeightSpecification = 30;
54
55                     return item;
56                 }),
57                 GroupHeaderTemplate = new DataTemplate(() =>
58                 {
59                     DefaultTitleItem group = new DefaultTitleItem();
60                     //Set Width Specification as MatchParent to fit the Item width with parent View.
61                     group.WidthSpecification = LayoutParamPolicies.MatchParent;
62
63                     group.Label.SetBinding(TextLabel.TextProperty, "Date");
64                     group.Label.HorizontalAlignment = HorizontalAlignment.Begin;
65
66                     return group;
67                 }),
68                 Header = myTitle,
69                 IsGrouped = true,
70                 ScrollingDirection = ScrollableBase.Direction.Vertical,
71                 WidthSpecification = LayoutParamPolicies.MatchParent,
72                 HeightSpecification = LayoutParamPolicies.MatchParent,
73                                 SelectionMode = selMode
74             };
75             colView.SelectionChanged += SelectionEvt;
76
77             window.Add(colView);
78         }
79
80         public void SelectionEvt(object sender, SelectionChangedEventArgs ev)
81         {
82             List<object> oldSel = new List<object>(ev.PreviousSelection);
83             List<object> newSel = new List<object>(ev.CurrentSelection);
84
85             foreach (object item in oldSel)
86             {
87                 if (item != null && item is Gallery)
88                 {
89                     Gallery galItem = (Gallery)item;
90                     if (!(newSel.Contains(item)))
91                     {
92                         galItem.Selected = false;
93                         Tizen.Log.Debug("Unselected: {0}", galItem.ViewLabel);
94                         selectedCount--;
95                     }
96                 }
97                 else continue;
98             }
99             foreach (object item in newSel)
100             {
101                 if (item != null && item is Gallery)
102                 {
103                     Gallery galItem = (Gallery)item;
104                     if (!(oldSel.Contains(item)))
105                     {
106                         galItem.Selected = true;
107                         Tizen.Log.Debug("Selected: {0}", galItem.ViewLabel);
108                         selectedCount++;
109                     }
110                 }
111                 else continue;
112             }
113             if (colView.Header != null && colView.Header is DefaultTitleItem)
114             {
115                 DefaultTitleItem title = (DefaultTitleItem)colView.Header;
116                 title.Text = "Grid Sample Count["+ groupSource.Count + "] Selected["+selectedCount+"]";
117             }
118         }
119
120         public void Deactivate()
121         {
122             if (colView != null)
123             {
124                 colView.Dispose();
125             }
126         }
127     }
128 }