title: Sliding between fragments
body: <p>I'm developing an app and currently I am trying to add <code>ScreenSlide</code> to it. I managed to do this using a <a href="http://developer.android.com/training/animation/screen-slide.html" rel="noreferrer">tutorial</a>, but usual sliding between <code>x</code> pages is not quite what I'm looking for. </p>
  
  <p>With code provided below I can slide between 5 pages, but pages aligned in a straight line and you can't go from first page straight to 5th and vise versa. 
  In my app I have 4 pages. When I slide left I switch between first 2 pages, when I slide right I switch between 2 last pages. On image below you can see how my current code switches page and under it - my goal. </p>
  
  <p><a href="https://i.stack.imgur.com/kioUI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/kioUI.png" alt="enter image description here"></a></p>
  
  <pre><code>    public class MainActivity extends FragmentActivity {
      private static final int NUM_PAGES = 5;
  
  
      private ViewPager mPager;
      private ScreenSlidePageFragment[] pages = new ScreenSlidePageFragment[NUM_PAGES];
  
  
      private PagerAdapter mPagerAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_screen_slide);
  
          mPager = (ViewPager) findViewById(R.id.pager);
          mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
          mPager.setAdapter(mPagerAdapter);
  
      }
  
      @Override
      public void onBackPressed() {
          if (mPager.getCurrentItem() == 0) {
              super.onBackPressed();
          } else {
              mPager.setCurrentItem(mPager.getCurrentItem() - 1);
          } 
      }
  
      private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
          public ScreenSlidePagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
  
  
          @Override
          public int getCount() {
              return NUM_PAGES;
          }
  
          @Override
          public Fragment getItem(int position) {
              boolean moveRight = mPager.getCurrentItem() &lt; position;
              boolean moveLeft = mPager.getCurrentItem() &gt; position;
              switch(position){
                  case 5: 
                      if(moveRight){
  
                          return geLog.w("i"
                              + "Info", Integer.toString(position));
                         //return getPageByPosition(2);
                      if(moveLeft)
                          return getPageByPosition(2);
  
  
              }
              return new ScreenSlidePageFragment();
          }
  
          private Fragment getPageByPosition(int position){
              int index = position - 1;
              if(index &lt; 0 || index &gt; NUM_PAGES-1)
                  throw new InvalidParameterException("requested position is invalid");
  
              if(pages[index] == null)
                  pages[index] = new ScreenSlidePageFragment();
              return pages[index]; 
          }
  
      }
  
  
  }
  </code></pre>
  
  <p><strong>[UPDATE]</strong></p>
  
  <p>I've managed to write a code that allows me to infinitely slide to the right between 6 different pages. Left side is limited though - I can slide only to the first page(so if I'm on 1st page after I cycled 3 times to the right, I can make only 3 cycles backwards). I think I am very close to finding the solution. Any ideas?</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>    public class MainActivity extends FragmentActivity  {
  
  private static final int NUM_PAGES = 6;
  private ViewPager pager;
  private PagerAdapter pagerAdapter;
  private List&lt;ScreenSlidePageFragment&gt; slideList;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      slideList = new ArrayList&lt;ScreenSlidePageFragment&gt;();
      for (int i = 0; i&lt;NUM_PAGES; i++){
          ScreenSlidePageFragment slide = new ScreenSlidePageFragment();
          slide.setIndex(i+1);
          slideList.add(slide);
      }
  
      pager = (ViewPager) findViewById(R.id.pager);
      pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
      pager.setAdapter(pagerAdapter);
  }
  
  private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
  
      public ScreenSlidePagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          int _pos = position % NUM_PAGES;
          return slideList.get(_pos);
      }
  
      @Override
      public int getCount() {
          return Integer.MAX_VALUE;
      }
  }
  }
  </code></pre>
  
  <p>ScreenSlidePageFragment.java</p>
  
  <pre><code>  public class ScreenSlidePageFragment extends Fragment {
  
  
          private int index;
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              ViewGroup root = (ViewGroup)inflater.inflate(R.layout.slide, container, false);
              TextView tw = (TextView) root.findViewById(R.id.textView);
              tw.setText(Integer.toString(index));
              return root;
          }
  
          public void setIndex(int index){
              this.index = index;
          }
          }
  </code></pre>