Skip to content

Commit

Permalink
Add max line attribute to specify the maximum number of flex lines.
Browse files Browse the repository at this point in the history
Fixes #156
  • Loading branch information
thagikura committed May 14, 2018
1 parent b397060 commit 1870940
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.android.flexbox

import android.view.View
import android.view.ViewGroup
import com.google.android.flexbox.FlexContainer.NOT_SET

/**
* Fake implementation of [FlexContainer].
Expand All @@ -43,6 +44,8 @@ internal class FakeFlexContainer : FlexContainer {
@AlignContent
private var alignContent = AlignContent.STRETCH

private var maxLine = -1

override fun getFlexItemCount() = views.size

override fun getFlexItemAt(index: Int) = views[index]
Expand Down Expand Up @@ -95,6 +98,12 @@ internal class FakeFlexContainer : FlexContainer {
this.alignItems = alignItems
}

override fun getMaxLine(): Int = this.maxLine

override fun setMaxLine(maxLine: Int) {
this.maxLine = maxLine
}

override fun getFlexLines() = flexLines

override fun isMainAxisDirectionHorizontal(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3967,6 +3967,28 @@ class FlexboxAndroidTest {
assertThat(view2.right, `is`(240))
}

@Test
@FlakyTest
@Throws(Throwable::class)
fun testMaxLines() {
val activity = activityRule.activity
val flexboxLayout = createFlexboxLayout(R.layout.activity_empty_children,
object : Configuration {
override fun apply(flexboxLayout: FlexboxLayout) {
flexboxLayout.maxLine = 3
for (i in 1..50) {
val textView = createTextView(activity, i.toString(), 0)
val lp = FlexboxLayout.LayoutParams(100, 100)
lp.flexShrink = 0f
textView.layoutParams = lp
flexboxLayout.addView(textView)
}
}
})
assertThat(flexboxLayout.childCount, `is`(50))
assertThat(flexboxLayout.flexLines.size, `is`(3))
}

@Throws(Throwable::class)
private fun createFlexboxLayout(@LayoutRes activityLayoutResId: Int,
configuration: Configuration = Configuration.EMPTY): FlexboxLayout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3310,6 +3310,30 @@ class FlexboxLayoutManagerTest {
assertThat(layoutManager.getChildAt(0).top, `is`(0))
}

@Test
@FlakyTest
@Throws(Throwable::class)
fun testMaxLine() {
val activity = activityRule.activity
val layoutManager = FlexboxLayoutManager(activity)
val adapter = TestAdapter()
activityRule.runOnUiThread {
activity.setContentView(R.layout.recyclerview)
val recyclerView = activity.findViewById<RecyclerView>(R.id.recyclerview)
layoutManager.flexDirection = FlexDirection.ROW
layoutManager.maxLine = 3
recyclerView.layoutManager = layoutManager
recyclerView.adapter = adapter
for (i in 1..50) {
val lp = createLayoutParams(activity, 100, 70)
lp.flexShrink = 0f
adapter.addItem(lp)
}
}
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
assertThat(layoutManager.flexLines.size, `is`(3))
}

/**
* Creates a new flex item.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,14 @@ interface FlexContainer {
void setFlexLines(List<FlexLine> flexLines);

/**
*
* @return the current value of the maximum number of flex lines. If not set, {@link #NOT_SET}
* is returned.
*/
int getMaxLine();

/**
*
* @param maxLine the int value, which specifies the maximum number of lines
* @param maxLine the int value, which specifies the maximum number of flex lines
*/
void setMaxLine(int maxLine);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ void calculateFlexLines(FlexLinesResult result, int mainMeasureSpec,
getViewMeasuredSizeMain(child, isMainHorizontal)
+ getFlexItemMarginStartMain(flexItem, isMainHorizontal) +
getFlexItemMarginEndMain(flexItem, isMainHorizontal),
flexItem, i, indexInFlexLine)) {
flexItem, i, indexInFlexLine, flexLines.size())) {
if (flexLine.getItemCountNotGone() > 0) {
addFlexLine(flexLines, flexLine, i > 0 ? i - 1 : 0, sumCrossSize);
sumCrossSize += flexLine.mCrossSize;
Expand Down Expand Up @@ -824,12 +824,15 @@ private int getFlexItemMarginEndCross(FlexItem flexItem, boolean isMainHorizonta
* @param childLength the length of a child view which is to be collected to the flex line
* @param flexItem the LayoutParams for the view being determined whether a new flex line
* is needed
* @param index the index of the view being added within the entire flex container
* @param indexInFlexLine the index of the view being added within the current flex line
* @param flexLinesSize the number of the existing flexlines size
* @return {@code true} if a wrap is required, {@code false} otherwise
* @see FlexContainer#getFlexWrap()
* @see FlexContainer#setFlexWrap(int)
*/
private boolean isWrapRequired(View view, int mode, int maxSize, int currentLength,
int childLength, FlexItem flexItem, int index, int indexInFlexLine) {
int childLength, FlexItem flexItem, int index, int indexInFlexLine, int flexLinesSize) {
if (mFlexContainer.getFlexWrap() == FlexWrap.NOWRAP) {
return false;
}
Expand All @@ -840,7 +843,11 @@ private boolean isWrapRequired(View view, int mode, int maxSize, int currentLeng
return false;
}
int maxLine = mFlexContainer.getMaxLine();
if (maxLine != NOT_SET && maxLine <= )
// Judge the condition by adding 1 to the current flexLinesSize because the flex line
// being computed isn't added to the flexLinesSize.
if (maxLine != NOT_SET && maxLine <= flexLinesSize + 1) {
return false;
}
int decorationLength =
mFlexContainer.getDecorationLengthMainAxis(view, index, indexInFlexLine);
if (decorationLength > 0) {
Expand Down

1 comment on commit 1870940

@Pradeepa-precious
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can any one plz explain where I have changes made to add max line attribute.....I am not clear with this. Am struggling a long...

Please # to comment.