• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 package com.example.android.apis.view;
18 
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 import android.graphics.Rect;
22 import com.example.android.apis.R;
23 
24 //BEGIN_INCLUDE(Complete)
25 import android.content.Context;
26 import android.content.res.TypedArray;
27 import android.util.AttributeSet;
28 import android.view.Gravity;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.RemoteViews;
32 
33 /**
34  * Example of writing a custom layout manager.  This is a fairly full-featured
35  * layout manager that is relatively general, handling all layout cases.  You
36  * can simplify it for more specific cases.
37  */
38 @RemoteViews.RemoteView
39 public class CustomLayout extends ViewGroup {
40     /** The amount of space used by children in the left gutter. */
41     private int mLeftWidth;
42 
43     /** The amount of space used by children in the right gutter. */
44     private int mRightWidth;
45 
46     /** These are used for computing child frames based on their gravity. */
47     private final Rect mTmpContainerRect = new Rect();
48     private final Rect mTmpChildRect = new Rect();
49 
CustomLayout(Context context)50     public CustomLayout(Context context) {
51         super(context);
52     }
53 
CustomLayout(Context context, AttributeSet attrs)54     public CustomLayout(Context context, AttributeSet attrs) {
55         this(context, attrs, 0);
56     }
57 
CustomLayout(Context context, AttributeSet attrs, int defStyle)58     public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
59         super(context, attrs, defStyle);
60     }
61 
62     /**
63      * Any layout manager that doesn't scroll will want this.
64      */
65     @Override
shouldDelayChildPressedState()66     public boolean shouldDelayChildPressedState() {
67         return false;
68     }
69 
70     /**
71      * Ask all children to measure themselves and compute the measurement of this
72      * layout based on the children.
73      */
74     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)75     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
76         int count = getChildCount();
77 
78         // These keep track of the space we are using on the left and right for
79         // views positioned there; we need member variables so we can also use
80         // these for layout later.
81         mLeftWidth = 0;
82         mRightWidth = 0;
83 
84         // Measurement will ultimately be computing these values.
85         int maxHeight = 0;
86         int maxWidth = 0;
87         int childState = 0;
88 
89         // Iterate through all children, measuring them and computing our dimensions
90         // from their size.
91         for (int i = 0; i < count; i++) {
92             final View child = getChildAt(i);
93             if (child.getVisibility() != GONE) {
94                 // Measure the child.
95                 measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
96 
97                 // Update our size information based on the layout params.  Children
98                 // that asked to be positioned on the left or right go in those gutters.
99                 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
100                 if (lp.position == LayoutParams.POSITION_LEFT) {
101                     mLeftWidth += Math.max(maxWidth,
102                             child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
103                 } else if (lp.position == LayoutParams.POSITION_RIGHT) {
104                     mRightWidth += Math.max(maxWidth,
105                             child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
106                 } else {
107                     maxWidth = Math.max(maxWidth,
108                             child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
109                 }
110                 maxHeight = Math.max(maxHeight,
111                         child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
112                 childState = combineMeasuredStates(childState, child.getMeasuredState());
113             }
114         }
115 
116         // Total width is the maximum width of all inner children plus the gutters.
117         maxWidth += mLeftWidth + mRightWidth;
118 
119         // Check against our minimum height and width
120         maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
121         maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
122 
123         // Report our final dimensions.
124         setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
125                 resolveSizeAndState(maxHeight, heightMeasureSpec,
126                         childState << MEASURED_HEIGHT_STATE_SHIFT));
127     }
128 
129     /**
130      * Position all children within this layout.
131      */
132     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)133     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
134         final int count = getChildCount();
135 
136         // These are the far left and right edges in which we are performing layout.
137         int leftPos = getPaddingLeft();
138         int rightPos = right - left - getPaddingRight();
139 
140         // This is the middle region inside of the gutter.
141         final int middleLeft = leftPos + mLeftWidth;
142         final int middleRight = rightPos - mRightWidth;
143 
144         // These are the top and bottom edges in which we are performing layout.
145         final int parentTop = getPaddingTop();
146         final int parentBottom = bottom - top - getPaddingBottom();
147 
148         for (int i = 0; i < count; i++) {
149             final View child = getChildAt(i);
150             if (child.getVisibility() != GONE) {
151                 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
152 
153                 final int width = child.getMeasuredWidth();
154                 final int height = child.getMeasuredHeight();
155 
156                 // Compute the frame in which we are placing this child.
157                 if (lp.position == LayoutParams.POSITION_LEFT) {
158                     mTmpContainerRect.left = leftPos + lp.leftMargin;
159                     mTmpContainerRect.right = leftPos + width + lp.rightMargin;
160                     leftPos = mTmpContainerRect.right;
161                 } else if (lp.position == LayoutParams.POSITION_RIGHT) {
162                     mTmpContainerRect.right = rightPos - lp.rightMargin;
163                     mTmpContainerRect.left = rightPos - width - lp.leftMargin;
164                     rightPos = mTmpContainerRect.left;
165                 } else {
166                     mTmpContainerRect.left = middleLeft + lp.leftMargin;
167                     mTmpContainerRect.right = middleRight - lp.rightMargin;
168                 }
169                 mTmpContainerRect.top = parentTop + lp.topMargin;
170                 mTmpContainerRect.bottom = parentBottom - lp.bottomMargin;
171 
172                 // Use the child's gravity and size to determine its final
173                 // frame within its container.
174                 Gravity.apply(lp.gravity, width, height, mTmpContainerRect, mTmpChildRect);
175 
176                 // Place the child.
177                 child.layout(mTmpChildRect.left, mTmpChildRect.top,
178                         mTmpChildRect.right, mTmpChildRect.bottom);
179             }
180         }
181     }
182 
183     // ----------------------------------------------------------------------
184     // The rest of the implementation is for custom per-child layout parameters.
185     // If you do not need these (for example you are writing a layout manager
186     // that does fixed positioning of its children), you can drop all of this.
187 
188     @Override
generateLayoutParams(AttributeSet attrs)189     public LayoutParams generateLayoutParams(AttributeSet attrs) {
190         return new CustomLayout.LayoutParams(getContext(), attrs);
191     }
192 
193     @Override
generateDefaultLayoutParams()194     protected LayoutParams generateDefaultLayoutParams() {
195         return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
196     }
197 
198     @Override
generateLayoutParams(ViewGroup.LayoutParams p)199     protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
200         return new LayoutParams(p);
201     }
202 
203     @Override
checkLayoutParams(ViewGroup.LayoutParams p)204     protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
205         return p instanceof LayoutParams;
206     }
207 
208     /**
209      * Custom per-child layout information.
210      */
211     public static class LayoutParams extends MarginLayoutParams {
212         /**
213          * The gravity to apply with the View to which these layout parameters
214          * are associated.
215          */
216         public int gravity = Gravity.TOP | Gravity.START;
217 
218         public static int POSITION_MIDDLE = 0;
219         public static int POSITION_LEFT = 1;
220         public static int POSITION_RIGHT = 2;
221 
222         public int position = POSITION_MIDDLE;
223 
LayoutParams(Context c, AttributeSet attrs)224         public LayoutParams(Context c, AttributeSet attrs) {
225             super(c, attrs);
226 
227             // Pull the layout param values from the layout XML during
228             // inflation.  This is not needed if you don't care about
229             // changing the layout behavior in XML.
230             TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.CustomLayoutLP);
231             gravity = a.getInt(R.styleable.CustomLayoutLP_android_layout_gravity, gravity);
232             position = a.getInt(R.styleable.CustomLayoutLP_layout_position, position);
233             a.recycle();
234         }
235 
LayoutParams(int width, int height)236         public LayoutParams(int width, int height) {
237             super(width, height);
238         }
239 
LayoutParams(ViewGroup.LayoutParams source)240         public LayoutParams(ViewGroup.LayoutParams source) {
241             super(source);
242         }
243     }
244 }
245 //END_INCLUDE(Complete)
246