Skip to content

Commit 3fd1434

Browse files
authored
Fix the issue that margins for items added programmatically were not taken into account. (#359)
1 parent d1994f5 commit 3fd1434

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

demo-playground/src/main/java/com/google/android/flexbox/FlexboxLayoutFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class FlexboxLayoutFragment : Fragment() {
5757
flexContainer.addView(textView)
5858
}
5959
} else {
60-
for (i in 0..flexContainer.flexItemCount - 1) {
60+
for (i in 0 until flexContainer.flexItemCount) {
6161
flexContainer.getFlexItemAt(i).setOnClickListener(
6262
FlexItemClickListener(activity,
6363
FlexItemChangedListenerImpl(flexContainer), i))

flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.kt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,6 +3777,78 @@ class FlexboxAndroidTest {
37773777
onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout)))
37783778
}
37793779

3780+
@Test
3781+
@FlakyTest
3782+
@Throws(Throwable::class)
3783+
fun testAddItemProgrammatically_withMarginLayoutParams() {
3784+
val activity = activityRule.activity
3785+
val flexboxLayout = createFlexboxLayout(R.layout.activity_empty_children,
3786+
object : Configuration {
3787+
override fun apply(flexboxLayout: FlexboxLayout) {
3788+
flexboxLayout.alignItems = AlignItems.FLEX_START
3789+
val first = createTextView(activity, "1", 0)
3790+
val second = createTextView(activity, "2", 0)
3791+
val lp1 = ViewGroup.MarginLayoutParams(100, 100)
3792+
lp1.setMargins(10, 10, 10, 10)
3793+
val lp2 = ViewGroup.MarginLayoutParams(100, 100)
3794+
lp2.setMargins(20, 20, 20, 20)
3795+
first.layoutParams = lp1
3796+
second.layoutParams = lp2
3797+
flexboxLayout.addView(first)
3798+
flexboxLayout.addView(second)
3799+
}
3800+
})
3801+
3802+
assertThat(flexboxLayout.childCount, `is`(2))
3803+
val view1 = flexboxLayout.getChildAt(0)
3804+
val view2 = flexboxLayout.getChildAt(1)
3805+
// Assert the coordinates of the views added programmatically with margins
3806+
assertThat(view1.left, `is`(10))
3807+
assertThat(view1.top, `is`(10))
3808+
assertThat(view1.bottom, `is`(110))
3809+
assertThat(view1.right, `is`(110))
3810+
assertThat(view2.left, `is`(140))
3811+
assertThat(view2.top, `is`(20))
3812+
assertThat(view2.bottom, `is`(120))
3813+
assertThat(view2.right, `is`(240))
3814+
}
3815+
3816+
@Test
3817+
@FlakyTest
3818+
@Throws(Throwable::class)
3819+
fun testAddItemProgrammatically_withFlexboxLayoutLayoutParams() {
3820+
val activity = activityRule.activity
3821+
val flexboxLayout = createFlexboxLayout(R.layout.activity_empty_children,
3822+
object : Configuration {
3823+
override fun apply(flexboxLayout: FlexboxLayout) {
3824+
flexboxLayout.alignItems = AlignItems.FLEX_START
3825+
val first = createTextView(activity, "1", 0)
3826+
val second = createTextView(activity, "2", 0)
3827+
val lp1 = FlexboxLayout.LayoutParams(100, 100)
3828+
lp1.setMargins(10, 10, 10, 10)
3829+
val lp2 = FlexboxLayout.LayoutParams(100, 100)
3830+
lp2.setMargins(20, 20, 20, 20)
3831+
first.layoutParams = lp1
3832+
second.layoutParams = lp2
3833+
flexboxLayout.addView(first)
3834+
flexboxLayout.addView(second)
3835+
}
3836+
})
3837+
3838+
assertThat(flexboxLayout.childCount, `is`(2))
3839+
val view1 = flexboxLayout.getChildAt(0)
3840+
val view2 = flexboxLayout.getChildAt(1)
3841+
// Assert the coordinates of the views added programmatically with margins
3842+
assertThat(view1.left, `is`(10))
3843+
assertThat(view1.top, `is`(10))
3844+
assertThat(view1.bottom, `is`(110))
3845+
assertThat(view1.right, `is`(110))
3846+
assertThat(view2.left, `is`(140))
3847+
assertThat(view2.top, `is`(20))
3848+
assertThat(view2.bottom, `is`(120))
3849+
assertThat(view2.right, `is`(240))
3850+
}
3851+
37803852
@Throws(Throwable::class)
37813853
private fun createFlexboxLayout(@LayoutRes activityLayoutResId: Int,
37823854
configuration: Configuration = Configuration.EMPTY): FlexboxLayout {

flexbox/src/main/java/com/google/android/flexbox/FlexboxLayout.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,8 +1106,13 @@ public LayoutParams generateLayoutParams(AttributeSet attrs) {
11061106
}
11071107

11081108
@Override
1109-
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1110-
return new LayoutParams(p);
1109+
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
1110+
if (lp instanceof FlexboxLayout.LayoutParams) {
1111+
return new FlexboxLayout.LayoutParams((FlexboxLayout.LayoutParams) lp);
1112+
} else if (lp instanceof MarginLayoutParams) {
1113+
return new FlexboxLayout.LayoutParams((MarginLayoutParams) lp);
1114+
}
1115+
return new LayoutParams(lp);
11111116
}
11121117

11131118
@FlexDirection

0 commit comments

Comments
 (0)