Skip to content

Commit a1295e1

Browse files
ruiaraujofacebook-github-bot
authored andcommitted
Fix Viewpager on Android when using native navigation.
Summary: See the "broken" video attached to really understand the problem easily. On Android after navigating to any other screen using wix navigation library, the native viewpager would lose the settling page behaviour which is quite annoying for the users. This is caused by the onAttachedToWindow that resets mFirstLayout to true inside ViewPager. By request another layout pass, everything works as expected. Working video is the application with patched RN. [broken.mp4](https://github.com/facebook/react-native/files/1128028/broken.mp4.zip) [working.mp4](https://github.com/facebook/react-native/files/1128032/working.mp4.zip) Closes #14867 Differential Revision: D7154981 Pulled By: hramos fbshipit-source-id: 2b3570800a5320ed2c12c488748d9e1358936c84
1 parent 498cf7e commit a1295e1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java

+20
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,26 @@ public void setScrollEnabled(boolean scrollEnabled) {
210210
mScrollEnabled = scrollEnabled;
211211
}
212212

213+
214+
@Override
215+
protected void onAttachedToWindow() {
216+
super.onAttachedToWindow();
217+
// The viewpager reset an internal flag on this method so we need to run another layout pass
218+
// after attaching to window.
219+
this.requestLayout();
220+
post(measureAndLayout);
221+
}
222+
223+
private final Runnable measureAndLayout = new Runnable() {
224+
@Override
225+
public void run() {
226+
measure(
227+
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
228+
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
229+
layout(getLeft(), getTop(), getRight(), getBottom());
230+
}
231+
};
232+
213233
/*package*/ void addViewToAdapter(View child, int index) {
214234
getAdapter().addView(child, index);
215235
}

0 commit comments

Comments
 (0)