Android ViewPager Tutorial with Example

By | June 9, 2019
ViewPager in Android Tutorial Example

What is ViewPager ?

ViewPager is Android allows the user to flip left and right through pages of data. user can swipe left to right in ViewPager. it is used to Fragments.

ViewPager has the ability to dynamically add and remove pages (or tabs) at anytime. ViewPager are user could then swipe left or right to see other categorized lists. Using the ViewPager requires some knowledge of both Fragments and PageAdapters. ViewPager provides the functionality to flip pages in app.

Pager Adapter In ViewPager

Android ViewPager is a layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.

PagerAdapter providing the adapter to populate pages inside of a ViewPager.

It does not Required Fragment you can use only xml layout file to attach in view pager.

It is limited (fixed) number of items (Fragments). PagerAdapter never removes a fragment instance from FragmentManager.

PagerAdapter used to layout xml file only it does not required fragments create.its create simple xml layout file and then attach the layout file from pagerAdapter.

you can Implement pagerAdapter to you must override methods in PagerAdapter.

ViewPager are two type of Adapter : PagerAdapter and FragmentStatePagerAdapter

PagerAdapter Methods in Android

instantiateItem(ViewGroup, int) : In this method is used to  create the page for the given position passed to it as an argument.  You can inflate layout file and then set resource in layout file. it is return of view objects.

destroyItem(ViewGroup, int, Object) : In this method is used to Removes the page from the container for the given can removed object using removeView but could’ve also used removeViewAt() method by passing it the position.

getCount() : In this method is used to return the number of views available.

isViewFromObject(View, Object) : In this method is used to returned by instantiateItem() is a key/identifier. This method check the View passed or not. It is required by a PagerAdapter.

FragmentStatePagerAdapter in ViewPager Android

In this adapter is required Fragments. you can use this adapter to required creates fragments file.

A FragmentStatePagerAdapter is more memory savvy. It is removed fragment instance from FragmentManager.

The state of the removed Fragments is stored inside the FragmentStatePagerAdapter.  It is better the content of the fragments are static than something that constantly changes or gets updated.

ViewPager Example in Android

Implements below This Library in build.gradle in App Module

Create activity_main.xml File Follow this Code.

Create fragment_first.xml File Follow this code.

Create fragment_second.xml File Follow this Code.

Create File Follow this Code

Create File Follow this Code.

Create File Follow this Code.

Create File Follow this Code.

In this Example of Create Simple ViewPager and Tabs and create two fragments and in this fragments implement ViewPager adapter class.In this adapter to count fragments and display fragments.

you can select first tab to open First Fragment. and you can click second tab to open Second Fragment screen.