Parent Fragment Not Displaying Child ViewPager

亡梦爱人 提交于 2019-12-24 10:46:50

问题


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();

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();

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();

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.

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.

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


回答1:


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.



来源:https://stackoverflow.com/questions/9600297/parent-fragment-not-displaying-child-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!