简体   繁体   English

如何使用ViewPager显示不同的布局?

[英]How to display different layouts with the ViewPager?

In Xamarin, I currently have two different activities and I would like to combine these together and use a ViewPager. 在Xamarin中,我目前有两个不同的活动,我想将它们组合在一起并使用ViewPager。

One activity uses the following content view: SetContentView(Resource.Layout.MapWithOverlayLayout); 一个活动使用以下内容视图: SetContentView(Resource.Layout.MapWithOverlayLayout);

The second activity uses the following content view: SetContentView(Resource.Layout.HomeScreen); 第二个活动使用以下内容视图: 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? 我可以帮助编写一个简单的ViewPager,它可以将上述两个内容视图添加到ViewPager中,然后通过代码显示每个内容视图吗?

Thanks in advance 提前致谢

For a ViewPager, you will need Fragments instead of Activities . 对于ViewPager, 您将需要 Fragments而不是Activities

Here is how to implement a ViewPager with Fragments and different layouts : How to implement a ViewPager with different Fragments / Layouts 下面是如何实现ViewPager片段和不同的布局如何实现ViewPager不同片段/布局

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. 基本思想是为每个布局创建一个Fragment ,然后在FragmentPagerAdapter ,根据您当前所在的页面加载正确的Fragment

This is how I am doing it in my other post and sorry about not posting the same code here. 这就是我在其他帖子中的做法,并抱歉不在此处发布相同的代码。

Take a look at those links. 看看这些链接。 It will help for sure. 这肯定会有所帮助。

Android:How to create different view in ViewPager? Android:如何在ViewPager中创建不同的视图?

Its a pretty simple implementation. 这是一个非常简单的实现。 You can define your own layouts and then use it in the view pager. 您可以定义自己的布局,然后在视图分页器中使用它。

Here's my other post where you can get what you want to achieve: 这是我的另一篇文章,你可以得到你想要达到的目标:

How to use swipe gesture in a view pager's page with android? 如何在Android的视图寻呼机页面中使用滑动手势?

Hope this helps..:) 希望这可以帮助..:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM