title: ViewPager set current page programmatically
body: <p>I wrote a custom <code>ViewPager</code> to disable <code>Swipe Scroll</code>, but I want to swipe programmatically. I have three <code>Tab</code> in my view pager, but when I call <code>viewPager.setCurrentItem(viewPager.getCurrentItem()+1)</code> on the first <code>Fragment</code>, it moves to the third <code>Fragment</code> instead of the second <code>Fragment</code>. And if I call same function in the second <code>Fragment</code>, it goes to the third. If I call (viewPager.getCurrentItem()-1)` in the third fragment, it works fine by moving back. Any help would be appreciated. My code is below:</p>
  
  <p><strong>NonSwipeAbleViewPager</strong></p>
  
  <pre><code>public class NonSwipeableViewPager extends ViewPager {
  
  private boolean swipeable;
  
  public NonSwipeableViewPager(Context context, AttributeSet attrs) {
      super(context, attrs);
      TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyViewPager);
      try {
          swipeable = a.getBoolean(R.styleable.MyViewPager_swipeable, true);
      } finally {
          a.recycle();
      }
  }
  
  @Override
  public boolean onInterceptTouchEvent(MotionEvent event) {
      return swipeable ? super.onInterceptTouchEvent(event) : false;
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      return swipeable ? super.onTouchEvent(event) : false;
  }
  }
  </code></pre>
  
  <p><strong>Declaration in XML</strong></p>
  
  <pre><code>&lt;co.example.customview.NonSwipeableViewPager
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      app:swipeable="false" /&gt;
  </code></pre>
  
  <p><strong>Calling it</strong></p>
  
  <pre><code> @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.menuNext:
              NonSwipeableViewPager pages = (NonSwipeableViewPager) getActivity().findViewById(R.id.pager);
              pages.setCurrentItem(pages.getCurrentItem()+1, true);
              break;
          default:
              return super.onOptionsItemSelected(item);
      }
      return super.onOptionsItemSelected(item);
  }
  </code></pre>