Use ViewPager with simple views without Fragments

There are some simple use cases where you may not want to use ViewPager with Fragments. You can simply use it with views and it will work the same way!
You can check these articles:

To use this approach you can generate a simple enum with your views like this one:

public enum CustomPagerEnum {

    RED(R.string.red, R.layout.view_red),
    BLUE(R.string.blue, R.layout.view_blue),
    ORANGE(R.string.orange, R.layout.view_orange);

    private int mTitleResId;
    private int mLayoutResId;

    CustomPagerEnum(int titleResId, int layoutResId) {
        mTitleResId = titleResId;
        mLayoutResId = layoutResId;
    }

    public int getTitleResId() {
        return mTitleResId;
    }

    public int getLayoutResId() {
        return mLayoutResId;
    }

}

Your PagerAdapter should look like this:

public class CustomPagerAdapter extends PagerAdapter {

    private Context mContext;

    public CustomPagerAdapter(Context context) {
        mContext = context;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewGroup layout = (ViewGroup) inflater.inflate(customPagerEnum.getLayoutResId(), collection, false);
        collection.addView(layout);
        return layout;
    }

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

    @Override
    public int getCount() {
        return CustomPagerEnum.values().length;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
        return mContext.getString(customPagerEnum.getTitleResId());
    }

}
  • CustomPagerAdapter(Context context): The constructor needs a Context reference, since you’ll want to access some string and layout resources later on. You just save the Context as a member variable of the class.
  • instantiateItem: Using the LayoutInflater, you can inflate any desired XML layout. In this case, you use the enum and inflate the particular enum value’s associated layout. Next, you add the newly inflated layout to the collection of Views maintained by the PagerAdapter, and then you return that layout. The object being returned by this method is also used later on, as the second parameter in the isViewFromObjectmethod. Both refer to a particular item in the PagerAdapter. In this case, it’s a View.
  • destroyItem: This method removes a particular view from the collection of Views maintained by the PagerAdapter.
  • getCount: You simply return the number of views that will be maintained by the ViewPager. For this example, the count is the number of enum values in the model object.
  • isViewFromObject: This method checks whether a particular object belongs to a given position, which is made simple in this example—just check whether the View equals the object (i.e., points to the same reference). As noted earlier, the second parameter is of type Object and is the same as the return value from the instantiateItemmethod.
  • getPageTitle: At a given position, you need to supply the PagerAdapter with a title. This usually manifests itself in the ActionBar as the Activity’s title, or sometimes tabs will hook into this method for labelling each tab.

And that’s it 🙂

You may also like...