How to display different layouts with the ViewPager?

Garry picture Garry · Mar 14, 2014 · Viewed 10.3k times · Source

In Xamarin, I currently have two different activities and I would like to combine these together and use a ViewPager.

One activity uses the following content view: SetContentView(Resource.Layout.MapWithOverlayLayout);

The second activity uses the following content view: SetContentView(Resource.Layout.HomeScreen);

Can I please have some help to code a simple ViewPager that can add the two above content views into a ViewPager and then show each of these content views via code?

Thanks in advance

Answer

Philipp Jahoda picture Philipp Jahoda · Mar 14, 2014

For a ViewPager, you will need Fragments instead of Activities.

Here is how to implement a ViewPager with Fragments and different layouts: How to implement a ViewPager with different Fragments / Layouts

The basic idea is that you create a Fragment for every layout you have, and then inside your FragmentPagerAdapter, load the correct Fragment depending on which page you are currently on.