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.
19 namespace Tizen.NUI.Components
21 // Helper class for LayoutManagers to abstract measurements depending on the View's orientation.
22 // It is developed to easily support vertical and horizontal orientations in a LayoutManager but
23 // can also be used to abstract calls around view bounds and child measurements with margins and
26 // @see #createHorizontalHelper(FlexibleViewRecyclerView.LayoutManager)
27 // @see #createVerticalHelper(FlexibleViewRecyclerView.LayoutManager)
28 internal abstract class OrientationHelper
30 public const int HORIZONTAL = 0;
31 public const int VERTICAL = 1;
33 private const int INVALID_SIZE = -1;
35 protected FlexibleViewLayoutManager mLayoutManager;
37 private float mLastTotalSpace = INVALID_SIZE;
39 public OrientationHelper(FlexibleViewLayoutManager layoutManager)
41 mLayoutManager = layoutManager;
44 // Call this method after onLayout method is complete if state is NOT pre-layout.
45 // This method records information like layout bounds that might be useful in the next layout
47 public void OnLayoutComplete()
49 mLastTotalSpace = GetTotalSpace();
52 // Returns the layout space change between the previous layout pass and current layout pass.
53 // Make sure you call {@link #onLayoutComplete()} at the end of your LayoutManager's
54 // {@link FlexibleViewRecyclerView.LayoutManager#onLayoutChildren(FlexibleViewRecyclerView.Recycler,
55 // FlexibleViewRecyclerView.State)} method.
57 // @return The difference between the current total space and previous layout's total space.
58 // @see #onLayoutComplete()
59 public float GetTotalSpaceChange()
61 return INVALID_SIZE == mLastTotalSpace ? 0 : GetTotalSpace() - mLastTotalSpace;
64 // Returns the start of the view including its decoration and margin.
65 // For example, for the horizontal helper, if a View's left is at pixel 20, has 2px left
66 // decoration and 3px left margin, returned value will be 15px.
68 // @param view The view element to check
69 // @return The first pixel of the element
70 // @see #getDecoratedEnd(android.view.View)
71 public abstract float GetViewHolderStart(FlexibleViewViewHolder holder);
73 // Returns the end of the view including its decoration and margin.
74 // For example, for the horizontal helper, if a View's right is at pixel 200, has 2px right
75 // decoration and 3px right margin, returned value will be 205.
77 // @param view The view element to check
78 // @return The last pixel of the element
79 // @see #getDecoratedStart(android.view.View)
80 public abstract float GetViewHolderEnd(FlexibleViewViewHolder holder);
82 // Returns the space occupied by this View in the current orientation including decorations and
85 // @param view The view element to check
86 // @return Total space occupied by this view
87 // @see #getDecoratedMeasurementInOther(View)
89 public abstract float GetViewHolderMeasurement(FlexibleViewViewHolder holder);
91 // Returns the space occupied by this View in the perpendicular orientation including
92 // decorations and margins.
94 // @param view The view element to check
95 // @return Total space occupied by this view in the perpendicular orientation to current one
96 // @see #getDecoratedMeasurement(View)
97 public abstract float GetViewHolderMeasurementInOther(FlexibleViewViewHolder holder);
99 // Returns the start position of the layout after the start padding is added.
101 // @return The very first pixel we can draw.
102 public abstract float GetStartAfterPadding();
104 // Returns the end position of the layout after the end padding is removed.
106 // @return The end boundary for this layout.
107 public abstract float GetEndAfterPadding();
109 // Returns the end position of the layout without taking padding into account.
111 // @return The end boundary for this layout without considering padding.
112 public abstract float GetEnd();
114 // Offsets all children's positions by the given amount.
116 // @param amount Value to add to each child's layout parameters
117 public abstract void OffsetChildren(float amount, bool immediate);
119 // Returns the total space to layout. This number is the difference between
120 // {@link #getEndAfterPadding()} and {@link #getStartAfterPadding()}.
122 // @return Total space to layout children
123 public abstract float GetTotalSpace();
125 // Offsets the child in this orientation.
127 // @param view View to offset
128 // @param offset offset amount
129 internal abstract void OffsetChild(FlexibleViewViewHolder holder, int offset);
131 // Returns the padding at the end of the layout. For horizontal helper, this is the right
132 // padding and for vertical helper, this is the bottom padding. This method does not check
133 // whether the layout is RTL or not.
135 // @return The padding at the end of the layout.
136 public abstract float GetEndPadding();
138 // Creates an OrientationHelper for the given LayoutManager and orientation.
140 // @param layoutManager LayoutManager to attach to
141 // @param orientation Desired orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}
142 // @return A new OrientationHelper
143 public static OrientationHelper CreateOrientationHelper(
144 FlexibleViewLayoutManager layoutManager, int orientation)
146 if (orientation == HORIZONTAL)
148 return CreateHorizontalHelper(layoutManager);
150 else if (orientation == VERTICAL)
152 return CreateVerticalHelper(layoutManager);
155 throw new ArgumentException("invalid orientation");
159 // Creates a horizontal OrientationHelper for the given LayoutManager.
161 // @param layoutManager The LayoutManager to attach to.
162 // @return A new OrientationHelper
163 public static OrientationHelper CreateHorizontalHelper(FlexibleViewLayoutManager layoutManager)
165 return new HorizontalHelper(layoutManager);
169 // Creates a vertical OrientationHelper for the given LayoutManager.
171 // @param layoutManager The LayoutManager to attach to.
172 // @return A new OrientationHelper
173 public static OrientationHelper CreateVerticalHelper(FlexibleViewLayoutManager layoutManager)
175 return new VerticalHelper(layoutManager);
179 internal class HorizontalHelper : OrientationHelper
181 public HorizontalHelper(FlexibleViewLayoutManager layoutManager) : base(layoutManager)
186 public override float GetEndAfterPadding()
188 return mLayoutManager.Width - mLayoutManager.PaddingRight;
191 public override float GetEnd()
193 return mLayoutManager.Width;
196 public override void OffsetChildren(float amount, bool immediate)
198 mLayoutManager.OffsetChildrenHorizontal(amount, immediate);
202 public override float GetStartAfterPadding()
204 return mLayoutManager.PaddingLeft;
207 public override float GetViewHolderMeasurement(FlexibleViewViewHolder holder)
209 return holder.Right - holder.Left;
212 public override float GetViewHolderMeasurementInOther(FlexibleViewViewHolder holder)
214 return holder.Bottom - holder.Top;
217 public override float GetViewHolderEnd(FlexibleViewViewHolder holder)
222 public override float GetViewHolderStart(FlexibleViewViewHolder holder)
227 public override float GetTotalSpace()
229 return mLayoutManager.Width - mLayoutManager.PaddingLeft
230 - mLayoutManager.PaddingRight;
233 internal override void OffsetChild(FlexibleViewViewHolder holder, int offset)
235 //holder.offsetLeftAndRight(offset);
238 public override float GetEndPadding()
240 return mLayoutManager.PaddingRight;
245 internal class VerticalHelper : OrientationHelper
247 public VerticalHelper(FlexibleViewLayoutManager layoutManager) : base(layoutManager)
252 public override float GetEndAfterPadding()
254 return mLayoutManager.Height - mLayoutManager.PaddingBottom;
257 public override float GetEnd()
259 return mLayoutManager.Height;
262 public override void OffsetChildren(float amount, bool immediate)
264 mLayoutManager.OffsetChildrenVertical(amount, immediate);
267 public override float GetStartAfterPadding()
269 return mLayoutManager.PaddingTop;
272 public override float GetViewHolderMeasurement(FlexibleViewViewHolder holder)
274 return holder.Bottom - holder.Top;
277 public override float GetViewHolderMeasurementInOther(FlexibleViewViewHolder holder)
279 return holder.Right - holder.Left;
282 public override float GetViewHolderEnd(FlexibleViewViewHolder holder)
284 return holder.Bottom;
287 public override float GetViewHolderStart(FlexibleViewViewHolder holder)
292 public override float GetTotalSpace()
294 return mLayoutManager.Height - mLayoutManager.PaddingTop
295 - mLayoutManager.PaddingBottom;
298 internal override void OffsetChild(FlexibleViewViewHolder holder, int offset)
300 //holder.offsetTopAndBottom(offset);
303 public override float GetEndPadding()
305 return mLayoutManager.PaddingBottom;