父片段不显示儿童ViewPager [英] Parent Fragment Not Displaying Child ViewPager

查看:205
本文介绍了父片段不显示儿童ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用采用了Android兼容包与API级别10.我有一个FragmentActivity,并在其onCreate方法,我动态添加一个新创建的片段实例(myPagerFragment):

I am using the Android Compatibility Package with API Level 10. I have a FragmentActivity and in its onCreate method, I dynamically add a newly created Fragment instance (myPagerFragment) using:

getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, myPagerFragment).commit();

该myPagerFragment包含ViewPager这使得使用FragmentPagerAdapter的。执行是根据与没有什么特别的Andr​​oid文档。当在ViewPager子片段中的一个按钮被点击,则myPagerFragment将使用移除并以新的片段的实例(myOtherFragment)代替:

The myPagerFragment contains a ViewPager which makes use of a FragmentPagerAdapter. The implementation is according to the Android documentation with nothing special. When a button in one of the ViewPager child Fragments is clicked, then the myPagerFragment will be removed and replaced by a new Fragment instance (myOtherFragment) using:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.fragment_container, myOtherFragment).commit();

该myOtherFragment显示包含一个按钮,一个简单的观点。点击该按钮,然后myOtherFragment将被删除,并使用与myPagerFragment的新实例更换一次:

The myOtherFragment displays a simple view containing a button. When this button is clicked, then the myOtherFragment will be removed and replaced again with a new instance of myPagerFragment using:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.fragment_container, myPagerFragment).commit();

在这一点上myPagerFragment不再显示,因为它在一开始做,但相反,显示空白屏幕。有没有崩溃,没有什么有用的在日志中。

At this point the myPagerFragment no longer displays as it did in the beginning, but instead, displays a blank screen. There is no crash and nothing useful in the log.

我已经试过这种方法与其他意见,例如,与包含一个简单的视图定义一个片段取代myPagerFragment,这正常工作和预期。这仅仅是包含ViewPager,即在连续负载显示不正确的片段。

I have tried this method with other views, for instance, replacing myPagerFragment with a Fragment that contains a simple view definition, and this works correctly and as expected. It is only Fragments that contain a ViewPager, that displays incorrectly at successive loads.

我如何可以使包含ViewPager片段总是正确显示?

How can I make Fragments that contain a ViewPager to always display correctly?

推荐答案

您在片段不能嵌套片段。因此,你不能在片段 FragmentPagerAdapter ViewPager C>。请重新设计的用户界面解决这个矛盾,如摆脱的最外层片段

You cannot nest fragments in fragments. Hence, you cannot put a FragmentPagerAdapter in a ViewPager in a Fragment. Please redesign your UI to resolve this conflict, such as by getting rid of the outer-most Fragment.

这篇关于父片段不显示儿童ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆