michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.widget; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.TypedArray; michael@0: import android.util.AttributeSet; michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: michael@0: public class FlowLayout extends ViewGroup { michael@0: private int mSpacing; michael@0: michael@0: public FlowLayout(Context context) { michael@0: super(context); michael@0: } michael@0: michael@0: public FlowLayout(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: TypedArray a = context.obtainStyledAttributes(attrs, org.mozilla.gecko.R.styleable.FlowLayout); michael@0: mSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_spacing, (int) context.getResources().getDimension(R.dimen.flow_layout_spacing)); michael@0: a.recycle(); michael@0: } michael@0: michael@0: @Override michael@0: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { michael@0: final int parentWidth = MeasureSpec.getSize(widthMeasureSpec); michael@0: final int childCount = getChildCount(); michael@0: int rowWidth = 0; michael@0: int totalWidth = 0; michael@0: int totalHeight = 0; michael@0: boolean firstChild = true; michael@0: michael@0: for (int i = 0; i < childCount; i++) { michael@0: final View child = getChildAt(i); michael@0: if (child.getVisibility() == GONE) michael@0: continue; michael@0: michael@0: measureChild(child, widthMeasureSpec, heightMeasureSpec); michael@0: michael@0: final int childWidth = child.getMeasuredWidth(); michael@0: final int childHeight = child.getMeasuredHeight(); michael@0: michael@0: if (firstChild || (rowWidth + childWidth > parentWidth)) { michael@0: rowWidth = 0; michael@0: totalHeight += childHeight; michael@0: if (!firstChild) michael@0: totalHeight += mSpacing; michael@0: firstChild = false; michael@0: } michael@0: michael@0: rowWidth += childWidth; michael@0: michael@0: if (rowWidth > totalWidth) michael@0: totalWidth = rowWidth; michael@0: michael@0: rowWidth += mSpacing; michael@0: } michael@0: michael@0: setMeasuredDimension(totalWidth, totalHeight); michael@0: } michael@0: michael@0: @Override michael@0: protected void onLayout(boolean changed, int l, int t, int r, int b) { michael@0: final int childCount = getChildCount(); michael@0: final int totalWidth = r - l; michael@0: int x = 0; michael@0: int y = 0; michael@0: int prevChildHeight = 0; michael@0: michael@0: for (int i = 0; i < childCount; i++) { michael@0: final View child = getChildAt(i); michael@0: if (child.getVisibility() == GONE) michael@0: continue; michael@0: michael@0: final int childWidth = child.getMeasuredWidth(); michael@0: final int childHeight = child.getMeasuredHeight(); michael@0: if (x + childWidth > totalWidth) { michael@0: x = 0; michael@0: y += prevChildHeight + mSpacing; michael@0: } michael@0: prevChildHeight = childHeight; michael@0: child.layout(x, y, x + childWidth, y + childHeight); michael@0: x += childWidth + mSpacing; michael@0: } michael@0: } michael@0: }