简体   繁体   English

在FragmentActivity中向ViewPager添加一个新片段

[英]Add a new fragment to ViewPager within a FragmentActivity

I have spent the better part of two days searching for an answer that solves my question, I have found plenty that come close but not specific to what I need: 我花了两天的大部分时间来寻找可以解决我的问题的答案,我发现有很多可以接近但并非特定于我需要的东西:

I would like to add a new FragmentActivity to a viewPager from inside a current FragmentActivity by calling the FragmentPageAdapter (or at least, this is how I see I need to solve my problem). 我想通过调用FragmentPageAdapter从当前FragmentActivity内部向viewPager添加新的FragmentActivity(或者至少,这是我认为需要解决的问题)。

For example: In fragment 1 I would like to press a button to update and replace Fragment 2 with new data, and then set Fragment 2 as the currently viewed fragment. 例如:在片段1中,我想按一个按钮来更新片段2并将其替换为新数据,然后将片段2设置为当前查看的片段。

What you could do is implementing a custom FragmentPagerAdapter or FragmentStatePagerAdapter (If you have lots of more complex fragments). 您可以做的是实现自定义FragmentPagerAdapter或FragmentStatePagerAdapter(如果您有很多更复杂的片段)。

Documentation: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html 文档: http : //developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

The Adapter handles your contents so whatever calls or operations you wanna make on a certain fragment could be done quite easy from there. 适配器会处理您的内容,因此您想要在某个片段上进行的任何调用或操作都可以从那里轻松完成。

You could pass the adapter itself to every fragment that its holding or just implement listener interfaces so that the fragments can trigger the changes through the adapter. 您可以将适配器本身传递给它持有的每个片段,也可以只实现侦听器接口,以便片段可以通过适配器触发更改。

I hope this helps you out if its still an issue! 希望这对您仍然有帮助!

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

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