title: ViewPager + Fragments + Change page with button
body: <p>I'm working with a ViewPager and change pages with a button. I've got it to work but it has inconsistent performance. Sometimes it just wont react. You can still scroll with touch but the button doesn't work. I have a ViewPager with a few fragments. This is the ViewPager code in FragmentActivity:</p>
  
  <pre><code>private void initializeViewPager() {
      mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
      mViewPager = (ViewPager) findViewById(R.id.pager);
      mViewPager.setAdapter(mSectionsPagerAdapter);
      mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
  
          ActionBar actionBar = getActionBar();
  
              @Override
              public void onPageSelected(int position) {
                  currentPage = position;
              }
  
              @Override
              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  
              }
  
              @Override
              public void onPageScrollStateChanged(int state) {
                  Log.d(TAG, "onPageScrollStateChanged: " + state);
  
              }
  
          });
  }
  </code></pre>
  
  <p>This is the code in one of the Fragments:</p>
  
  <p><strong>// Referring to the ViewPager from FragmentActivity</strong></p>
  
  <pre><code>static ViewPager mViewPager = MainActivity.mViewPager;
  </code></pre>
  
  <p><strong>// Register onClick and setPagerItem</strong> </p>
  
  <pre><code>ImageView next = (ImageView)v.findViewById(R.id.ic_menu_next);
      next.requestFocus();
      next.setOnClickListener(new OnClickListener() {
       @Override
             public void onClick(View view) {
                 mViewPager.setCurrentItem(getItem(+1), true);
             }
  
          });
  </code></pre>
  
  <p><strong>// This figures out where in the ViewPager you are</strong> </p>
  
  <pre><code>private static int getItem(int i) {
      int a = mViewPager.getCurrentItem();
      i += a;
      return i;
  }
  </code></pre>
  
  <p>Any suggestions?</p>