title: Android: Fragment and ViewPager starts always at first position
body: <p>I use the Master/Detail design for my Android application. If I click on a item in the listview in my ItemListActivity the position is given to my ItemDetailActivity. This one implements the ViewPager and the FragmentStatePagerAdapter. So I want to swipe between the detail views.</p>
  
  <p>I do not how to tell the ViewPager to start at the desired position (the integer from the ItemListActivity). My ViewPager starts always at the position 0 of my list no matter which one I clicked on the list.</p>
  
  <p>This is my code:</p>
  
  <p><strong>ItemDetailActivity</strong></p>
  
  <pre><code>public class ItemDetailActivity extends FragmentActivity {
  
      RSSFeed feed;
      int pos;
      private static List&lt;Fragment&gt; fragments;
  
      /**
       * The pager widget, which handles animation and allows swiping horizontally to access previous
       * and next wizard steps.
       */
      private ViewPager mPager;
  
      /**
       * The pager adapter, which provides the pages to the view pager widget.
       */
      private PagerAdapter mPagerAdapter;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // define layout of this activity
          setContentView(R.layout.activity_item_detail);
  
          // Get the feed object and the position from the Intent
          feed = (RSSFeed) getIntent().getExtras().get("feed");
          //position of the clicked item from the list
          pos = getIntent().getExtras().getInt("pos");
  
          //create all 20 ItemDetailFragments
          fragments = new ArrayList&lt;Fragment&gt;();
           for(int i=0; i&lt;=20; i++)
                  fragments.add(ItemDetailFragment.create(i ,feed));
  
          // Instantiate a ViewPager and a PagerAdapter.
          mPager = (ViewPager) findViewById(R.id.viewpager);
          mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
          mPager.setAdapter(mPagerAdapter);
          mPager.setPageTransformer(true, new ZoomOutPageTransformer());
      }
  
      @Override
      public void onBackPressed() {
          if (mPager.getCurrentItem() == 0) {
              // If the user is currently looking at the first step, allow the system to handle the
              // Back button. This calls finish() on this activity and pops the back stack.
              super.onBackPressed();
          } else {
              // Otherwise, select the previous step.
              mPager.setCurrentItem(mPager.getCurrentItem() - 1);
          }
      }
  
      /**
       * A simple pager adapter that represents ItemDetailFragment objects, in
       * sequence.
       * 
       * Creates a class that extends the FragmentStatePagerAdapter abstract class and
       * implements the getItem() method to supply instances of ScreenSlidePageFragment
       * as new pages. The pager adapter also requires that you implement the getCount()
       * method, which returns the amount of pages the adapter will create. 
       */
      private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
  
          public ScreenSlidePagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int page_number) {
              return fragments.get(page_number);
          }
  
          @Override
          public int getCount() {
              //get the count of items in the feed
              return feed.getItemCount();
          }
      }
  </code></pre>
  
  <p><strong>ItemDetailFragment:</strong></p>
  
  <pre><code>/**
   * A fragment representing a single Item detail screen. This fragment is either
   * contained in a {@link ItemListActivity} in two-pane mode (on tablets) or a
   * {@link ItemDetailActivity} on handsets.
   */
  public class ItemDetailFragment extends Fragment {
  
      private static int fPos;
      static RSSFeed fFeed;
      private ShareActionProvider mShareActionProvider;
      Date pDate;
  
      /**
       * Mandatory empty constructor for the fragment manager to instantiate the
       * fragment (e.g. upon screen orientation changes).
       */
      public ItemDetailFragment() {
      }
  
      /**
       * Factory method for this fragment class. Constructs a new fragment for the given page number.
       */
      public static ItemDetailFragment create(int pos, RSSFeed feed) {
  
          ItemDetailFragment fragment = new ItemDetailFragment();     
          Bundle args = new Bundle();
          args.putSerializable("feed", feed);
          args.putInt("pos", pos);
          fragment.setArguments(args);        
          return fragment;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // show ActionBar
          setHasOptionsMenu(true);
  
          // get data from DetailActivity
          fFeed = (RSSFeed) getArguments().getSerializable("feed");
          fPos = getArguments().getInt("pos");
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          // inflate the fragment with the custom detail fragment
          View view = inflater
                  .inflate(R.layout.detail_fragment, container, false);
  
          return view;
      }
  @SuppressLint("SetJavaScriptEnabled")
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
                 //here I put the data in my webview
      }
  </code></pre>
  
  <p><strong>activity_item_detail.xml</strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
      android:id="@+id/main_item_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
     &lt;android.support.v4.view.ViewPager
       android:id="@+id/viewpager"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" /&gt;
  
      &lt;FrameLayout
          android:id="@+id/item_detail_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_above="@+id/adView"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          tools:context=".ItemDetailActivity"
          tools:ignore="MergeRootFrame" &gt;
  
      &lt;/FrameLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>So at the moment I can sqipe through all items but it always starts at the first item. I want to start it at that item i clicked on in my listview.</p>
  
  <p>How can I do this?</p>