title: Viewpager + Configuration Change = off center Current Item
body: <p>I have an app whose main navigation feature is a ViewPager with essentially infinite pages. I'm using something similar to <a href="https://github.com/antonyt/InfiniteViewPager" rel="nofollow noreferrer">InfiniteViewPager</a> (with a <code>FragmentStatePagerAdapter</code>), starting navigation in the middle of the ViewPager so there are pages to the left and right to start. I'm also using negative margins <code>mPager.setPageMargin(-50);</code> to have the pages on either side slightly visible.</p>
  <p>I'm loading lots of data from the web, so I'm handling configuration changes myself by using <code>android:configChanges=&quot;orientation|keyboardHidden&quot;</code> and the <code>onConfigurationChanged</code> override in my activity. Mainly, I don't want to recreate my Activity because I think it would take much too long when changing orientation.</p>
  <h2>Problem</h2>
  <p>I open the app, swipe in either direction, wait for the ViewPager to stop animating, then change orientation. The current page is now slightly off center and one neighboring page is more exposed than the other. Sometimes one of the neighboring pages is completely offscreen.</p>
  <p>If I touch the page, the ViewPager scroll to the current page and displays as expected, but unless I do that, the display is wrong. I get this problem going from Landscape to Portrait and Portrait to Landscape. I've tried setting a <code>OnGlobalLayoutListener</code> and performing <code>mViewPager.setCurrentItem(mViewPager.getCurrentItem())</code> but <code>ViewPager</code> ignores <code>setCurrentItem</code> if you ask it to set the current item to what is actually the current item.</p>
  <h2>Code</h2>
  <p>MainActivity</p>
  <pre><code>@Override
  public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      //whatever I do in here doesn't make a difference.
      //my viewpager still displays off center, even if I do nothing custom
  }
  </code></pre>
  <p>Pager Adapter</p>
  <pre><code>private class MyAdapter extends FragmentStatePagerAdapter{
  
      public CarouselPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          PageData dataForThisPage = mModel.getDataForPage(position);
          PageContainerFragment f = PageContainerFragment.create(position, dataForThisPage);
  
          return f;
      }
  
      @Override
      public int getCount() {
          return 2000;
      }
  }
  </code></pre>
  <p>I've been trying to figure this out for hours and I can't get anywhere. Any help would be greatly appreciated!</p>
  <p>Edit: styling</p>