Learn Pain Less

HomeOur TeamContact
Android
Create ViewPager without Fragment Android
Pawneshwer Gupta
Pawneshwer Gupta
November 06, 2016
1 min

Table Of Contents

01
Create ViewPager without Fragment Android
02
How to create ViewPager without Fragments.
Create ViewPager without Fragment Android

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 = 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

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.

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

ViewPager

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

How to use Text To Speech inside RecyclerView in Android
How to use Text To Speech inside RecyclerView in Android
July 27, 2020
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media