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 protected RecycleAdapter mAdapter;
31 protected View mContainer;
32 protected RecycleLayoutManager mLayoutManager;
33 protected int mTotalItemCount = 15;
34 private List<PropertyNotification> notifications = new List<PropertyNotification>();
36 public RecyclerView() : base()
38 Initialize(new RecycleAdapter(), new RecycleLayoutManager());
42 /// Default constructor.
44 /// <param name="adapter">Recycle adapter of RecyclerView.</param>
45 /// <param name="layoutManager">Recycle layoutManager of RecyclerView.</param>
46 /// <since_tizen> 8 </since_tizen>
47 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
48 [EditorBrowsable(EditorBrowsableState.Never)]
49 public RecyclerView(RecycleAdapter adapter, RecycleLayoutManager layoutManager)
51 Initialize(adapter, layoutManager);
54 private void Initialize(RecycleAdapter adapter, RecycleLayoutManager layoutManager)
56 Scrolling += OnScrolling;
59 mAdapter.OnDataChanged += OnAdapterDataChanged;
61 mLayoutManager = layoutManager;
62 mLayoutManager.Container = ContentContainer;
63 mLayoutManager.ItemSize = mAdapter.CreateRecycleItem().Size;
64 mLayoutManager.DataCount = mAdapter.Data.Count;
69 private void OnItemSizeChanged(object source, PropertyNotification.NotifyEventArgs args)
71 mLayoutManager.Layout(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
74 public int TotalItemCount
78 return mTotalItemCount;
82 mTotalItemCount = value;
87 private void InitializeItems()
89 for(int i = Children.Count -1 ; i > -1 ; i--)
91 Children[i].Unparent();
92 notifications[i].Notified -= OnItemSizeChanged;
93 notifications.RemoveAt(i);
96 for (int i = 0; i < mTotalItemCount; i++)
98 RecycleItem item = mAdapter.CreateRecycleItem();
100 item.Name = "[" + i + "] recycle";
102 if (i < mAdapter.Data.Count)
104 mAdapter.BindData(item);
108 PropertyNotification noti = item.AddPropertyNotification("size", PropertyCondition.Step(0.1f));
109 noti.Notified += OnItemSizeChanged;
110 notifications.Add(noti);
113 mLayoutManager.Layout(0.0f);
115 if (ScrollingDirection == Direction.Horizontal)
117 ContentContainer.SizeWidth = mLayoutManager.CalculateLayoutOrientationSize();
121 ContentContainer.SizeHeight = mLayoutManager.CalculateLayoutOrientationSize();
126 public new Direction ScrollingDirection
130 return base.ScrollingDirection;
134 base.ScrollingDirection = value;
136 if (ScrollingDirection == Direction.Horizontal)
138 ContentContainer.SizeWidth = mLayoutManager.CalculateLayoutOrientationSize();
142 ContentContainer.SizeHeight = mLayoutManager.CalculateLayoutOrientationSize();
148 /// Recycler adpater.
150 /// <since_tizen> 8 </since_tizen>
151 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
152 [EditorBrowsable(EditorBrowsableState.Never)]
153 public RecycleAdapter Adapter
163 mAdapter.OnDataChanged -= OnAdapterDataChanged;
167 mAdapter.OnDataChanged += OnAdapterDataChanged;
168 mLayoutManager.ItemSize = mAdapter.CreateRecycleItem().Size;
169 mLayoutManager.DataCount = mAdapter.Data.Count;
175 /// Recycler layoutManager.
177 /// <since_tizen> 8 </since_tizen>
178 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
179 [EditorBrowsable(EditorBrowsableState.Never)]
180 public RecycleLayoutManager LayoutManager
184 return mLayoutManager;
188 mLayoutManager = value;
189 mLayoutManager.Container = ContentContainer;
190 mLayoutManager.ItemSize = mAdapter.CreateRecycleItem().Size;
191 mLayoutManager.DataCount = mAdapter.Data.Count;
196 private void OnScrolling(object source, ScrollEventArgs args)
198 mLayoutManager.Layout(ScrollingDirection == Direction.Horizontal ? args.Position.X : args.Position.Y);
199 List<RecycleItem> recycledItemList = mLayoutManager.Recycle(ScrollingDirection == Direction.Horizontal ? args.Position.X : args.Position.Y);
200 BindData(recycledItemList);
203 private void OnAdapterDataChanged(object source, EventArgs args)
205 List<RecycleItem> changedData = new List<RecycleItem>();
207 foreach (RecycleItem item in Children)
209 changedData.Add(item);
212 BindData(changedData);
215 private void BindData(List<RecycleItem> changedData)
217 foreach (RecycleItem item in changedData)
219 if (item.DataIndex > -1 && item.DataIndex < mAdapter.Data.Count)
222 item.Name = "["+item.DataIndex+"]";
223 mAdapter.BindData(item);
233 /// Adjust scrolling position by own scrolling rules.
234 /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
236 /// <param name="position">Scroll position which is calculated by ScrollableBase</param>
237 /// <returns>Adjusted scroll destination</returns>
238 /// <since_tizen> 8 </since_tizen>
239 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
240 [EditorBrowsable(EditorBrowsableState.Never)]
241 protected override float AdjustTargetPositionOfScrollAnimation(float position)
243 // Destination is depending on implementation of layout manager.
244 // Get destination from layout manager.
245 return mLayoutManager.CalculateCandidateScrollPosition(position);