1 /* Copyright (c) 2020 Samsung Electronics Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 using Tizen.NUI.BaseComponents;
18 using System.Collections.Generic;
19 using System.ComponentModel;
21 namespace Tizen.NUI.Components
24 /// [Draft] This class provides a View that can recycle items to improve performance.
26 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 public class RecyclerView : ScrollableBase
30 private RecycleAdapter adapter;
31 private RecycleLayoutManager layoutManager;
32 private int totalItemCount = 15;
33 private List<PropertyNotification> notifications = new List<PropertyNotification>();
35 public RecyclerView() : base()
37 Initialize(new RecycleAdapter(), new RecycleLayoutManager());
41 /// Default constructor.
43 /// <param name="adapter">Recycle adapter of RecyclerView.</param>
44 /// <param name="layoutManager">Recycle layoutManager of RecyclerView.</param>
45 /// <since_tizen> 8 </since_tizen>
46 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
47 [EditorBrowsable(EditorBrowsableState.Never)]
48 public RecyclerView(RecycleAdapter adapter, RecycleLayoutManager layoutManager)
50 Initialize(adapter, layoutManager);
53 private void Initialize(RecycleAdapter adapter, RecycleLayoutManager layoutManager)
55 Scrolling += OnScrolling;
57 this.adapter = adapter;
58 this.adapter.OnDataChanged += OnAdapterDataChanged;
60 this.layoutManager = layoutManager;
61 this.layoutManager.Container = ContentContainer;
62 this.layoutManager.ItemSize = this.adapter.CreateRecycleItem().Size;
63 this.layoutManager.DataCount = this.adapter.Data.Count;
68 private void OnItemSizeChanged(object source, PropertyNotification.NotifyEventArgs args)
70 layoutManager.Layout(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
73 public int TotalItemCount
77 return totalItemCount;
81 totalItemCount = value;
86 private void InitializeItems()
88 for(int i = Children.Count -1 ; i > -1 ; i--)
90 Children[i].Unparent();
91 notifications[i].Notified -= OnItemSizeChanged;
92 notifications.RemoveAt(i);
95 for (int i = 0; i < totalItemCount; i++)
97 RecycleItem item = adapter.CreateRecycleItem();
99 item.Name = "[" + i + "] recycle";
101 if (i < adapter.Data.Count)
103 adapter.BindData(item);
107 PropertyNotification noti = item.AddPropertyNotification("size", PropertyCondition.Step(0.1f));
108 noti.Notified += OnItemSizeChanged;
109 notifications.Add(noti);
112 layoutManager.Layout(0.0f);
114 if (ScrollingDirection == Direction.Horizontal)
116 ContentContainer.SizeWidth = layoutManager.CalculateLayoutOrientationSize();
120 ContentContainer.SizeHeight = layoutManager.CalculateLayoutOrientationSize();
125 public new Direction ScrollingDirection
129 return base.ScrollingDirection;
133 base.ScrollingDirection = value;
135 if (ScrollingDirection == Direction.Horizontal)
137 ContentContainer.SizeWidth = layoutManager.CalculateLayoutOrientationSize();
141 ContentContainer.SizeHeight = layoutManager.CalculateLayoutOrientationSize();
147 /// Recycler adpater.
149 /// <since_tizen> 8 </since_tizen>
150 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
151 [EditorBrowsable(EditorBrowsableState.Never)]
152 public RecycleAdapter Adapter
162 adapter.OnDataChanged -= OnAdapterDataChanged;
166 adapter.OnDataChanged += OnAdapterDataChanged;
167 layoutManager.ItemSize = adapter.CreateRecycleItem().Size;
168 layoutManager.DataCount = adapter.Data.Count;
174 /// Recycler layoutManager.
176 /// <since_tizen> 8 </since_tizen>
177 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
178 [EditorBrowsable(EditorBrowsableState.Never)]
179 public RecycleLayoutManager LayoutManager
183 return layoutManager;
187 LayoutManager = value;
188 LayoutManager.Container = ContentContainer;
189 LayoutManager.ItemSize = Adapter.CreateRecycleItem().Size;
190 LayoutManager.DataCount = Adapter.Data.Count;
195 private void OnScrolling(object source, ScrollEventArgs args)
197 layoutManager.Layout(ScrollingDirection == Direction.Horizontal ? args.Position.X : args.Position.Y);
198 List<RecycleItem> recycledItemList = layoutManager.Recycle(ScrollingDirection == Direction.Horizontal ? args.Position.X : args.Position.Y);
199 BindData(recycledItemList);
202 private void OnAdapterDataChanged(object source, EventArgs args)
204 List<RecycleItem> changedData = new List<RecycleItem>();
206 foreach (RecycleItem item in Children)
208 changedData.Add(item);
211 BindData(changedData);
214 private void BindData(List<RecycleItem> changedData)
216 foreach (RecycleItem item in changedData)
218 if (item.DataIndex > -1 && item.DataIndex < adapter.Data.Count)
221 item.Name = "["+item.DataIndex+"]";
222 adapter.BindData(item);
232 /// Adjust scrolling position by own scrolling rules.
233 /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
235 /// <param name="position">Scroll position which is calculated by ScrollableBase</param>
236 /// <returns>Adjusted scroll destination</returns>
237 /// <since_tizen> 8 </since_tizen>
238 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
239 [EditorBrowsable(EditorBrowsableState.Never)]
240 protected override float AdjustTargetPositionOfScrollAnimation(float position)
242 // Destination is depending on implementation of layout manager.
243 // Get destination from layout manager.
244 return layoutManager.CalculateCandidateScrollPosition(position);