title: Android ViewPager w/ multiple Fragment classes each w/ separate layout file
body: <p>My app uses the <code>android.support.v4.view.ViewPager</code> class. For each page in the ViewPager, I want there to be one Fragment, each with its own layout file. Each Fragment is defined in its own file. I am having a hard time implementing this. To clarify, there is: </p>
  
  <p>1 ViewPager <br />
  4 Fragment Files - FragmentOne.class, FragmentTwo.class, FragmentThree.class, FragmentFour.class<br />
  4 XML Layout Files - frag_one.xml, frag_two.xml, frag_three.xml, frag_four.xml</p>
  
  <p>The examples that I have seen only deal with one layout file.</p>
  
  <p>Here is my FragmentActivity where the ViewPager is held:</p>
  
  <pre><code>public class Main extends FragmentActivity {
      MyAdapter mAdapter;
      ViewPager mPager;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          mAdapter = new MyAdapter(getSupportFragmentManager());
  
          mPager = (ViewPager) findViewById(R.id.pager);
          mPager.setAdapter(mAdapter);
  
      }
  
      public static class MyAdapter extends FragmentPagerAdapter {
          public MyAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public int getCount() {
              return 4;
          }
  
          @Override
          public Fragment getItem(int position) {
              return EditorFragment.newInstance(position);
          }
      }
  
  }
  </code></pre>
  
  <p>My main question is: How do I get the adapter, <code>MyAdapter</code>, to recognize the four separate Fragment files and smoothly page across them with ViewPager? What kind of modifications should I make to <code>getItem()</code>?</p>
  
  <p>Any help is appreciated.</p>
  
  <p>EDIT:</p>
  
  <p>So, I tried what lulumeya suggested and came up with this:</p>
  
  <p>EDIT 2: Yes, those break statements fixed it. It works now. Thanks to everyone who helped! Now I have a nice segregation, thanks.</p>
  
  <pre><code>    @Override
      public Fragment getItem(int position) {
          Fragment f = new Fragment();
  
          switch (position) {
          case 0:
              f = FragmentOne.newInstance(position);
                      break;
          case 1:
              f = FragmentTwo.newInstance(position);
                      break;
          case 2:
              f = FragmentThree.newInstance(position);
                      break;
          case 3:
              f = FragmentFour.newInstance(position);
                      break;
          }
  
          return f;
      }
  </code></pre>
  
  <p>The app lauches, which is always a good sign, but is stuck on layout four. When I page to the right it is also layout four, for all pages. I was sure to change the layout in the inflater for each Fragment. I am still going to work on this. @lulumeya - Is this what you meant?</p>