未验证 提交 3aa56093 编写于 作者: T Takeshi Hagikura 提交者: GitHub

Implement space_evenly as one of the values for Justify Content. (#404)

Implement space_evenly as one of the values for Justify Content.
上级 cd4c6352
......@@ -61,6 +61,8 @@ internal class FragmentHelper(private val activity: MainActivity, private val fl
private lateinit var SPACE_AROUND: String
private lateinit var SPACE_EVENLY: String
private lateinit var sharedPreferences: SharedPreferences
fun initializeViews() {
......@@ -91,6 +93,7 @@ internal class FragmentHelper(private val activity: MainActivity, private val fl
STRETCH = activity.getString(R.string.stretch)
SPACE_BETWEEN = activity.getString(R.string.space_between)
SPACE_AROUND = activity.getString(R.string.space_around)
SPACE_EVENLY = activity.getString(R.string.space_evenly)
}
/**
......@@ -224,6 +227,7 @@ internal class FragmentHelper(private val activity: MainActivity, private val fl
CENTER -> JustifyContent.CENTER
SPACE_BETWEEN -> JustifyContent.SPACE_BETWEEN
SPACE_AROUND -> JustifyContent.SPACE_AROUND
SPACE_EVENLY -> JustifyContent.SPACE_EVENLY
else -> return
}
}
......@@ -239,6 +243,7 @@ internal class FragmentHelper(private val activity: MainActivity, private val fl
JustifyContent.CENTER -> CENTER
JustifyContent.SPACE_AROUND -> SPACE_AROUND
JustifyContent.SPACE_BETWEEN -> SPACE_BETWEEN
JustifyContent.SPACE_EVENLY -> SPACE_EVENLY
else -> FLEX_START
}
}
......
......@@ -42,6 +42,7 @@ limitations under the License.
<string name="center" translatable="false">Center</string>
<string name="space_between" translatable="false">Space Between</string>
<string name="space_around" translatable="false">Space Around</string>
<string name="space_evenly" translatable="false">Space Evenly</string>
<string name="baseline" translatable="false">Baseline</string>
<string name="stretch" translatable="false">Stretch</string>
<string name="auto" translatable="false">Auto</string>
......@@ -65,6 +66,7 @@ limitations under the License.
<item>@string/center</item>
<item>@string/space_between</item>
<item>@string/space_around</item>
<item>@string/space_evenly</item>
</string-array>
<string-array translatable="false" name="array_align_items">
......
......@@ -716,6 +716,34 @@ class FlexboxAndroidTest {
assertThat(flexboxLayout.right - textView3.right, isEqualAllowingError(space))
}
@Test
@FlakyTest
@Throws(Throwable::class)
fun testJustifyContent_spaceEvenly() {
val activity = activityRule.activity
val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test,
object : Configuration {
override fun apply(flexboxLayout: FlexboxLayout) {
flexboxLayout.justifyContent = JustifyContent.SPACE_EVENLY
}
})
assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_EVENLY))
onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout)))
onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout)))
onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout)))
val textView1 = activity.findViewById<TextView>(R.id.text1)
val textView2 = activity.findViewById<TextView>(R.id.text2)
val textView3 = activity.findViewById<TextView>(R.id.text3)
var space = flexboxLayout.width - textView1.width - textView2.width - textView3.width
space /= 4 // Divide by the number of children + 1
assertThat(textView1.left, isEqualAllowingError(space))
assertThat(textView2.left - textView1.right, isEqualAllowingError(space))
assertThat(textView3.left - textView2.right, isEqualAllowingError(space))
assertThat(flexboxLayout.right - textView3.right, isEqualAllowingError(space))
}
@Test
@FlakyTest
@Throws(Throwable::class)
......@@ -746,6 +774,35 @@ class FlexboxAndroidTest {
assertThat(flexboxLayout.right - textView3.right - padding, isEqualAllowingError(space))
}
@Test
@FlakyTest
@Throws(Throwable::class)
fun testJustifyContent_spaceEvenly_withPadding() {
val activity = activityRule.activity
val padding = 40
val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test,
object : Configuration {
override fun apply(flexboxLayout: FlexboxLayout) {
flexboxLayout.justifyContent = JustifyContent.SPACE_EVENLY
flexboxLayout.setPadding(padding, padding, padding, padding)
}
})
assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_EVENLY))
val textView1 = activity.findViewById<TextView>(R.id.text1)
val textView2 = activity.findViewById<TextView>(R.id.text2)
val textView3 = activity.findViewById<TextView>(R.id.text3)
var space = flexboxLayout.width - textView1.width - textView2.width - textView3.width -
padding * 2
space /= 4 // Divide by the number of children + 1
assertThat(textView1.left - padding, isEqualAllowingError(space))
assertThat(textView2.left - textView1.right, isEqualAllowingError(space))
assertThat(textView3.left - textView2.right, isEqualAllowingError(space))
assertThat(flexboxLayout.right - textView3.right - padding, isEqualAllowingError(space))
}
@Test
@FlakyTest
@Throws(Throwable::class)
......@@ -907,6 +964,36 @@ class FlexboxAndroidTest {
assertThat(flexboxLayout.bottom - textView3.bottom, isEqualAllowingError(space))
}
@Test
@FlakyTest
@Throws(Throwable::class)
fun testJustifyContent_spaceEvenly_flexDirection_column() {
val activity = activityRule.activity
val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test,
object : Configuration {
override fun apply(flexboxLayout: FlexboxLayout) {
flexboxLayout.justifyContent = JustifyContent.SPACE_EVENLY
flexboxLayout.flexDirection = FlexDirection.COLUMN
}
})
assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_EVENLY))
assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN))
onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout)))
onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout)))
onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout)))
val textView1 = activity.findViewById<TextView>(R.id.text1)
val textView2 = activity.findViewById<TextView>(R.id.text2)
val textView3 = activity.findViewById<TextView>(R.id.text3)
var space = flexboxLayout.height - textView1.height - textView2.height - textView3.height
space /= 4 // Divide by the number of children + 1
assertThat(textView1.top, isEqualAllowingError(space))
assertThat(textView2.top - textView1.bottom, isEqualAllowingError(space))
assertThat(textView3.top - textView2.bottom, isEqualAllowingError(space))
assertThat(flexboxLayout.bottom - textView3.bottom, isEqualAllowingError(space))
}
@Test
@FlakyTest
@Throws(Throwable::class)
......@@ -938,6 +1025,37 @@ class FlexboxAndroidTest {
assertThat(flexboxLayout.bottom - textView3.bottom - padding, isEqualAllowingError(space))
}
@Test
@FlakyTest
@Throws(Throwable::class)
fun testJustifyContent_spaceEvenly_flexDirection_column_withPadding() {
val activity = activityRule.activity
val padding = 40
val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test,
object : Configuration {
override fun apply(flexboxLayout: FlexboxLayout) {
flexboxLayout.justifyContent = JustifyContent.SPACE_EVENLY
flexboxLayout.flexDirection = FlexDirection.COLUMN
flexboxLayout.setPadding(padding, padding, padding, padding)
}
})
assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_EVENLY))
assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN))
val textView1 = activity.findViewById<TextView>(R.id.text1)
val textView2 = activity.findViewById<TextView>(R.id.text2)
val textView3 = activity.findViewById<TextView>(R.id.text3)
var space = flexboxLayout.height - textView1.height - textView2.height - textView3.height -
padding * 2
space /= 4 // Divide by the number of children + 1
assertThat(textView1.top - padding, isEqualAllowingError(space))
assertThat(textView2.top - textView1.bottom, isEqualAllowingError(space))
assertThat(textView3.top - textView2.bottom, isEqualAllowingError(space))
assertThat(flexboxLayout.bottom - textView3.bottom - padding,
isEqualAllowingError(space))
}
@Test
@FlakyTest
@Throws(Throwable::class)
......
......@@ -479,6 +479,41 @@ class FlexboxLayoutManagerTest {
assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(292)))
}
@Test
@FlakyTest
@Throws(Throwable::class)
fun testJustifyContent_spaceEvenly_direction_row() {
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)
recyclerView.layoutManager = layoutManager
recyclerView.adapter = adapter
val lp1 = createLayoutParams(activity, 50, 100)
adapter.addItem(lp1)
val lp2 = createLayoutParams(activity, 50, 100)
adapter.addItem(lp2)
val lp3 = createLayoutParams(activity, 50, 100)
adapter.addItem(lp3)
layoutManager.justifyContent = JustifyContent.SPACE_EVENLY
// RecyclerView width: 320, height: 240.
}
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_EVENLY))
assertThat(layoutManager.flexItemCount, `is`(3))
assertThat(layoutManager.flexLines.size, `is`(1))
assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(43)))
assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(93)))
assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(135)))
assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(185)))
assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(228)))
assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(278)))
}
@Test
@FlakyTest
@Throws(Throwable::class)
......@@ -657,6 +692,42 @@ class FlexboxLayoutManagerTest {
assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(78)))
}
@Test
@FlakyTest
@Throws(Throwable::class)
fun testJustifyContent_spaceEvenly_direction_rowReverse() {
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)
recyclerView.layoutManager = layoutManager
recyclerView.adapter = adapter
val lp1 = createLayoutParams(activity, 50, 100)
adapter.addItem(lp1)
val lp2 = createLayoutParams(activity, 50, 100)
adapter.addItem(lp2)
val lp3 = createLayoutParams(activity, 50, 100)
adapter.addItem(lp3)
layoutManager.flexDirection = FlexDirection.ROW_REVERSE
layoutManager.justifyContent = JustifyContent.SPACE_EVENLY
// RecyclerView width: 320, height: 240.
}
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW_REVERSE))
assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_EVENLY))
assertThat(layoutManager.flexItemCount, `is`(3))
assertThat(layoutManager.flexLines.size, `is`(1))
assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(228)))
assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(278)))
assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(135)))
assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(185)))
assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(43)))
assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(93)))
}
@Test
@FlakyTest
@Throws(Throwable::class)
......@@ -837,6 +908,42 @@ class FlexboxLayoutManagerTest {
assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(225)))
}
@Test
@FlakyTest
@Throws(Throwable::class)
fun testJustifyContent_spaceEvenly_direction_column() {
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)
recyclerView.layoutManager = layoutManager
recyclerView.adapter = adapter
val lp1 = createLayoutParams(activity, 100, 50)
adapter.addItem(lp1)
val lp2 = createLayoutParams(activity, 100, 50)
adapter.addItem(lp2)
val lp3 = createLayoutParams(activity, 100, 50)
adapter.addItem(lp3)
layoutManager.flexDirection = FlexDirection.COLUMN
layoutManager.justifyContent = JustifyContent.SPACE_EVENLY
// RecyclerView width: 320, height: 240.
}
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN))
assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_EVENLY))
assertThat(layoutManager.flexItemCount, `is`(3))
assertThat(layoutManager.flexLines.size, `is`(1))
assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(23)))
assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(73)))
assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(95)))
assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(145)))
assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(167)))
assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(217)))
}
@Test
@FlakyTest
@Throws(Throwable::class)
......@@ -1017,6 +1124,42 @@ class FlexboxLayoutManagerTest {
assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(65)))
}
@Test
@FlakyTest
@Throws(Throwable::class)
fun testJustifyContent_spaceEvenly_direction_columnReverse() {
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)
recyclerView.layoutManager = layoutManager
recyclerView.adapter = adapter
val lp1 = createLayoutParams(activity, 100, 50)
adapter.addItem(lp1)
val lp2 = createLayoutParams(activity, 100, 50)
adapter.addItem(lp2)
val lp3 = createLayoutParams(activity, 100, 50)
adapter.addItem(lp3)
layoutManager.flexDirection = FlexDirection.COLUMN_REVERSE
layoutManager.justifyContent = JustifyContent.SPACE_EVENLY
// RecyclerView width: 320, height: 240.
}
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN_REVERSE))
assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_EVENLY))
assertThat(layoutManager.flexItemCount, `is`(3))
assertThat(layoutManager.flexLines.size, `is`(1))
assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(167)))
assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(217)))
assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(95)))
assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(145)))
assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(23)))
assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(73)))
}
@Test
@FlakyTest
@Throws(Throwable::class)
......
......@@ -641,7 +641,7 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
childLeft = paddingLeft + (width - flexLine.mMainSize) / 2f;
childRight = width - paddingRight - (width - flexLine.mMainSize) / 2f;
break;
case JustifyContent.SPACE_AROUND:
case JustifyContent.SPACE_AROUND: {
int visibleCount = flexLine.getItemCountNotGone();
if (visibleCount != 0) {
spaceBetweenItem = (width - flexLine.mMainSize)
......@@ -650,13 +650,25 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
childLeft = paddingLeft + spaceBetweenItem / 2f;
childRight = width - paddingRight - spaceBetweenItem / 2f;
break;
case JustifyContent.SPACE_BETWEEN:
}
case JustifyContent.SPACE_BETWEEN: {
childLeft = paddingLeft;
int visibleItem = flexLine.getItemCountNotGone();
float denominator = visibleItem != 1 ? visibleItem - 1 : 1f;
int visibleCount = flexLine.getItemCountNotGone();
float denominator = visibleCount != 1 ? visibleCount - 1 : 1f;
spaceBetweenItem = (width - flexLine.mMainSize) / denominator;
childRight = width - paddingRight;
break;
}
case JustifyContent.SPACE_EVENLY: {
int visibleCount = flexLine.getItemCountNotGone();
if (visibleCount != 0) {
spaceBetweenItem = (width - flexLine.mMainSize)
/ (float) (visibleCount + 1);
}
childLeft = paddingLeft + spaceBetweenItem;
childRight = width - paddingRight - spaceBetweenItem;
break;
}
default:
throw new IllegalStateException(
"Invalid justifyContent is set: " + mJustifyContent);
......@@ -786,7 +798,7 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
childTop = paddingTop + (height - flexLine.mMainSize) / 2f;
childBottom = height - paddingBottom - (height - flexLine.mMainSize) / 2f;
break;
case JustifyContent.SPACE_AROUND:
case JustifyContent.SPACE_AROUND: {
int visibleCount = flexLine.getItemCountNotGone();
if (visibleCount != 0) {
spaceBetweenItem = (height - flexLine.mMainSize)
......@@ -795,13 +807,25 @@ public class FlexboxLayout extends ViewGroup implements FlexContainer {
childTop = paddingTop + spaceBetweenItem / 2f;
childBottom = height - paddingBottom - spaceBetweenItem / 2f;
break;
case JustifyContent.SPACE_BETWEEN:
}
case JustifyContent.SPACE_BETWEEN: {
childTop = paddingTop;
int visibleItem = flexLine.getItemCountNotGone();
float denominator = visibleItem != 1 ? visibleItem - 1 : 1f;
int visibleCount = flexLine.getItemCountNotGone();
float denominator = visibleCount != 1 ? visibleCount - 1 : 1f;
spaceBetweenItem = (height - flexLine.mMainSize) / denominator;
childBottom = height - paddingBottom;
break;
}
case JustifyContent.SPACE_EVENLY: {
int visibleCount = flexLine.getItemCountNotGone();
if (visibleCount != 0) {
spaceBetweenItem = (height - flexLine.mMainSize)
/ (float) (visibleCount + 1);
}
childTop = paddingTop + spaceBetweenItem;
childBottom = height - paddingBottom - spaceBetweenItem;
break;
}
default:
throw new IllegalStateException(
"Invalid justifyContent is set: " + mJustifyContent);
......
......@@ -1469,6 +1469,14 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
spaceBetweenItem = (parentWidth - flexLine.mMainSize) / denominator;
childRight = parentWidth - paddingRight;
break;
case JustifyContent.SPACE_EVENLY:
if (flexLine.mItemCount != 0) {
spaceBetweenItem = (parentWidth - flexLine.mMainSize)
/ (float) (flexLine.mItemCount + 1);
}
childLeft = paddingLeft + spaceBetweenItem;
childRight = parentWidth - paddingRight - spaceBetweenItem;
break;
default:
throw new IllegalStateException(
"Invalid justifyContent is set: " + mJustifyContent);
......@@ -1580,6 +1588,14 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
spaceBetweenItem = (parentHeight - flexLine.mMainSize) / denominator;
childBottom = parentHeight - paddingBottom;
break;
case JustifyContent.SPACE_EVENLY:
if (flexLine.mItemCount != 0) {
spaceBetweenItem = (parentHeight - flexLine.mMainSize)
/ (float) (flexLine.mItemCount + 1);
}
childTop = paddingTop + spaceBetweenItem;
childBottom = parentHeight - paddingBottom - spaceBetweenItem;
break;
default:
throw new IllegalStateException(
"Invalid justifyContent is set: " + mJustifyContent);
......
......@@ -47,4 +47,14 @@ public @interface JustifyContent {
* the flex lines.
*/
int SPACE_AROUND = 4;
/**
* Flex items are evenly distributed along the flex line. The difference between
* {@link #SPACE_AROUND} is that all the spaces between items should be the same as the
* space before the first item and after the last item.
* See
* <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">the document on MDN</a>
* for more details.
*/
int SPACE_EVENLY = 5;
}
......@@ -40,6 +40,7 @@ limitations under the License.
<enum name="center" value="2" />
<enum name="space_between" value="3" />
<enum name="space_around" value="4" />
<enum name="space_evenly" value="5" />
</attr>
<attr name="alignItems">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册