193

When I start an activity which implements viewpager, the viewpager created various fragments. I want to use different layouts for each fragment, but the problem is that viewpager shows only two layouts at the max (second layout on all of the remaining fragments after 1).

Here is the code for SwipeActivity which implements the viewpager :

public class SwipeActivity extends FragmentActivity
{

    MyPageAdapter pageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe);
        pageAdapter = new MyPageAdapter(getSupportFragmentManager());
        ViewPager pager=(ViewPager)findViewById(R.id.pager);
        pager.setAdapter(pageAdapter);
        ActionBar bar = getActionBar();
        bar.setDisplayHomeAsUpEnabled(true);
    }
    /**
    * Custom Page adapter
    */
    private class MyPageAdapter extends FragmentPagerAdapter
    {
        public MyPageAdapter(FragmentManager fm)
        {
            super(fm);
        }
        @Override
        public int getCount()
        {
            return 5;
        }
        @Override
        public Fragment getItem(int position)
        {
            switch(position)
            {
                case 0: return new MyFragment();
                case 1: return SecondFragment.newInstance("asdasd");
                default : return RamFragment.newInstance("s");
            }
        }
     }
}

Here is the code for the fragments

public class MyFragment extends Fragment
{
   @Override
   public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup,    Bundle paramBundle)
   {
     return paramLayoutInflater.inflate(R.layout.processorlayout, paramViewGroup, false);
   }
}

I used 5 fragments like this, all having different layouts, but the viewpager shows only 2 at the max.

EDIT : code for SecondFragment

public class SecondFragment extends Fragment
{
   public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

  public static final SecondFragment newInstance(String paramString)
  {
    SecondFragment f = new SecondFragment();
    Bundle localBundle = new Bundle(1);
    localBundle.putString("EXTRA_MESSAGE", paramString);
    f.setArguments(localBundle);
    return f;
  }

  @Override
  public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
  {
     return paramLayoutInflater.inflate(R.layout.motherboardlayout, paramViewGroup, false);
  }
}
Philipp Jahoda
  • 47,594
  • 21
  • 164
  • 175
Haxor
  • 2,266
  • 4
  • 14
  • 18
  • I am gettng a type mismatch error. Can you take a look at my post? http://stackoverflow.com/questions/28334800/type-mismatch-cannot-convert-from-my-activity-to-fragment – Schardt12 Feb 05 '15 at 01:49
  • https://androidbeasts.wordpress.com/2015/08/11/tabs-with-swipe-views/#more-79 – Aakash Aug 11 '15 at 23:16
  • What is the issue of your code so ? – user1510006 Jan 23 '16 at 16:24
  • Hi you can read this [post](http://stackoverflow.com/questions/32829376/viewpager-with-circle-and-title-indicator/34786197#34786197) very simple answer with complete code provided. – Nirmal Dhara May 03 '16 at 17:08

6 Answers6

529

As this is a very frequently asked question, I wanted to take the time and effort to explain the ViewPager with multiple Fragments and Layouts in detail. Here you go.

ViewPager with multiple Fragments and Layout files - How To

The following is a complete example of how to implement a ViewPager with different fragment Types and different layout files.

In this case, I have 3 Fragment classes, and a different layout file for each class. In order to keep things simple, the fragment-layouts only differ in their background color. Of course, any layout-file can be used for the Fragments.

FirstFragment.java has a orange background layout, SecondFragment.java has a green background layout and ThirdFragment.java has a red background layout. Furthermore, each Fragment displays a different text, depending on which class it is from and which instance it is.

Also be aware that I am using the support-library's Fragment: android.support.v4.app.Fragment

MainActivity.java (Initializes the Viewpager and has the adapter for it as an inner class). Again have a look at the imports. I am using the android.support.v4 package.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     

        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int pos) {
            switch(pos) {

            case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
            case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
            case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
            case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
            case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
            default: return ThirdFragment.newInstance("ThirdFragment, Default");
            }
        }

        @Override
        public int getCount() {
            return 5;
        }       
    }
}

activity_main.xml (The MainActivitys .xml file) - a simple layout file, only containing the ViewPager that fills the whole screen.

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/viewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

The Fragment classes, FirstFragment.java import android.support.v4.app.Fragment;

public class FirstFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.first_frag, container, false);

        TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
        tv.setText(getArguments().getString("msg"));

        return v;
    }

    public static FirstFragment newInstance(String text) {

        FirstFragment f = new FirstFragment();
        Bundle b = new Bundle();
        b.putString("msg", text);

        f.setArguments(b);

        return f;
    }
}

first_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_dark" >

    <TextView
        android:id="@+id/tvFragFirst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="26dp"
        android:text="TextView" />
</RelativeLayout>

SecondFragment.java

public class SecondFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.second_frag, container, false);

    TextView tv = (TextView) v.findViewById(R.id.tvFragSecond);
    tv.setText(getArguments().getString("msg"));

    return v;
}

public static SecondFragment newInstance(String text) {

    SecondFragment f = new SecondFragment();
    Bundle b = new Bundle();
    b.putString("msg", text);

    f.setArguments(b);

    return f;
}
}

second_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_dark" >

    <TextView
        android:id="@+id/tvFragSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="26dp"
        android:text="TextView" />

</RelativeLayout>

ThirdFragment.java

public class ThirdFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.third_frag, container, false);

    TextView tv = (TextView) v.findViewById(R.id.tvFragThird);      
    tv.setText(getArguments().getString("msg"));

    return v;
}

public static ThirdFragment newInstance(String text) {

    ThirdFragment f = new ThirdFragment();
    Bundle b = new Bundle();
    b.putString("msg", text);

    f.setArguments(b);

    return f;
}
}

third_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light" >

    <TextView
        android:id="@+id/tvFragThird"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="26dp"
        android:text="TextView" />

</RelativeLayout>

The end result is the following:

The Viewpager holds 5 Fragments, Fragments 1 is of type FirstFragment, and displays the first_frag.xml layout, Fragment 2 is of type SecondFragment and displays the second_frag.xml, and Fragment 3-5 are of type ThirdFragment and all display the third_frag.xml.

enter image description here

Above you can see the 5 Fragments between which can be switched via swipe to the left or right. Only one Fragment can be displayed at the same time of course.

Last but not least:

I would recommend that you use an empty constructor in each of your Fragment classes.

Instead of handing over potential parameters via constructor, use the newInstance(...) method and the Bundle for handing over parameters.

This way if detached and re-attached the object state can be stored through the arguments. Much like Bundles attached to Intents.

Philipp Jahoda
  • 47,594
  • 21
  • 164
  • 175
  • Added an empty constructor to all the fragment classes, changed the getItem method. Still I see only 2 layouts in all fragments – Haxor Aug 24 '13 at 09:22
  • Ok. To understand you correctly: You have 5 Fragments in total, the 1st and 2nd Fragment are of class MyFragment (1) and SecondFragment (2), the other 3 Fragments are of class RamFragment? And you want to have an own layout for MyFragment, an own layout for SecondFragment and an own layout for all the RamFragments? Is that correct? – Philipp Jahoda Aug 24 '13 at 09:25
  • Yes, that is what I want. Even If I change the order of the layouts like 1) Ramfragment 2)MyFragment 3)Secondfragment. First 2 layouts are correctly working, But from the 3rd layout what I see is a copy of second layout only. – Haxor Aug 24 '13 at 09:29
  • Please have a look at my updated answer, I added a full example and tested it. – Philipp Jahoda Aug 24 '13 at 10:41
  • Did you use the suppport Fragment? Other than that, I dont know either. – Philipp Jahoda Aug 24 '13 at 21:37
  • hi @PhilippJahoda, thanks for code,i use your code but i want to set animation while changing fragment, can you tell me how can do that? thank. – Shayan Pourvatan Dec 24 '13 at 07:11
  • Thank you so much @Phillip Jahoda.. It is working ;) – Faraz Ahmed Jan 29 '14 at 13:32
  • 7
    @PhilippJahoda great answer, thanks. I would like to mention one thing though, if the ViewPager is defined in a Fragment than **getSupportChildFragmentManager()** should be used instead of getSupportFragmentManager() when creating the adapter. Otherwise the code breaks when orientation change occurs. – guy.gc Mar 23 '14 at 05:56
  • 6
    Yes, that is correct. But in my example the ViewPager is defined in an Activity, and therefore, getSupportFragmentManager() is appropriate. Furthermore the method getSupportChildFragmentManager() does not exist, I assume you are referring to getChildFragmentManager(). – Philipp Jahoda Mar 23 '14 at 08:29
  • I am trying to use the above code, however my ViewPager is not showing. Can I please have some help at this link: http://stackoverflow.com/questions/22499324/viewpager-views-are-not-displaying – Garry Mar 24 '14 at 09:43
  • How can I replace first fragment with a new fragment from firstfragment e.g. on button click ... I want swipe feature must be there i.e. If yoy swipe from this new fragment you go to second fragment.. – Pramod May 16 '14 at 12:22
  • I did it exactly as you did, but with one difference, i used a ActionBarActivity for my MainActivity. It happens that after some swipes the UI freezes. No error is thrown and the app doesnt crash it just freezes. Did you experience also something like this? – krackmoe May 18 '14 at 20:38
  • If I have android.support.v4.view.ViewPager and android.app.Fragment, then how could I write the adapter. It shows some compile error. – subair_a Jun 06 '14 at 12:24
  • 1
    You can use the ViewPager with native fragments when using the v13 support package. http://developer.android.com/reference/android/support/v13/app/package-summary.html – Philipp Jahoda Jun 06 '14 at 15:05
  • What if we have different fragments with a nav drawer etc. and we want to show a viewpager inside one of those fragments? – user754730 Jul 09 '14 at 20:45
  • same approach, except that one of your drawer fragments needs to hold the viewpager – Philipp Jahoda Jul 10 '14 at 07:46
  • Well, what mistake does Haxor do? I really don't want to change my code structure. – Alston Aug 07 '14 at 09:51
  • In `MainActivity.java`, what if the fragment's class name is a variable? Can you help me with this question? http://stackoverflow.com/questions/25195481/the-content-of-different-fragment-cant-show-as-the-viewpager-swipe-to-it?noredirect=1#comment39236079_25195481 – Alston Aug 08 '14 at 07:38
  • what if the height of the fragments vary ? – Vihaan Verma Aug 20 '14 at 13:49
  • 1
    as of Android Lollipop 5.0, is this still valid? – Orkun Ozen Jan 15 '15 at 16:54
  • 6
    In your MyPagerAdapter --> getItem(int pos) method you always get a new instance of the target fragment (based on the pos value). So every time you rotate your device (orientation change) the getItem method is invoked and fragments are created again and again. But they get stored in the fragment manager upon creation. I think you should check if they are already in the fragment manager because if you don't you waste memory. See http://pastebin.com/0bJc9mHA – rekt0x Mar 02 '15 at 08:10
  • For some reason, even though I have the same code, I just can't make it work. I create 5 fragments of the same class with the newInstance() each one with a different argument (therefore source of data), but the second one has exactly the same content like the first one. What am I doing wrong? – Sotiris Falieris Mar 02 '15 at 12:03
  • @PhilippJahoda I am getting this error "Type mismatch: cannot convert from XXXXShowAllFragment to Fragment" in getItem(int position). – Karthikeyan Ve Mar 25 '15 at 14:01
  • @Zortkun, yes it's still valid with Lollipop. Thank you so much Philipp, it's such a classy solution ! It's the sole clean solution I found for implementing a SupportMapFragment in a ViewPager, which also is in a Fragment. – Standaa - Monica side Apr 16 '15 at 08:15
  • why you create public static FirstFragment newInstance(String text) { } tell me its reason and it we do not create than what happen? – Faisal Ashraf May 19 '15 at 09:58
  • 1
    How do you capture click events in the fragments you added and change the page of the viewpager? – portfoliobuilder Sep 30 '15 at 21:32
  • But as @m1nd said, this will create new fragment instance on each config change, i.e. orientation change, and using internal code as in pasted link http://pastebin.com/0bJc9mHA isn't best approach, and also doesn't exist in case we want to use FragmentStatePagerAdapter – DoubleK Dec 01 '15 at 17:31
  • Thanks! It is the best tutorial which I found! – kostyabakay Mar 10 '16 at 00:50
  • If I want to stop swipe after third fragment how could I do it ? – Terance May 11 '16 at 08:28
  • How do I switch fragments on button click? – zeeks Jul 02 '16 at 11:22
  • @PhilippJahoda this is excellent,but can you tell what if i want to attach dynamic fragments – Aditya Vyas-Lakhan Oct 21 '16 at 06:21
  • Is it better if I create and store Fragments already (probably in Vadapter's constructor)? instead of creating them in getItem()? – Neon Warge Jan 05 '17 at 01:57
  • Perfect! Thank you very much, it works perfectly! By the way, here is a library for those who want to add indicators to the views: https://github.com/ongakuer/CircleIndicator – Razvan Sep 22 '17 at 13:45
  • GENIUS! Thank you, bro. – Richard Nikolas Sep 26 '17 at 14:27
  • great answer , this help me alot :) – Achref Gassoumi Oct 25 '17 at 09:55
  • Amazing answer. But I have a question. Let's assume that each fragment has a separate list and I want to add a SearchView in MainActivity for searching a keyword in all different lists. How could I implement this SearchView button for all fragments? – TeachMeJava May 03 '18 at 18:00
  • if the viewpager height is made half, then will the fragment layout draw within the height of viewpager or the full screen height? – Hari Kiran Jun 22 '20 at 06:59
10

Create an array of Views and apply it to: container.addView(viewarr[position]);

public class Layoutes extends PagerAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    Layoutes(Context context){
        this.context=context;
    }
    int layoutes[]={R.layout.one,R.layout.two,R.layout.three};
    @Override
    public int getCount() {
        return layoutes.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view==(LinearLayout)object);
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position){
        layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View one=layoutInflater.inflate(R.layout.one,container,false);
        View two=layoutInflater.inflate(R.layout.two,container,false);
        View three=layoutInflater.inflate(R.layout.three,container,false);
        View viewarr[]={one,two,three};
        container.addView(viewarr[position]);
        return viewarr[position];
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object){
        container.removeView((LinearLayout) object);
    }

}
Khalil M
  • 1,432
  • 2
  • 17
  • 33
akash ingle
  • 171
  • 1
  • 12
6

Code for adding fragment

public Fragment getItem(int position) {

    switch (position){
        case 0:
            return new Fragment1();

        case 1:
            return new Fragment2();

        case 2:
            return new Fragment3();

        case 3:
            return new Fragment4();

        default:
            break;
    }

    return null;
}

Create an xml file for each fragment say for Fragment1, use fragment_one.xml as layout file, use the below code in Fragment1 java file.

public class Fragment1 extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_one, container, false);

        return view;

    }
}

Later you can make necessary corrections.. It worked for me.

blackpla9ue
  • 3,191
  • 20
  • 29
sushh
  • 105
  • 1
  • 10
4

Basic ViewPager Example

This answer is a simplification of the documentation, this tutorial, and the accepted answer. It's purpose is to get a working ViewPager up and running as quickly as possible. Further edits can be made after that.

enter image description here

XML

Add the xml layouts for the main activity and for each page (fragment). In our case we are only using one fragment layout, but if you have different layouts on the different pages then just make one for each of them.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.verticalviewpager.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textview"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

Code

This is the code for the main activity. It includes the PagerAdapter and FragmentOne as inner classes. If these get too large or you are reusing them in other places, then you can move them to their own separate classes.

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends AppCompatActivity {

    static final int NUMBER_OF_PAGES = 2;

    MyAdapter mAdapter;
    ViewPager mPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdapter = new MyAdapter(getSupportFragmentManager());
        mPager = findViewById(R.id.viewpager);
        mPager.setAdapter(mAdapter);
    }

    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUMBER_OF_PAGES;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    return FragmentOne.newInstance(0, Color.WHITE);
                case 1:
                    // return a different Fragment class here
                    // if you want want a completely different layout
                    return FragmentOne.newInstance(1, Color.CYAN);
                default:
                    return null;
            }
        }
    }

    public static class FragmentOne extends Fragment {

        private static final String MY_NUM_KEY = "num";
        private static final String MY_COLOR_KEY = "color";

        private int mNum;
        private int mColor;

        // You can modify the parameters to pass in whatever you want
        static FragmentOne newInstance(int num, int color) {
            FragmentOne f = new FragmentOne();
            Bundle args = new Bundle();
            args.putInt(MY_NUM_KEY, num);
            args.putInt(MY_COLOR_KEY, color);
            f.setArguments(args);
            return f;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments() != null ? getArguments().getInt(MY_NUM_KEY) : 0;
            mColor = getArguments() != null ? getArguments().getInt(MY_COLOR_KEY) : Color.BLACK;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_one, container, false);
            v.setBackgroundColor(mColor);
            TextView textView = v.findViewById(R.id.textview);
            textView.setText("Page " + mNum);
            return v;
        }
    }
}

Finished

If you copied and pasted the three files above to your project, you should be able to run the app and see the result in the animation above.

Going on

There are quite a few things you can do with ViewPagers. See the following links to get started:

Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
3

This is also fine:

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/viewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

    }
}


public class FragmentTab1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragmenttab1, container, false);
        return rootView;
    }
}

class MyPagerAdapter extends FragmentPagerAdapter{

    public MyPagerAdapter(FragmentManager fragmentManager){
        super(fragmentManager);

    }
    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        switch(position){
            case 0:
                FragmentTab1 fm =   new FragmentTab1();
                return fm;
            case 1: return new FragmentTab2();
            case 2: return new FragmentTab3();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/Fragment1" />

</RelativeLayout>
Aaron Blenkush
  • 2,937
  • 2
  • 24
  • 49
Ashutoshg
  • 127
  • 6
0

Create new instances in your fragments and do like so in your Activity

 private class SlidePagerAdapter extends FragmentStatePagerAdapter {
    public SlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch(position){
            case 0:
                return Fragment1.newInstance();
            case 1:
                return Fragment2.newInstance();
            case 2:
                return Fragment3.newInstance();
            case 3:
                return Fragment4.newInstance();


            default: break;

        }
        return null;
    }
Mwongera808
  • 762
  • 10
  • 15