Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions flowlayout/src/main/java/com/nex3z/flowlayout/FlowLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ 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;
private int mChildSpacingForLastRow = DEFAULT_CHILD_SPACING_FOR_LAST_ROW;
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<Float> mHorizontalSpacingForRow = new ArrayList<>();
private List<Integer> mHeightForRow = new ArrayList<>();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions flowlayout/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<enum name="auto" value="-65536"/>
</attr>
<attr name="rtl" format="boolean"/>
<attr name="maxRows" format="integer"/>
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

Expand Down