Hello guys,

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 = R.id.page_one; //pass id of that view to return, Views will be added in XML.
                break;
            case 1:
                resId = R.id.page_two;
                break;
        }
        return findViewById(resId); // return selected view.
    }

    //and
    @Override
    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

package com.edablogs.viewpagerfragment;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;

public class ScrollingActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);

        MyPagerAdapter adapter = new MyPagerAdapter();
        mViewPager.setAdapter(adapter); //set adapter to viewpager.
    }

    //Normal pager adapter to handle items inside Viewpager.
    class MyPagerAdapter extends PagerAdapter {

        public Object instantiateItem(ViewGroup collection, int position) {

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

        @Override
        public int getCount() {
            return 2; // number of maximum views in View Pager.
        }

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

}

activity_scrolling.xml file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.edablogs.viewpagerfragment.ScrollingActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >

        <LinearLayout
            android:id="@+id/page_one"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <TextView
                android:text="PAGE ONE IN"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textColor="#000"
                android:textSize="24dp"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/page_two"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <TextView
                android:text="PAGE TWO IN"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textColor="#000"
                android:textSize="24dp"/>
        </LinearLayout>
    </android.support.v4.view.ViewPager>

</FrameLayout>

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