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.CommonUI
22 * Helper class for LayoutManagers to abstract measurements depending on the View's orientation.
23 * It is developed to easily support vertical and horizontal orientations in a LayoutManager but
24 * can also be used to abstract calls around view bounds and child measurements with margins and
27 * @see #createHorizontalHelper(RecyclerView.LayoutManager)
28 * @see #createVerticalHelper(RecyclerView.LayoutManager)
30 internal abstract class OrientationHelper
32 public static readonly int HORIZONTAL = 0;
33 public static readonly int VERTICAL = 1;
35 private static readonly int INVALID_SIZE = -1;
37 protected FlexibleView.LayoutManager mLayoutManager;
39 private float mLastTotalSpace = INVALID_SIZE;
41 public OrientationHelper(FlexibleView.LayoutManager layoutManager)
43 mLayoutManager = layoutManager;
47 * Call this method after onLayout method is complete if state is NOT pre-layout.
48 * This method records information like layout bounds that might be useful in the next layout
51 public void OnLayoutComplete()
53 mLastTotalSpace = GetTotalSpace();
57 * Returns the layout space change between the previous layout pass and current layout pass.
58 * Make sure you call {@link #onLayoutComplete()} at the end of your LayoutManager's
59 * {@link RecyclerView.LayoutManager#onLayoutChildren(RecyclerView.Recycler,
60 * RecyclerView.State)} method.
62 * @return The difference between the current total space and previous layout's total space.
63 * @see #onLayoutComplete()
65 public float GetTotalSpaceChange()
67 return INVALID_SIZE == mLastTotalSpace ? 0 : GetTotalSpace() - mLastTotalSpace;
71 * Returns the start of the view including its decoration and margin.
72 * For example, for the horizontal helper, if a View's left is at pixel 20, has 2px left
73 * decoration and 3px left margin, returned value will be 15px.
75 * @param view The view element to check
76 * @return The first pixel of the element
77 * @see #getDecoratedEnd(android.view.View)
79 public abstract float GetViewHolderStart(FlexibleView.ViewHolder holder);
82 * Returns the end of the view including its decoration and margin.
83 * For example, for the horizontal helper, if a View's right is at pixel 200, has 2px right
84 * decoration and 3px right margin, returned value will be 205.
86 * @param view The view element to check
87 * @return The last pixel of the element
88 * @see #getDecoratedStart(android.view.View)
90 public abstract float GetViewHolderEnd(FlexibleView.ViewHolder holder);
93 * Returns the space occupied by this View in the current orientation including decorations and
96 * @param view The view element to check
97 * @return Total space occupied by this view
98 * @see #getDecoratedMeasurementInOther(View)
100 public abstract float GetViewHolderMeasurement(FlexibleView.ViewHolder holder);
103 * Returns the space occupied by this View in the perpendicular orientation including
104 * decorations and margins.
106 * @param view The view element to check
107 * @return Total space occupied by this view in the perpendicular orientation to current one
108 * @see #getDecoratedMeasurement(View)
110 public abstract float GetViewHolderMeasurementInOther(FlexibleView.ViewHolder holder);
113 * Returns the start position of the layout after the start padding is added.
115 * @return The very first pixel we can draw.
117 public abstract float GetStartAfterPadding();
120 * Returns the end position of the layout after the end padding is removed.
122 * @return The end boundary for this layout.
124 public abstract float GetEndAfterPadding();
127 * Returns the end position of the layout without taking padding into account.
129 * @return The end boundary for this layout without considering padding.
131 public abstract float GetEnd();
134 * Offsets all children's positions by the given amount.
136 * @param amount Value to add to each child's layout parameters
138 public abstract void OffsetChildren(float amount, bool immediate);
141 * Returns the total space to layout. This number is the difference between
142 * {@link #getEndAfterPadding()} and {@link #getStartAfterPadding()}.
144 * @return Total space to layout children
146 public abstract float GetTotalSpace();
149 * Offsets the child in this orientation.
151 * @param view View to offset
152 * @param offset offset amount
154 internal abstract void OffsetChild(FlexibleView.ViewHolder holder, int offset);
157 * Returns the padding at the end of the layout. For horizontal helper, this is the right
158 * padding and for vertical helper, this is the bottom padding. This method does not check
159 * whether the layout is RTL or not.
161 * @return The padding at the end of the layout.
163 public abstract float GetEndPadding();
166 * Creates an OrientationHelper for the given LayoutManager and orientation.
168 * @param layoutManager LayoutManager to attach to
169 * @param orientation Desired orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}
170 * @return A new OrientationHelper
172 public static OrientationHelper CreateOrientationHelper(
173 FlexibleView.LayoutManager layoutManager, int orientation)
175 if (orientation == HORIZONTAL)
177 return CreateHorizontalHelper(layoutManager);
179 else if (orientation == VERTICAL)
181 return CreateVerticalHelper(layoutManager);
184 throw new ArgumentException("invalid orientation");
189 * Creates a horizontal OrientationHelper for the given LayoutManager.
191 * @param layoutManager The LayoutManager to attach to.
192 * @return A new OrientationHelper
194 public static OrientationHelper CreateHorizontalHelper(FlexibleView.LayoutManager layoutManager)
196 return new HorizontalHelper(layoutManager);
200 * Creates a vertical OrientationHelper for the given LayoutManager.
202 * @param layoutManager The LayoutManager to attach to.
203 * @return A new OrientationHelper
205 public static OrientationHelper CreateVerticalHelper(FlexibleView.LayoutManager layoutManager)
207 return new VerticalHelper(layoutManager);
211 internal class HorizontalHelper : OrientationHelper
213 public HorizontalHelper(FlexibleView.LayoutManager layoutManager): base(layoutManager)
218 public override float GetEndAfterPadding()
220 return mLayoutManager.GetWidth() - mLayoutManager.GetPaddingRight();
223 public override float GetEnd()
225 return mLayoutManager.GetWidth();
228 public override void OffsetChildren(float amount, bool immediate)
230 mLayoutManager.OffsetChildrenHorizontal(amount, immediate);
234 public override float GetStartAfterPadding()
236 return mLayoutManager.GetPaddingLeft();
239 public override float GetViewHolderMeasurement(FlexibleView.ViewHolder holder)
241 return holder.Right - holder.Left;
244 public override float GetViewHolderMeasurementInOther(FlexibleView.ViewHolder holder)
246 return holder.Bottom - holder.Top;
249 public override float GetViewHolderEnd(FlexibleView.ViewHolder holder)
254 public override float GetViewHolderStart(FlexibleView.ViewHolder holder)
259 public override float GetTotalSpace()
261 return mLayoutManager.GetWidth() - mLayoutManager.GetPaddingLeft()
262 - mLayoutManager.GetPaddingRight();
265 internal override void OffsetChild(FlexibleView.ViewHolder holder, int offset)
267 //holder.offsetLeftAndRight(offset);
270 public override float GetEndPadding()
272 return mLayoutManager.GetPaddingRight();
277 internal class VerticalHelper : OrientationHelper
279 public VerticalHelper(FlexibleView.LayoutManager layoutManager) : base(layoutManager)
284 public override float GetEndAfterPadding()
286 return mLayoutManager.GetHeight() - mLayoutManager.GetPaddingBottom();
289 public override float GetEnd()
291 return mLayoutManager.GetHeight();
294 public override void OffsetChildren(float amount, bool immediate)
296 mLayoutManager.OffsetChildrenVertical(amount, immediate);
299 public override float GetStartAfterPadding()
301 return mLayoutManager.GetPaddingTop();
304 public override float GetViewHolderMeasurement(FlexibleView.ViewHolder holder)
306 return holder.Bottom - holder.Top;
309 public override float GetViewHolderMeasurementInOther(FlexibleView.ViewHolder holder)
311 return holder.Right - holder.Left;
314 public override float GetViewHolderEnd(FlexibleView.ViewHolder holder)
316 return holder.Bottom;
319 public override float GetViewHolderStart(FlexibleView.ViewHolder holder)
324 public override float GetTotalSpace()
326 return mLayoutManager.GetHeight() - mLayoutManager.GetPaddingTop()
327 - mLayoutManager.GetPaddingBottom();
330 internal override void OffsetChild(FlexibleView.ViewHolder holder, int offset)
332 //holder.offsetTopAndBottom(offset);
335 public override float GetEndPadding()
337 return mLayoutManager.GetPaddingBottom();