d81a3cc6e5b47aa338064786921bd6729dfe5653
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / Examples / CollectionViewTest / CollectionViewTest8.xaml.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using System;
18 using System.Collections.ObjectModel;
19 using System.ComponentModel;
20 using Tizen.NUI;
21 using Tizen.NUI.Binding;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Components;
24 using Tizen.NUI.Components.Extension;
25
26 namespace NUITizenGallery
27 {
28     public partial class CollectionViewTest8 : ContentPage
29     {
30         public CollectionViewTest8()
31         {
32             InitializeComponent();
33             BindingContext = new TestSourceModel(40);
34
35             var customStyle = new RecyclerViewItemStyle()
36             {
37                 BackgroundColor = new Selector<Color>()
38                 {
39                     Normal = Color.White,
40                     Pressed = Color.Red,
41                     Selected = Color.Blue,
42                     Disabled = Color.Grey,
43                 },
44             };
45
46             ColView.ItemTemplate = new DataTemplate(() =>
47             {
48                 var item = new RecyclerViewItem(customStyle)
49                 {
50                     WidthSpecification = 100,
51                     HeightSpecification = LayoutParamPolicies.MatchParent,
52                 };
53                 var label = new TextLabel()
54                 {
55                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
56                     PivotPoint = Tizen.NUI.PivotPoint.Center,
57                     PositionUsesPivotPoint = true,
58                 };
59                 label.PixelSize = 30;
60                 label.SetBinding(TextLabel.TextProperty, "Index");
61                 item.Add(label);
62
63                 return item;
64             });
65         }
66
67         protected override void Dispose(DisposeTypes type)
68         {
69             if (Disposed)
70             {
71                 return;
72             }
73
74             if (type == DisposeTypes.Explicit)
75             {
76                 RemoveAllChildren(true);
77             }
78
79             base.Dispose(type);
80         }
81
82         private void RemoveAllChildren(bool dispose = false)
83         {
84             RecursiveRemoveChildren(this, dispose);
85         }
86
87         private void RecursiveRemoveChildren(View parent, bool dispose)
88         {
89             if (parent == null)
90             {
91                 return;
92             }
93
94             int maxChild = (int)parent.ChildCount;
95             for (int i = maxChild - 1; i >= 0; --i)
96             {
97                 View child = parent.GetChildAt((uint)i);
98                 if (child == null)
99                 {
100                     continue;
101                 }
102
103                 RecursiveRemoveChildren(child, dispose);
104                 parent.Remove(child);
105                 if (dispose)
106                 {
107                     child.Dispose();
108                 }
109             }
110         }
111     }
112 }