Skip to content

Commit

Permalink
Fix screen container layout on Android. (#217)
Browse files Browse the repository at this point in the history
This is a similar fix to the one already merged in #215 but for Android this time. We change default screen container to layout its direct children without using flexbox. This is to follow the same pattern as with native stack container.
  • Loading branch information
kmagiera committed Nov 6, 2019
1 parent 26384b6 commit a017713
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
38 changes: 35 additions & 3 deletions android/src/main/java/com/swmansion/rnscreens/ScreenContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,45 @@ public class ScreenContainer<T extends ScreenFragment> extends ViewGroup {
private @Nullable FragmentTransaction mCurrentTransaction;
private boolean mNeedUpdate;
private boolean mIsAttached;
private boolean mLayoutEnqueued = false;

private ChoreographerCompat.FrameCallback mFrameCallback = new ChoreographerCompat.FrameCallback() {
private final ChoreographerCompat.FrameCallback mFrameCallback = new ChoreographerCompat.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
updateIfNeeded();
}
};

private final Runnable mLayoutRunnable = new Runnable() {
@Override
public void run() {
mLayoutEnqueued = false;
measure(
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
layout(getLeft(), getTop(), getRight(), getBottom());
}
};

public ScreenContainer(Context context) {
super(context);
}

@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
// no-op
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for (int i = 0, size = getChildCount(); i < size; i++) {
getChildAt(i).layout(0, 0, getWidth(), getHeight());
}
}

@Override
public void requestLayout() {
super.requestLayout();

if (!mLayoutEnqueued) {
mLayoutEnqueued = true;
post(mLayoutRunnable);
}
}

protected void markUpdated() {
Expand Down Expand Up @@ -162,6 +186,14 @@ protected void onDetachedFromWindow() {
mIsAttached = false;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
for (int i = 0, size = getChildCount(); i < size; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
}

private void updateIfNeeded() {
if (!mNeedUpdate || !mIsAttached) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public int getChildCount(ScreenContainer parent) {
public View getChildAt(ScreenContainer parent, int index) {
return parent.getScreenAt(index);
}

@Override
public boolean needsCustomLayoutForChildren() {
return true;
}
}
37 changes: 0 additions & 37 deletions android/src/main/java/com/swmansion/rnscreens/ScreenStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class ScreenStack extends ScreenContainer<ScreenStackFragment> {
private final Set<ScreenStackFragment> mDismissed = new HashSet<>();

private ScreenStackFragment mTopScreen = null;
private boolean mLayoutEnqueued = false;

private final FragmentManager.OnBackStackChangedListener mBackStackListener = new FragmentManager.OnBackStackChangedListener() {
@Override
Expand Down Expand Up @@ -59,48 +58,12 @@ protected ScreenStackFragment adapt(Screen screen) {
return new ScreenStackFragment(screen);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for (int i = 0, size = getChildCount(); i < size; i++) {
getChildAt(i).layout(0, 0, getWidth(), getHeight());
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
for (int i = 0, size = getChildCount(); i < size; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
getFragmentManager().removeOnBackStackChangedListener(mBackStackListener);
}

private final Runnable mLayoutRunnable = new Runnable() {
@Override
public void run() {
mLayoutEnqueued = false;
measure(
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
layout(getLeft(), getTop(), getRight(), getBottom());
}
};

@Override
public void requestLayout() {
super.requestLayout();

if (!mLayoutEnqueued) {
mLayoutEnqueued = true;
post(mLayoutRunnable);
}
}

@Override
protected void removeScreenAt(int index) {
Screen toBeRemoved = getScreenAt(index);
Expand Down

0 comments on commit a017713

Please sign in to comment.