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