Skip to content

Commit

Permalink
Fixes the case where the first view's visibility is gone and the seco…
Browse files Browse the repository at this point in the history
…nd view is in the second line. (#300)

In that case, the position of the second view is misplaced.

Fixes #283
  • Loading branch information
thagikura committed Jun 28, 2017
1 parent 3a9bb66 commit aece313
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3895,6 +3895,36 @@ public void testChildNeedsRemeasure_column() throws Throwable {
onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout)));
}

@Test
@FlakyTest
public void testFirstViewGone_firstLineSingleItem_row() throws Throwable {
// This test verifies the case where the first view's visibility is gone and the second
// view is in the next flex line. In that case, the second view's position is misplaced.
// https://github.com/google/flexbox-layout/issues/283
createFlexboxLayout(R.layout.activity_first_view_gone_first_line_single_item);
onView(withId(R.id.text2)).check(isTopAlignedWith(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)));
}

@Test
@FlakyTest
public void testFirstViewGone_firstLineSingleItem_column() throws Throwable {
// This test verifies the case where the first view's visibility is gone and the second
// view is in the next flex line. In that case, the second view's position is misplaced.
// https://github.com/google/flexbox-layout/issues/283
createFlexboxLayout(R.layout.activity_first_view_gone_first_line_single_item,
new Configuration() {
@Override
public void apply(FlexboxLayout flexboxLayout) {
flexboxLayout.setFlexDirection(FlexDirection.COLUMN);
}
});
onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout)));
onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout)));
onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout)));
}

private FlexboxLayout createFlexboxLayout(@LayoutRes final int activityLayoutResId)
throws Throwable {
return createFlexboxLayout(activityLayoutResId, Configuration.EMPTY);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!--
This layout verifies the case where the first view's visibility is gone and the second view is
in the next flex line by setting the layout_wrapBefore="true"
-->
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/flexbox_layout"
android:layout_width="320dp"
android:layout_height="320dp"
app:flexDirection="row"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch">

<TextView
android:id="@+id/text1"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="1"
android:visibility="gone" />

<TextView
android:id="@+id/text2"
android:layout_width="120dp"
android:layout_height="60dp"
android:text="2"
app:layout_wrapBefore="true"
/>

<TextView
android:id="@+id/text3"
android:layout_width="100dp"
android:layout_height="60dp"
android:text="3"
app:layout_wrapBefore="true"
/>
</com.google.android.flexbox.FlexboxLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ private void layoutHorizontal(boolean isRtl, int left, int top, int right, int b
// Use float to reduce the round error that may happen in when justifyContent ==
// SPACE_BETWEEN or SPACE_AROUND
float childLeft;
int currentViewIndex = 0;

int height = bottom - top;
int width = right - left;
Expand Down Expand Up @@ -656,19 +655,17 @@ private void layoutHorizontal(boolean isRtl, int left, int top, int right, int b
spaceBetweenItem = Math.max(spaceBetweenItem, 0);

for (int j = 0; j < flexLine.mItemCount; j++) {
View child = getReorderedChildAt(currentViewIndex);
if (child == null) {
continue;
} else if (child.getVisibility() == View.GONE) {
currentViewIndex++;
int index = flexLine.mFirstIndex + j;
View child = getReorderedChildAt(index);
if (child == null || child.getVisibility() == View.GONE) {
continue;
}
LayoutParams lp = ((LayoutParams) child.getLayoutParams());
childLeft += lp.leftMargin;
childRight -= lp.rightMargin;
int beforeDividerLength = 0;
int endDividerLength = 0;
if (hasDividerBeforeChildAtAlongMainAxis(currentViewIndex, j)) {
if (hasDividerBeforeChildAtAlongMainAxis(index, j)) {
beforeDividerLength = mDividerVerticalWidth;
childLeft += beforeDividerLength;
childRight -= beforeDividerLength;
Expand Down Expand Up @@ -703,7 +700,6 @@ private void layoutHorizontal(boolean isRtl, int left, int top, int right, int b
}
childLeft += child.getMeasuredWidth() + spaceBetweenItem + lp.rightMargin;
childRight -= child.getMeasuredWidth() + spaceBetweenItem + lp.leftMargin;
currentViewIndex++;

if (isRtl) {
flexLine.updatePositionFromView(child, /*leftDecoration*/endDividerLength, 0,
Expand Down Expand Up @@ -747,7 +743,6 @@ private void layoutVertical(boolean isRtl, boolean fromBottomToTop, int left, in

int paddingRight = getPaddingRight();
int childLeft = getPaddingLeft();
int currentViewIndex = 0;

int width = right - left;
int height = bottom - top;
Expand Down Expand Up @@ -805,19 +800,17 @@ private void layoutVertical(boolean isRtl, boolean fromBottomToTop, int left, in
spaceBetweenItem = Math.max(spaceBetweenItem, 0);

for (int j = 0; j < flexLine.mItemCount; j++) {
View child = getReorderedChildAt(currentViewIndex);
if (child == null) {
continue;
} else if (child.getVisibility() == View.GONE) {
currentViewIndex++;
int index = flexLine.mFirstIndex + j;
View child = getReorderedChildAt(index);
if (child == null || child.getVisibility() == View.GONE) {
continue;
}
LayoutParams lp = ((LayoutParams) child.getLayoutParams());
childTop += lp.topMargin;
childBottom -= lp.bottomMargin;
int beforeDividerLength = 0;
int endDividerLength = 0;
if (hasDividerBeforeChildAtAlongMainAxis(currentViewIndex, j)) {
if (hasDividerBeforeChildAtAlongMainAxis(index, j)) {
beforeDividerLength = mDividerHorizontalHeight;
childTop += beforeDividerLength;
childBottom -= beforeDividerLength;
Expand Down Expand Up @@ -851,7 +844,6 @@ private void layoutVertical(boolean isRtl, boolean fromBottomToTop, int left, in
}
childTop += child.getMeasuredHeight() + spaceBetweenItem + lp.bottomMargin;
childBottom -= child.getMeasuredHeight() + spaceBetweenItem + lp.topMargin;
currentViewIndex++;

if (fromBottomToTop) {
flexLine.updatePositionFromView(child, 0, /*topDecoration*/endDividerLength, 0,
Expand Down

0 comments on commit aece313

Please sign in to comment.