public class MyActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); // Add a fragment to the layout MyFragment fragment = new MyFragment(); getSupportFragmentManager().beginTransaction() .add(R.id.container, fragment) .commit(); } }
public class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return new MyFragment1(); case 1: return new MyFragment2(); case 2: return new MyFragment3(); default: return null; } } @Override public int getCount() { return 3; } }This example shows how to create a pager adapter that displays multiple fragments. The adapter is used in a ViewPager to allow the user to swipe left and right between the fragments. The fragments are created using the getItem() method and the getCount() method returns the number of fragments in the adapter. Package library: android.support.v4