From 3fd14340cbe65ba86149daddf4bdc74fd3137dbd Mon Sep 17 00:00:00 2001 From: Takeshi Hagikura Date: Mon, 25 Sep 2017 14:21:49 +0900 Subject: [PATCH] Fix the issue that margins for items added programmatically were not taken into account. (#359) --- .../android/flexbox/FlexboxLayoutFragment.kt | 2 +- .../flexbox/test/FlexboxAndroidTest.kt | 72 +++++++++++++++++++ .../google/android/flexbox/FlexboxLayout.java | 9 ++- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/demo-playground/src/main/java/com/google/android/flexbox/FlexboxLayoutFragment.kt b/demo-playground/src/main/java/com/google/android/flexbox/FlexboxLayoutFragment.kt index 6edd16f1..f46f17ed 100644 --- a/demo-playground/src/main/java/com/google/android/flexbox/FlexboxLayoutFragment.kt +++ b/demo-playground/src/main/java/com/google/android/flexbox/FlexboxLayoutFragment.kt @@ -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)) diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.kt index 1ba8c8d3..56f65bee 100644 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.kt +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.kt @@ -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 { diff --git a/flexbox/src/main/java/com/google/android/flexbox/FlexboxLayout.java b/flexbox/src/main/java/com/google/android/flexbox/FlexboxLayout.java index 95ef3855..a4986b50 100644 --- a/flexbox/src/main/java/com/google/android/flexbox/FlexboxLayout.java +++ b/flexbox/src/main/java/com/google/android/flexbox/FlexboxLayout.java @@ -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