Skip to content
This repository has been archived by the owner on Sep 11, 2021. It is now read-only.

Commit

Permalink
Fix the issue that margins for items added programmatically were not …
Browse files Browse the repository at this point in the history
…taken into account. (google#359)
  • Loading branch information
thagikura authored Sep 25, 2017
1 parent d1994f5 commit 3fd1434
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class FlexboxLayoutFragment : Fragment() {
flexContainer.addView(textView)
}
} else {
for (i in 0..flexContainer.flexItemCount - 1) {
for (i in 0 until flexContainer.flexItemCount) {
flexContainer.getFlexItemAt(i).setOnClickListener(
FlexItemClickListener(activity,
FlexItemChangedListenerImpl(flexContainer), i))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3777,6 +3777,78 @@ class FlexboxAndroidTest {
onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout)))
}

@Test
@FlakyTest
@Throws(Throwable::class)
fun testAddItemProgrammatically_withMarginLayoutParams() {
val activity = activityRule.activity
val flexboxLayout = createFlexboxLayout(R.layout.activity_empty_children,
object : Configuration {
override fun apply(flexboxLayout: FlexboxLayout) {
flexboxLayout.alignItems = AlignItems.FLEX_START
val first = createTextView(activity, "1", 0)
val second = createTextView(activity, "2", 0)
val lp1 = ViewGroup.MarginLayoutParams(100, 100)
lp1.setMargins(10, 10, 10, 10)
val lp2 = ViewGroup.MarginLayoutParams(100, 100)
lp2.setMargins(20, 20, 20, 20)
first.layoutParams = lp1
second.layoutParams = lp2
flexboxLayout.addView(first)
flexboxLayout.addView(second)
}
})

assertThat(flexboxLayout.childCount, `is`(2))
val view1 = flexboxLayout.getChildAt(0)
val view2 = flexboxLayout.getChildAt(1)
// Assert the coordinates of the views added programmatically with margins
assertThat(view1.left, `is`(10))
assertThat(view1.top, `is`(10))
assertThat(view1.bottom, `is`(110))
assertThat(view1.right, `is`(110))
assertThat(view2.left, `is`(140))
assertThat(view2.top, `is`(20))
assertThat(view2.bottom, `is`(120))
assertThat(view2.right, `is`(240))
}

@Test
@FlakyTest
@Throws(Throwable::class)
fun testAddItemProgrammatically_withFlexboxLayoutLayoutParams() {
val activity = activityRule.activity
val flexboxLayout = createFlexboxLayout(R.layout.activity_empty_children,
object : Configuration {
override fun apply(flexboxLayout: FlexboxLayout) {
flexboxLayout.alignItems = AlignItems.FLEX_START
val first = createTextView(activity, "1", 0)
val second = createTextView(activity, "2", 0)
val lp1 = FlexboxLayout.LayoutParams(100, 100)
lp1.setMargins(10, 10, 10, 10)
val lp2 = FlexboxLayout.LayoutParams(100, 100)
lp2.setMargins(20, 20, 20, 20)
first.layoutParams = lp1
second.layoutParams = lp2
flexboxLayout.addView(first)
flexboxLayout.addView(second)
}
})

assertThat(flexboxLayout.childCount, `is`(2))
val view1 = flexboxLayout.getChildAt(0)
val view2 = flexboxLayout.getChildAt(1)
// Assert the coordinates of the views added programmatically with margins
assertThat(view1.left, `is`(10))
assertThat(view1.top, `is`(10))
assertThat(view1.bottom, `is`(110))
assertThat(view1.right, `is`(110))
assertThat(view2.left, `is`(140))
assertThat(view2.top, `is`(20))
assertThat(view2.bottom, `is`(120))
assertThat(view2.right, `is`(240))
}

@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 @@ -1106,8 +1106,13 @@ public LayoutParams generateLayoutParams(AttributeSet attrs) {
}

@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
if (lp instanceof FlexboxLayout.LayoutParams) {
return new FlexboxLayout.LayoutParams((FlexboxLayout.LayoutParams) lp);
} else if (lp instanceof MarginLayoutParams) {
return new FlexboxLayout.LayoutParams((MarginLayoutParams) lp);
}
return new LayoutParams(lp);
}

@FlexDirection
Expand Down

0 comments on commit 3fd1434

Please sign in to comment.