简体   繁体   English

我的片段不会同时创建

[英]My fragments do not create at the same time

I really don't get it.我真的不明白。

Here is my situation :这是我的情况:

I have one activity and three fragments.我有一个活动和三个片段。 My fragments are organised with ViewPager, so I can swipe between them easily (horizontaly) :我的片段是用 ViewPager 组织的,所以我可以轻松地在它们之间滑动(水平):

Fragment 1 (main, left) - Fragment 2 (middle) - Fragment 3 (right)片段 1(主要,左) - 片段 2(中间) - 片段 3(右)

When I launch my app.当我启动我的应用程序时。 I'm at Fragment 1 (left), and when I read my Logcat, it's indicated that my Fragment 1 and Fragment 2 has been created and onResume() has been called (for both of them).我在 Fragment 1(左),当我阅读我的 Logcat 时,它表明我的 Fragment 1 和 Fragment 2 已经创建并且onResume()已经被调用(对于它们两个)。 But not the Fragment 3. So, I swipe to the right (to go at the Fragment 2) and only at this moment I can read : onCreateView() Fragment 3 and onResume() Fragment 3但不是 Fragment 3。所以,我向右滑动(转到 Fragment 2),只有此时我才能阅读: onCreateView() Fragment 3onResume() Fragment 3

I go back to Fragment 2 and it says : onResume() Fragment 1 .我回到 Fragment 2,它说: onResume() Fragment 1 And I go back to the Fragment 1 (Left), and It says : onPause() Fragment 3然后我回到片段 1(左),它说: onPause() Fragment 3

Another weird thing : My fragment 2 (middle) absolutely never calls onPause() or onResume() except when I leave the application...另一个奇怪的事情:我的片段 2(中间)绝对不会调用 onPause() 或 onResume() 除非我离开应用程序......

It makes no sense right ?没有意义吧?

EDIT编辑

I followed this tutorial to realise my pageviewer : https://www.javacodegeeks.com/2013/04/android-tutorial-using-the-viewpager.html我按照本教程来实现我的网页浏览器: https : //www.javacodegeeks.com/2013/04/android-tutorial-using-the-viewpager.html

EDIT 2编辑 2

When I display ScanFragment scanfragment = (ScanFragment) getSupportFragmentManager().getFragments().get(0);当我显示ScanFragment scanfragment = (ScanFragment) getSupportFragmentManager().getFragments().get(0); it returns me "Fragment 1", but after few swipes, it returnes me "Fragment 2"它返回我“片段 1”,但几次滑动后,它返回“片段 2”

Does someone understands ?有人明白吗?

This is is the expected behaviour of ViewPager.这是 ViewPager 的预期行为。 It will only create views/fragments when required.它只会在需要时创建视图/片段。 Here you can read more about ViewPager . 在这里你可以阅读更多关于ViewPager 的信息

暂无
暂无

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

相关问题 ViewPager 同时加载 2 个片段 - ViewPager loads 2 fragments at the same time 我怎样才能每次更改片段中的文本? 对于每个问题都需要相同片段的测验应用程序 - How can i change the texts in my fragments each time? For a quiz app that needs the same fragment for each question 如何同时重新加载所有片段 - How to reload all the fragments at the same time 如何为具有相同内容的多个片段创建片段模板(具有变量和方法的类、布局)? - How do I create a fragment template(class with variables and methods, layout) for multiple fragments with the same content? 我第一次访问“活动”时,“片段”中的菜单项不会根据代码更改 - Menuitems in my Fragments do not change according to the code for the first time I access Activity 我需要在这种情况下创建片段吗? - Do I need to create fragments in this kind of condition? 在两个片段中将AsyncTask onPostExecute方法定时在Android中同时完成 - Timing AsyncTask onPostExecute method in two Fragments to finish at the same time in android paintcomponent方法中的我的JLabel和GUI代码无法同时绘制 - My JLabel and GUI code in a paintcomponent method do not draw at the same time 使用页面查看器创建页面的所有片段中都显示相同的数据 - Same data is displaying in all fragments using page Viewers to create pages ViewPager和Fragments:为什么我总是看到相同的东西? - ViewPager and Fragments: Why do I always see the same thing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM