提交 0e32968f 编写于 作者: T Takeshi Hagikura 提交者: Takeshi Hagikura

Add support for stretch views. (#161)

For the case where:
- align items attribute is set to stretch
OR
- align self attribute for individual flex item is set to stretch
上级 b6e4b441
......@@ -411,4 +411,25 @@ public class FlexboxHelperTest {
assertThat(view3.getMeasuredWidth(), isEqualAllowingError(333));
assertThat(view4.getMeasuredWidth(), isEqualAllowingError(333));
}
@Test
public void testMakeCombinedLong() {
int higher = -1;
int lower = 10;
long combined = mFlexboxHelper.makeCombinedLong(lower, higher);
assertThat(mFlexboxHelper.extractHigherInt(combined), is(higher));
assertThat(mFlexboxHelper.extractLowerInt(combined), is(lower));
higher = Integer.MAX_VALUE;
lower = Integer.MIN_VALUE;
combined = mFlexboxHelper.makeCombinedLong(lower, higher);
assertThat(mFlexboxHelper.extractHigherInt(combined), is(higher));
assertThat(mFlexboxHelper.extractLowerInt(combined), is(lower));
higher = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY);
lower = View.MeasureSpec.makeMeasureSpec(300, View.MeasureSpec.UNSPECIFIED);
combined = mFlexboxHelper.makeCombinedLong(lower, higher);
assertThat(mFlexboxHelper.extractHigherInt(combined), is(higher));
assertThat(mFlexboxHelper.extractLowerInt(combined), is(lower));
}
}
......@@ -18,6 +18,7 @@ package com.google.android.flexbox.test;
import com.google.android.flexbox.AlignContent;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.AlignSelf;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
......@@ -1421,6 +1422,164 @@ public class FlexboxLayoutManagerTest {
assertThat(layoutManager.getChildCount(), is(not(200)));
}
@Test
@FlakyTest
public void testAlignItems_stretch_direction_row() throws Throwable {
final FlexboxTestActivity activity = mActivityRule.getActivity();
final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();
final TestAdapter adapter = new TestAdapter();
mActivityRule.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.setContentView(R.layout.recyclerview);
RecyclerView recyclerView = (RecyclerView) activity.findViewById(R.id.recyclerview);
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setAlignItems(AlignItems.STRETCH);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 70, 80);
adapter.addItem(lp1);
FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 70, 50);
adapter.addItem(lp2);
FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 70, 30);
adapter.addItem(lp3);
// RecyclerView width: 320, height: 240.
}
});
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW));
assertThat(layoutManager.getAlignItems(), is(AlignItems.STRETCH));
assertThat(layoutManager.getFlexItemCount(), is(3));
assertThat(layoutManager.getFlexLines().size(), is(1));
// Verify all items heights are stretched
assertThat(layoutManager.getChildAt(0).getHeight(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 80)));
assertThat(layoutManager.getChildAt(1).getHeight(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 80)));
assertThat(layoutManager.getChildAt(2).getHeight(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 80)));
}
@Test
@FlakyTest
public void testAlignItems_stretch_direction_column() throws Throwable {
final FlexboxTestActivity activity = mActivityRule.getActivity();
final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();
final TestAdapter adapter = new TestAdapter();
mActivityRule.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.setContentView(R.layout.recyclerview);
RecyclerView recyclerView = (RecyclerView) activity.findViewById(R.id.recyclerview);
layoutManager.setFlexDirection(FlexDirection.COLUMN);
layoutManager.setAlignItems(AlignItems.STRETCH);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 80, 70);
adapter.addItem(lp1);
FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 70);
adapter.addItem(lp2);
FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 30, 70);
adapter.addItem(lp3);
// RecyclerView width: 320, height: 240.
}
});
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN));
assertThat(layoutManager.getAlignItems(), is(AlignItems.STRETCH));
assertThat(layoutManager.getFlexItemCount(), is(3));
assertThat(layoutManager.getFlexLines().size(), is(1));
// Verify all items widths are stretched
assertThat(layoutManager.getChildAt(0).getWidth(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 80)));
assertThat(layoutManager.getChildAt(1).getWidth(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 80)));
assertThat(layoutManager.getChildAt(2).getWidth(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 80)));
}
@Test
@FlakyTest
public void testAlignSelf_stretch_direction_row() throws Throwable {
final FlexboxTestActivity activity = mActivityRule.getActivity();
final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();
final TestAdapter adapter = new TestAdapter();
mActivityRule.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.setContentView(R.layout.recyclerview);
RecyclerView recyclerView = (RecyclerView) activity.findViewById(R.id.recyclerview);
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setAlignItems(AlignItems.FLEX_START);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 70, 80);
adapter.addItem(lp1);
FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 70, 50);
adapter.addItem(lp2);
FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 70, 30);
lp3.setAlignSelf(AlignSelf.STRETCH);
adapter.addItem(lp3);
// RecyclerView width: 320, height: 240.
}
});
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW));
assertThat(layoutManager.getAlignItems(), is(AlignItems.FLEX_START));
assertThat(layoutManager.getFlexItemCount(), is(3));
assertThat(layoutManager.getFlexLines().size(), is(1));
// Verify the item whose align self is set to stretch is stretched
assertThat(layoutManager.getChildAt(0).getHeight(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 80)));
assertThat(layoutManager.getChildAt(1).getHeight(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 50)));
assertThat(layoutManager.getChildAt(2).getHeight(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 80)));
}
@Test
@FlakyTest
public void testAlignSelf_stretch_direction_column() throws Throwable {
final FlexboxTestActivity activity = mActivityRule.getActivity();
final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();
final TestAdapter adapter = new TestAdapter();
mActivityRule.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.setContentView(R.layout.recyclerview);
RecyclerView recyclerView = (RecyclerView) activity.findViewById(R.id.recyclerview);
layoutManager.setFlexDirection(FlexDirection.COLUMN);
layoutManager.setAlignItems(AlignItems.FLEX_START);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 80, 70);
adapter.addItem(lp1);
FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 70);
adapter.addItem(lp2);
FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 30, 70);
lp3.setAlignSelf(AlignSelf.STRETCH);
adapter.addItem(lp3);
// RecyclerView width: 320, height: 240.
}
});
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN));
assertThat(layoutManager.getAlignItems(), is(AlignItems.FLEX_START));
assertThat(layoutManager.getFlexItemCount(), is(3));
assertThat(layoutManager.getFlexLines().size(), is(1));
// Verify the item whose align self is set to stretch is stretched
assertThat(layoutManager.getChildAt(0).getWidth(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 80)));
assertThat(layoutManager.getChildAt(1).getWidth(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 50)));
assertThat(layoutManager.getChildAt(2).getWidth(),
isEqualAllowingError(TestUtil.dpToPixel(activity, 80)));
}
/**
* Creates a new flex item.
*
......
......@@ -37,4 +37,19 @@ public @interface AlignSelf {
* the {@link AlignItems} attribute from its parent.
*/
int AUTO = -1;
/** This item's edge is placed on the cross start line. */
int FLEX_START = AlignItems.FLEX_START;
/** This item's edge is placed on the cross end line. */
int FLEX_END = AlignItems.FLEX_END;
/** This item's edge is centered along the cross axis. */
int CENTER = AlignItems.CENTER;
/** This items is aligned based on their text's baselines. */
int BASELINE = AlignItems.BASELINE;
/** This item is stretched to fill the flex line's cross size. */
int STRETCH = AlignItems.STRETCH;
}
......@@ -454,6 +454,7 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
ensureOrientationHelper();
ensureLayoutState();
mFlexboxHelper.ensureMeasureSpecCache(childCount);
mFlexboxHelper.ensureMeasuredSizeCache(childCount);
mFlexboxHelper.ensureIndexToFlexLine(childCount);
mLayoutState.mShouldRecycle = false;
......@@ -884,8 +885,8 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
// retrieved from Recycler, in that case measured width/height are set to 0 even
// each visible child should be measured at least once in the FlexboxHelper
long measureSpec = mFlexboxHelper.mMeasureSpecCache[i];
int widthSpec = mFlexboxHelper.extractWidthMeasureSpec(measureSpec);
int heightSpec = mFlexboxHelper.extractHeightMeasureSpec(measureSpec);
int widthSpec = mFlexboxHelper.extractLowerInt(measureSpec);
int heightSpec = mFlexboxHelper.extractHigherInt(measureSpec);
LayoutParams lp = (LayoutParams) view.getLayoutParams();
if (shouldMeasureChild(view, widthSpec, heightSpec, lp)) {
// TODO: Need to consider decorator length
......@@ -1007,8 +1008,8 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
// retrieved from Recycler, in that case measured width/height are set to 0 even
// each visible child should be measured at least once in the FlexboxHelper
long measureSpec = mFlexboxHelper.mMeasureSpecCache[i];
int widthSpec = mFlexboxHelper.extractWidthMeasureSpec(measureSpec);
int heightSpec = mFlexboxHelper.extractHeightMeasureSpec(measureSpec);
int widthSpec = mFlexboxHelper.extractLowerInt(measureSpec);
int heightSpec = mFlexboxHelper.extractHigherInt(measureSpec);
LayoutParams lp = (LayoutParams) view.getLayoutParams();
if (shouldMeasureChild(view, widthSpec, heightSpec, lp)) {
// TODO: Need to consider decorator length
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册