ViewPager is a Layout manager that allows the user to flip left and right through pages of data. It is most often used in conjunction with Fragment, which is a convenient way to supply and manage the lifecycle of each page.
One important thing to note about ViewPager usage is that there are two different versions of both FragmentPagerAdapter and FragmentStatePagerAdapter.
If you are using android.support.v4.app.Fragment support library Fragments with a FragmentPagerAdapter or FragmentStatePagerAdapter, you need to use the v4 support library versions of the adapter, i.e. android.support.v4.app.FragmentStatePagerAdapter.
Adding a ViewPager
Make sure the following dependency is added to your app's build.gradle file under dependencies:
A ViewPager allows to show multiple fragments in an activity that can be navigated by either fliping left or right. A ViewPager needs to be feed of either Views or Fragments by using a PagerAdapter.
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. Also FragmentStatePagerAdapter will keep the state of each of your fragments.
Be aware that both implementations assume your fragments will keep their positions, so if you keep a list of the fragments instead of having a static number of them as you can see in the getItem() method, you will need to create a subclass of PagerAdapter and override at least instantiateItem(),destroyItem() and getItemPosition()methods.
Just add a ViewPager in your layout as described in the basic example:
Then define the adapter that will determine how many pages exist and which fragment to display for each page of the adapter.
If you are using android.app.Fragment you have to add this dependency:
If you are using android.support.v4.app.Fragment you have to add this dependency:
Firstly, we have to add TabLayout to our screen layout, and connect it with ViewPager. We can do this in two ways:
Nested TabLayout in ViewPager
In this case TabLayout will be automatically connected with ViewPager, but TabLayout will be next to ViewPager, not over him.
In this case, we can put TabLayout anywhere, but we have to connect TabLayout with ViewPager programmatically
Once we created our layout, we have to prepare our dots. So we create three files: selected_dot.xml, default_dot.xml and tab_selector.xml.
Now we need to add only 3 lines of code to TabLayout in our xml layout and you're done.
ViewPager with PreferenceFragment
Until recently, using android.support.v4.app.FragmentPagerAdapter would prevent the usage of a PreferenceFragment as one of the Fragments used in the FragmentPagerAdapter.
The latest versions of the support v7 library now include the PreferenceFragmentCompat class, which will work with a ViewPager and the v4 version of FragmentPagerAdapter.
Example Fragment that extends PreferenceFragmentCompat:
You can now use this Fragment in a android.support.v4.app.FragmentPagerAdapter subclass:
ViewPager with TabLayout
A TabLayout can be used for easier navigation.
You can set the tabs for each fragment in your adapter by using TabLayout.newTab() method but there is another more convenient and easier method for this task which is TabLayout.setupWithViewPager().
This method will sync by creating and removing tabs according to the contents of the adapter associated with your ViewPager each time you call it.
Also, it will set a callback so each time the user flips the page, the corresponding tab will be selected.
Just define a layout
Then implement the FragmentPagerAdapter and apply it to the ViewPager:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0