mobile/android/base/widget/FlowLayout.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/widget/FlowLayout.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.widget;
     1.9 +
    1.10 +import org.mozilla.gecko.R;
    1.11 +
    1.12 +import android.content.Context;
    1.13 +import android.content.res.TypedArray;
    1.14 +import android.util.AttributeSet;
    1.15 +import android.view.View;
    1.16 +import android.view.ViewGroup;
    1.17 +
    1.18 +public class FlowLayout extends ViewGroup {
    1.19 +    private int mSpacing;
    1.20 +
    1.21 +    public FlowLayout(Context context) {
    1.22 +        super(context);
    1.23 +    }
    1.24 +
    1.25 +    public FlowLayout(Context context, AttributeSet attrs) {
    1.26 +        super(context, attrs);
    1.27 +        TypedArray a = context.obtainStyledAttributes(attrs, org.mozilla.gecko.R.styleable.FlowLayout);
    1.28 +        mSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_spacing, (int) context.getResources().getDimension(R.dimen.flow_layout_spacing));
    1.29 +        a.recycle();
    1.30 +    }
    1.31 +
    1.32 +    @Override
    1.33 +    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    1.34 +        final int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    1.35 +        final int childCount = getChildCount();
    1.36 +        int rowWidth = 0;
    1.37 +        int totalWidth = 0;
    1.38 +        int totalHeight = 0;
    1.39 +        boolean firstChild = true;
    1.40 +
    1.41 +        for (int i = 0; i < childCount; i++) {
    1.42 +            final View child = getChildAt(i);
    1.43 +            if (child.getVisibility() == GONE)
    1.44 +                continue;
    1.45 +
    1.46 +            measureChild(child, widthMeasureSpec, heightMeasureSpec);
    1.47 +
    1.48 +            final int childWidth = child.getMeasuredWidth();
    1.49 +            final int childHeight = child.getMeasuredHeight();
    1.50 +
    1.51 +            if (firstChild || (rowWidth + childWidth > parentWidth)) {
    1.52 +                rowWidth = 0;
    1.53 +                totalHeight += childHeight;
    1.54 +                if (!firstChild)
    1.55 +                    totalHeight += mSpacing;
    1.56 +                firstChild = false;
    1.57 +            }
    1.58 +
    1.59 +            rowWidth += childWidth;
    1.60 +
    1.61 +            if (rowWidth > totalWidth)
    1.62 +                totalWidth = rowWidth;
    1.63 +
    1.64 +            rowWidth += mSpacing;
    1.65 +        }
    1.66 +
    1.67 +        setMeasuredDimension(totalWidth, totalHeight);
    1.68 +    }
    1.69 +
    1.70 +    @Override
    1.71 +    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    1.72 +        final int childCount = getChildCount();
    1.73 +        final int totalWidth = r - l;
    1.74 +        int x = 0;
    1.75 +        int y = 0;
    1.76 +        int prevChildHeight = 0;
    1.77 +
    1.78 +        for (int i = 0; i < childCount; i++) {
    1.79 +            final View child = getChildAt(i);
    1.80 +            if (child.getVisibility() == GONE)
    1.81 +                continue;
    1.82 +
    1.83 +            final int childWidth = child.getMeasuredWidth();
    1.84 +            final int childHeight = child.getMeasuredHeight();
    1.85 +            if (x + childWidth > totalWidth) {
    1.86 +                x = 0;
    1.87 +                y += prevChildHeight + mSpacing;
    1.88 +            }
    1.89 +            prevChildHeight = childHeight;
    1.90 +            child.layout(x, y, x + childWidth, y + childHeight);
    1.91 +            x += childWidth + mSpacing;
    1.92 +        }
    1.93 +    }
    1.94 +}

mercurial