Basic ViewPager usage with fragments
suggest changeA ViewPager shows multiple fragments in an activity. The user moves between them by flipping left or right. A PagerAdapter supplies the pages, which can be views or fragments.
There are however two more specific implementations that you will find most useful in case of using Fragments which are FragmentPagerAdapter and FragmentStatePagerAdapter. When a Fragment needs to be instantiated for the first time, getItem(position) will be called for each position that needs instantiating. The getCount() method will return the total number of pages so the ViewPager knows how many Fragments need to be shown.
Both FragmentPagerAdapter and FragmentStatePagerAdapter keep a cache of the Fragments that the ViewPager will need to show. By default the ViewPager will try to store a maximum of 3 Fragments that correspond to the currently visible Fragment, and the ones next to the right and left. FragmentStatePagerAdapter saves each fragment's state and destroys fragments that are far from the current page, so it uses less memory than FragmentPagerAdapter.
Both implementations assume the fragments keep their positions. If the pages come from a changing list instead of the fixed getItem() switch below, subclass PagerAdapter and override at least instantiateItem(), destroyItem(), and getItemPosition().
Just add a ViewPager in your layout as described in the basic example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/vpPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Then define the adapter that will determine how many pages exist and which fragment to display for each page of the adapter.
public class MyViewPagerActivity extends AppCompatActivity {
private static final String TAG = MyViewPagerActivity.class.getName();
private MyPagerAdapter mFragmentAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myActivityLayout);
//Apply the Adapter
mFragmentAdapter = new MyPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.vpPager);
mViewPager.setAdapter(mFragmentAdapter);
}
private class MyPagerAdapter extends FragmentPagerAdapter{
public MyPagerAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
default:
return null;
}
}
// Returns total number of pages
@Override
public int getCount() {
return 3;
}
}
}
If you are using android.app.Fragment you have to add this dependency:
compile 'com.android.support:support-v13:25.3.1'
If you are using android.support.v4.app.Fragment you have to add this dependency:
compile 'com.android.support:support-fragment:25.3.1'