mobile/android/base/widget/FlowLayout.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.widget;
michael@0 6
michael@0 7 import org.mozilla.gecko.R;
michael@0 8
michael@0 9 import android.content.Context;
michael@0 10 import android.content.res.TypedArray;
michael@0 11 import android.util.AttributeSet;
michael@0 12 import android.view.View;
michael@0 13 import android.view.ViewGroup;
michael@0 14
michael@0 15 public class FlowLayout extends ViewGroup {
michael@0 16 private int mSpacing;
michael@0 17
michael@0 18 public FlowLayout(Context context) {
michael@0 19 super(context);
michael@0 20 }
michael@0 21
michael@0 22 public FlowLayout(Context context, AttributeSet attrs) {
michael@0 23 super(context, attrs);
michael@0 24 TypedArray a = context.obtainStyledAttributes(attrs, org.mozilla.gecko.R.styleable.FlowLayout);
michael@0 25 mSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_spacing, (int) context.getResources().getDimension(R.dimen.flow_layout_spacing));
michael@0 26 a.recycle();
michael@0 27 }
michael@0 28
michael@0 29 @Override
michael@0 30 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
michael@0 31 final int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
michael@0 32 final int childCount = getChildCount();
michael@0 33 int rowWidth = 0;
michael@0 34 int totalWidth = 0;
michael@0 35 int totalHeight = 0;
michael@0 36 boolean firstChild = true;
michael@0 37
michael@0 38 for (int i = 0; i < childCount; i++) {
michael@0 39 final View child = getChildAt(i);
michael@0 40 if (child.getVisibility() == GONE)
michael@0 41 continue;
michael@0 42
michael@0 43 measureChild(child, widthMeasureSpec, heightMeasureSpec);
michael@0 44
michael@0 45 final int childWidth = child.getMeasuredWidth();
michael@0 46 final int childHeight = child.getMeasuredHeight();
michael@0 47
michael@0 48 if (firstChild || (rowWidth + childWidth > parentWidth)) {
michael@0 49 rowWidth = 0;
michael@0 50 totalHeight += childHeight;
michael@0 51 if (!firstChild)
michael@0 52 totalHeight += mSpacing;
michael@0 53 firstChild = false;
michael@0 54 }
michael@0 55
michael@0 56 rowWidth += childWidth;
michael@0 57
michael@0 58 if (rowWidth > totalWidth)
michael@0 59 totalWidth = rowWidth;
michael@0 60
michael@0 61 rowWidth += mSpacing;
michael@0 62 }
michael@0 63
michael@0 64 setMeasuredDimension(totalWidth, totalHeight);
michael@0 65 }
michael@0 66
michael@0 67 @Override
michael@0 68 protected void onLayout(boolean changed, int l, int t, int r, int b) {
michael@0 69 final int childCount = getChildCount();
michael@0 70 final int totalWidth = r - l;
michael@0 71 int x = 0;
michael@0 72 int y = 0;
michael@0 73 int prevChildHeight = 0;
michael@0 74
michael@0 75 for (int i = 0; i < childCount; i++) {
michael@0 76 final View child = getChildAt(i);
michael@0 77 if (child.getVisibility() == GONE)
michael@0 78 continue;
michael@0 79
michael@0 80 final int childWidth = child.getMeasuredWidth();
michael@0 81 final int childHeight = child.getMeasuredHeight();
michael@0 82 if (x + childWidth > totalWidth) {
michael@0 83 x = 0;
michael@0 84 y += prevChildHeight + mSpacing;
michael@0 85 }
michael@0 86 prevChildHeight = childHeight;
michael@0 87 child.layout(x, y, x + childWidth, y + childHeight);
michael@0 88 x += childWidth + mSpacing;
michael@0 89 }
michael@0 90 }
michael@0 91 }

mercurial