From a1295e1707a856b9cd5c3129320d386aa9166310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rui=20Ara=C3=BAjo?= Date: Mon, 5 Mar 2018 10:40:55 -0800 Subject: [PATCH] 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 https://github.com/facebook/react-native/pull/14867 Differential Revision: D7154981 Pulled By: hramos fbshipit-source-id: 2b3570800a5320ed2c12c488748d9e1358936c84 --- .../react/views/viewpager/ReactViewPager.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java b/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java index 77e93269bdcf3f..48b5f1a6194956 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java @@ -210,6 +210,26 @@ public void setScrollEnabled(boolean scrollEnabled) { mScrollEnabled = scrollEnabled; } + + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + // The viewpager reset an internal flag on this method so we need to run another layout pass + // after attaching to window. + this.requestLayout(); + post(measureAndLayout); + } + + private final Runnable measureAndLayout = new Runnable() { + @Override + public void run() { + measure( + MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY)); + layout(getLeft(), getTop(), getRight(), getBottom()); + } + }; + /*package*/ void addViewToAdapter(View child, int index) { getAdapter().addView(child, index); }