title: Synchronizing two ViewPagers using OnPageChangeListener
body: <p>I'm trying to synchronize two <code>ViewPager</code>s, as apparently have quite a lot of people before me, and I've got as far as this:</p>
  
  <pre><code>private ViewPager mNavPager;
  
  private ViewPager mMainPager;
  
  private final OnPageChangeListener mNavPagerListener = new OnPageChangeListener() {
  
      private boolean mNavDragging;
      private int mScrollPosition;
  
      @Override
      public void onPageSelected(int position) {
          mScrollPosition = position;
      }
  
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
          if(mNavDragging)
              mMainPager.scrollTo(positionOffsetPixels, 0);
      }
  
      @Override
      public void onPageScrollStateChanged(int state) {
          switch(state) {
          case ViewPager.SCROLL_STATE_DRAGGING:
          case ViewPager.SCROLL_STATE_SETTLING:
              mNavDragging = true;
              break;
          case ViewPager.SCROLL_STATE_IDLE:
              mNavDragging = false;
              break;
          }
      }
  };
  
  private OnPageChangeListener mMainPagerListener = new OnPageChangeListener() {
  
      private boolean mMainDragging;
      private int mScrollPosition;
  
      @Override
      public void onPageSelected(int position) {
          mScrollPosition = position;
      }
  
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
          if(mMainDragging)
              mNavPager.scrollTo(positionOffsetPixels, 0);
      }
  
      @Override
      public void onPageScrollStateChanged(int state) {
          switch(state) {
          case ViewPager.SCROLL_STATE_DRAGGING:
          case ViewPager.SCROLL_STATE_SETTLING:
              mMainDragging = true;
              break;
          case ViewPager.SCROLL_STATE_IDLE:
              mMainDragging = false;
              break;
          }
      }
  };
  </code></pre>
  
  <p>If either one is scrolled manually, the other is slaved to it using the scroll state property.  It works beautifully till the items reach their final position; at this point, the slaved pager flicks instantly back to the previously selected item, as though the scrolling hadn't taken place.</p>
  
  <p>I have tried calling <code>ViewPager#setCurrentItem(mScrolledPosition)</code> with a variety of different logic constraints but that doesn't work either, though it does occasionally make it worse.  I feel as though there must be something that can be done with that but I'm not sure what.</p>
  
  <p>How can I get the slaved pager to remain in the correct position?</p>