[NUI] Introduce CollectionView and related classes. (#2525)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / CollectionViewDemo / CollectionViewGridSample.cs
1 using System.Collections.Generic;
2 using Tizen.NUI.BaseComponents;
3 using Tizen.NUI.Components;
4 using Tizen.NUI.Binding;
5
6 namespace Tizen.NUI.Samples
7 {
8     public class CollectionViewGridSample : IExample
9     {
10         CollectionView colView;
11         int itemCount = 500;
12         int selectedCount;
13         ItemSelectionMode selMode;
14
15         public void Activate()
16         {
17             Window window = NUIApplication.GetDefaultWindow();
18
19             var myViewModelSource = new GalleryViewModel(itemCount);
20             selMode = ItemSelectionMode.MultipleSelections;
21             DefaultTitleItem myTitle = new DefaultTitleItem();
22             myTitle.Text = "Grid Sample Count["+itemCount+"] Selected["+selectedCount+"]";
23             //Set Width Specification as MatchParent to fit the Item width with parent View.
24             myTitle.WidthSpecification = LayoutParamPolicies.MatchParent;
25
26             colView = new CollectionView()
27             {
28                 ItemsSource = myViewModelSource,
29                 ItemsLayouter = new GridLayouter(),
30                 ItemTemplate = new DataTemplate(() =>
31                 {
32                     DefaultGridItem item = new DefaultGridItem();
33                     item.WidthSpecification = 180;
34                     item.HeightSpecification = 240;
35                     //Decorate Label
36                     item.Caption.SetBinding(TextLabel.TextProperty, "ViewLabel");
37                     item.Caption.HorizontalAlignment = HorizontalAlignment.Center;
38                     //Decorate Image
39                     item.Image.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl");
40                     item.Image.WidthSpecification = 170;
41                     item.Image.HeightSpecification = 170;
42                     //Decorate Badge checkbox.
43                     //[NOTE] This is sample of CheckBox usage in CollectionView.
44                     // Checkbox change their selection by IsSelectedProperty bindings with
45                     // SelectionChanged event with MulitpleSelections ItemSelectionMode of CollectionView.
46                     item.Badge = new CheckBox();
47                     //FIXME : SetBinding in RadioButton crashed as Sensitive Property is disposed.
48                     //item.Badge.SetBinding(CheckBox.IsSelectedProperty, "Selected");
49                     item.Badge.WidthSpecification = 30;
50                     item.Badge.HeightSpecification = 30;
51
52                     return item;
53                 }),
54                 Header = myTitle,
55                 ScrollingDirection = ScrollableBase.Direction.Vertical,
56                 WidthSpecification = LayoutParamPolicies.MatchParent,
57                 HeightSpecification = LayoutParamPolicies.MatchParent,
58                                 SelectionMode = selMode
59             };
60             colView.SelectionChanged += SelectionEvt;
61
62             window.Add(colView);
63         }
64
65         public void SelectionEvt(object sender, SelectionChangedEventArgs ev)
66         {
67             List<object> oldSel = new List<object>(ev.PreviousSelection);
68             List<object> newSel = new List<object>(ev.CurrentSelection);
69
70             foreach (object item in oldSel)
71             {
72                 if (item != null && item is Gallery)
73                 {
74                     Gallery galItem = (Gallery)item;
75                     if (!(newSel.Contains(item)))
76                     {
77                         galItem.Selected = false;
78                         Tizen.Log.Debug("Unselected: {0}", galItem.ViewLabel);
79                         selectedCount--;
80                     }
81                 }
82                 else continue;
83             }
84             foreach (object item in newSel)
85             {
86                 if (item != null && item is Gallery)
87                 {
88                     Gallery galItem = (Gallery)item;
89                     if (!(oldSel.Contains(item)))
90                     {
91                         galItem.Selected = true;
92                         Tizen.Log.Debug("Selected: {0}", galItem.ViewLabel);
93                         selectedCount++;
94                     }
95                 }
96                 else continue;
97             }
98             if (colView.Header != null && colView.Header is DefaultTitleItem)
99             {
100                 DefaultTitleItem title = (DefaultTitleItem)colView.Header;
101                 title.Text = "Grid Sample Count["+itemCount+"] Selected["+selectedCount+"]";
102             }
103         }
104
105         public void Deactivate()
106         {
107             if (colView != null)
108             {
109                 colView.Dispose();
110             }
111         }
112     }
113 }