A minimal PagerAdapter for ViewPager

As part of the android support library google has introduced the ViewPager class. ViewPager allows you to swipe left and right between a number of views. ViewPager is particularly useful for photo gallery style applications. Unfortunately google have not provided an example of of how to to do a minimal implementation of the PagerAdaptor class which provides the contents displayed in the ViewPager.

The following is a minimal implementation of PagerAdaptor which displays a list of Bitmaps in ImageViews:

import java.util.List;

import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

public class ImagePagerAdapter extends PagerAdapter {

    private List<Bitmap> bitmaps;

    public ImagePagerAdapter(List<Bitmap> newBitmaps) {
        bitmaps = newBitmaps;
    }

    @Override
    public int getCount() {
        return bitmaps.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position){

    	ImageView imageView = new ImageView(container.getContext());
        imageView.setLayoutParams(new ViewGroup.LayoutParams(
        		LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        Bitmap bitmap = bitmaps.get(position);
        imageView.setImageBitmap(bitmap);

        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object){
    	container.removeView((View)object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object){
    	return view == object;
    }
}