Create ViewPager without Fragment Android

Fetching Comments ...

Create ViewPager without Fragment Android

In last tutorial i wrote about how to access Fragments of ViewPager inside Parent Activity. But this this post i will write about how you can create ViewPager without having any Fragment in it. So this will be more developer friendly because sometime RecyclerView inside ViewPager will create problem if you are using ViewPager inside ScrollView or NestedScrollView. So this that case you can simply use RecyclerView as Pages of ViewPager.

But you need ViewPager adapter to control Views of ViewPager. In normal PagerAdapter we need to override below 2 methods.

    public Object instantiateItem(ViewGroup collection, int position) {

        int resId = 0;
        switch (position) {
            case 0:
                resId =; //pass id of that view to return, Views will be added in XML.
            case 1:
                resId =;
        return findViewById(resId); // return selected view.

    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1; // return true if both are equal.

here is complete code of this demo.

How to create ViewPager without Fragments.

ActivityClass java file

activity_scrolling.xml file

As you can see i have added two LinearLayout inside ViewPager in xml file. and passed id’s of that LinearLayouts inside PagerAdapter.

Share This:

LearnPainLess On Instagram

Get The Best Of All Hands Delivered To Your Inbox

Subscribe to our newsletter and stay updated.