diff --git a/flowlayout/src/main/java/com/nex3z/flowlayout/FlowLayout.java b/flowlayout/src/main/java/com/nex3z/flowlayout/FlowLayout.java index fedfaed..009c583 100644 --- a/flowlayout/src/main/java/com/nex3z/flowlayout/FlowLayout.java +++ b/flowlayout/src/main/java/com/nex3z/flowlayout/FlowLayout.java @@ -36,6 +36,7 @@ public class FlowLayout extends ViewGroup { private static final int DEFAULT_CHILD_SPACING_FOR_LAST_ROW = SPACING_UNDEFINED; private static final float DEFAULT_ROW_SPACING = 0; private static final boolean DEFAULT_RTL = false; + private static final int DEFAULT_MAX_ROWS = Integer.MAX_VALUE; private boolean mFlow = DEFAULT_FLOW; private int mChildSpacing = DEFAULT_CHILD_SPACING; @@ -43,6 +44,7 @@ public class FlowLayout extends ViewGroup { private float mRowSpacing = DEFAULT_ROW_SPACING; private float mAdjustedRowSpacing = DEFAULT_ROW_SPACING; private boolean mRtl = DEFAULT_RTL; + private int mMaxRows = DEFAULT_MAX_ROWS; private List mHorizontalSpacingForRow = new ArrayList<>(); private List mHeightForRow = new ArrayList<>(); @@ -74,6 +76,7 @@ public FlowLayout(Context context, AttributeSet attrs) { } catch (NumberFormatException e) { mRowSpacing = a.getDimension(R.styleable.FlowLayout_rowSpacing, dpToPx(DEFAULT_ROW_SPACING)); } + mMaxRows = a.getInt(R.styleable.FlowLayout_maxRows, DEFAULT_MAX_ROWS); mRtl = a.getBoolean(R.styleable.FlowLayout_rtl, DEFAULT_RTL); } finally { a.recycle(); @@ -126,7 +129,9 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { getSpacingForRow(childSpacing, rowSize, rowWidth, childNumInRow)); mChildNumForRow.add(childNumInRow); mHeightForRow.add(maxChildHeightInRow); - measuredHeight += maxChildHeightInRow; + if (mHorizontalSpacingForRow.size() <= mMaxRows){ + measuredHeight += maxChildHeightInRow; + } measuredWidth = max(measuredWidth, rowWidth); // Place the child view to next row @@ -163,7 +168,9 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mChildNumForRow.add(childNumInRow); mHeightForRow.add(maxChildHeightInRow); - measuredHeight += maxChildHeightInRow; + if (mHorizontalSpacingForRow.size() <= mMaxRows){ + measuredHeight += maxChildHeightInRow; + } measuredWidth = max(measuredWidth, rowWidth); if (childSpacing == SPACING_AUTO) { @@ -175,7 +182,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { } measuredHeight += getPaddingTop() + getPaddingBottom(); - int rowNum = mHorizontalSpacingForRow.size(); + int rowNum = min(mHorizontalSpacingForRow.size(), mMaxRows); float rowSpacing = mRowSpacing == SPACING_AUTO && heightMode == MeasureSpec.UNSPECIFIED ? 0 : mRowSpacing; if (rowSpacing == SPACING_AUTO) { @@ -338,6 +345,15 @@ public void setRowSpacing(float rowSpacing) { requestLayout(); } + public int getMaxRows() { + return mMaxRows; + } + + public void maxRows(int maxRows) { + mMaxRows = maxRows; + requestLayout(); + } + private int max(int a, int b) { return a > b ? a : b; } diff --git a/flowlayout/src/main/res/values/attrs.xml b/flowlayout/src/main/res/values/attrs.xml index b3f55fe..4d3784b 100644 --- a/flowlayout/src/main/res/values/attrs.xml +++ b/flowlayout/src/main/res/values/attrs.xml @@ -13,5 +13,6 @@ + \ No newline at end of file diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 2f1524e..80635a6 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -17,6 +17,7 @@ android:id="@+id/flow" android:layout_width="match_parent" android:layout_height="wrap_content" + app:maxRows="2" app:childSpacing="auto" app:rowSpacing="8dp"/>