2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 using System.ComponentModel;
19 namespace Tizen.NUI.Components
22 /// Layout collection of views in a grid.
24 /// <since_tizen> 6 </since_tizen>
25 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
26 [EditorBrowsable(EditorBrowsableState.Never)]
27 public class GridLayoutManager : LinearLayoutManager
29 private static readonly int DEFAULT_SPAN_COUNT = -1;
31 private int mSpanCount = DEFAULT_SPAN_COUNT;
34 /// Creates a GridLayoutManager with orientation.
36 /// <param name="spanCount">The number of columns or rows in the grid</param>
37 /// <param name="orientation">Layout orientation.Should be HORIZONTAL or VERTICAL</param>
38 /// <since_tizen> 6 </since_tizen>
39 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
40 [EditorBrowsable(EditorBrowsableState.Never)]
41 public GridLayoutManager(int spanCount, int orientation) : base(orientation)
43 mSpanCount = spanCount;
46 internal override void EnsureAnchorReady(FlexibleView.Recycler recycler, AnchorInfo anchorInfo, int itemDirection)
48 bool layingOutInPrimaryDirection = (itemDirection == LayoutState.ITEM_DIRECTION_TAIL);
49 int span = anchorInfo.Position;
50 if (layingOutInPrimaryDirection)
53 while (span > 0 && anchorInfo.Position > 0)
55 anchorInfo.Position--;
56 span = anchorInfo.Position;
61 // choose the max span we can get. hopefully last one
62 int indexLimit = ChildCount - 1;
63 int pos = anchorInfo.Position;
65 while (pos < indexLimit)
78 anchorInfo.Position = pos;
83 /// Retrieves a position that neighbor to current position by direction.
85 /// <param name="position">The anchor adapter position</param>
86 /// <param name="direction">The direction.</param>
87 /// <since_tizen> 6 </since_tizen>
88 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
89 [EditorBrowsable(EditorBrowsableState.Never)]
90 protected override int GetNextPosition(int position, FlexibleView.LayoutManager.Direction direction)
92 if (mOrientation == HORIZONTAL)
96 case FlexibleView.LayoutManager.Direction.Left:
97 if (position >= mSpanCount)
99 return position - mSpanCount;
102 case FlexibleView.LayoutManager.Direction.Right:
103 if (position < ItemCount - mSpanCount)
105 return position + mSpanCount;
108 case FlexibleView.LayoutManager.Direction.Up:
109 if (position % mSpanCount > 0)
114 case FlexibleView.LayoutManager.Direction.Down:
115 if (position < ItemCount - 1 && (position % mSpanCount < mSpanCount - 1))
126 case FlexibleView.LayoutManager.Direction.Left:
127 if (position % mSpanCount > 0)
132 case FlexibleView.LayoutManager.Direction.Right:
133 if (position < ItemCount - 1 && (position % mSpanCount < mSpanCount - 1))
138 case FlexibleView.LayoutManager.Direction.Up:
139 if (position >= mSpanCount)
141 return position - mSpanCount;
144 case FlexibleView.LayoutManager.Direction.Down:
145 if (position < ItemCount - mSpanCount)
147 return position + mSpanCount;
156 internal override void LayoutChunk(FlexibleView.Recycler recycler,
157 LayoutState layoutState, LayoutChunkResult result)
159 bool layingOutInPrimaryDirection =
160 layoutState.ItemDirection == LayoutState.ITEM_DIRECTION_TAIL;
162 int count = mSpanCount;
163 for (int i = 0; i < count; i++)
165 FlexibleView.ViewHolder holder = layoutState.Next(recycler);
168 result.Finished = true;
172 if (layingOutInPrimaryDirection)
177 result.Consumed = mOrientationHelper.GetViewHolderMeasurement(holder);
179 float left, top, width, height;
180 if (mOrientation == VERTICAL)
182 width = (Width - PaddingLeft - PaddingRight) / count;
183 height = result.Consumed;
184 if (layoutState.LayoutDirection == LayoutState.LAYOUT_END)
186 left = PaddingLeft + width * i;
187 top = layoutState.Offset;
191 left = PaddingLeft + width * (count - 1 - i);
192 top = layoutState.Offset - height;
194 LayoutChild(holder, left, top, width, height);
198 width = result.Consumed;
199 height = (Height - PaddingTop - PaddingBottom) / count;
200 if (layoutState.LayoutDirection == LayoutState.LAYOUT_END)
202 top = PaddingTop + height * i;
203 left = layoutState.Offset;
207 top = PaddingTop + height * (count - 1 - i);
208 left = layoutState.Offset - width;
210 LayoutChild(holder, left, top, width, height);