Android PagerAdapter

Android PagerAdapter Tutorial and Examples.

Normally you use PagerAdapters with ViewPager. ViewPager you know allows us swipe through pages.

Well PagerAdapter acts as the base class for providing the adapter to populate pages inside of that ViewPager.

Most of the time we don’t use pageradapter directly but instead use it’s implementations like:


There are 4 methods that are important to a PagerAdapter and have to be overriden:

  1. instantiateItem(ViewGroup,int)
  2. destroyItem(ViewGroup,int,Object)
  3. getCount()
  4. isViewFromObject(View,Object)

Common Methods

Here are some of the most commonly used methods of this class:

  1. getCount : Return the number of views available.
  2. destroyItem : Remove a page for the given position. The adapter is responsible for removing the view from its container, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).
  3. finishUpdate : Called when the a change in the shown pages has been completed. At this point you must ensure that all of the pages have actually been added or removed from the container as appropriate.
  4. getPageTitle : This method may be called by the ViewPager to obtain a title string to describe the specified page. This method may return null indicating no title for this page. The default implementation returns null.
  5. instantiateItem : Create the page for the given position. The adapter is responsible for add

PagerAdapter Examples.

Here’s an example scenario where we’ve used PagerAdapter, overriding it’s methods.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ItemPagerAdapter extends {

    Context mContext;
    LayoutInflater mLayoutInflater;
    final int[] mItems;

    public ItemPagerAdapter(Context context, int[] items) {
        this.mContext = context;
        this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mItems = items;

    public int getCount() {
        return mItems.length;

    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);

    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
        ImageView imageView = (ImageView) itemView.findViewById(;
        return itemView;

    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);

Find the full example here.


Share an Example

Share an Example

What is the capital of Egypt? ( Cairo )