Android ViewPager内部片段仅加载一次 [英] Android ViewPager Inside Fragment Loaded Only Once
问题描述
我在使用ViewPager时遇到问题.我正在使用FragmentActivity,并按照以下方法在FragmentActivity中添加了多个片段.
I am getting issue with ViewPager. I am using FragmentActivity and I added the multiple fragment inside FragmentActivity as following method.
public void addFragment(Fragment fragment, boolean addToBackStack, int transition) {
FragmentTransaction ft = getFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_up,R.anim.slide_down,R.anim.slide_up,R.anim.slide_down);
ft.replace(R.id.main, fragment);
ft.setTransition(transition);
if (addToBackStack)
ft.addToBackStack(null);
ft.commit();
}
我正在片段中使用ViewPager.加载FragmentActivity时,ViewPager工作正常.但是当我使用另一个片段时,ViewPager变得不可见.
I am using ViewPager Inside the fragment. ViewPager is working fine when FragmentActivity loaded. but when i use another fragment ViewPager got invisible.
例如
a.)片段A 与ViewPager
a.) Fragment A with ViewPager
b.)没有ViewPager的片段B
片段A 在我运行App时工作正常.当我转到 B片段时 然后我再次将片段B替换为片段A,然后ViewPager无法正常工作.我在使用ViewPager的地方看到空白视图.
Fragment A working fine when I run App. when i go to Fragment B then I again i replace Fragment B to Fragment A then ViewPager is not working. I see the blank view where I am using ViewPager.
请告诉我如何解决此问题
Please let me know How can i solve this issue
谢谢
推荐答案
我自己解决了这个问题,当您在片段中使用ViewPager
时,请使用getChildFragmentManager()
而不是getSupportFragmentManager()
或getFragmentManager()
.
I solved it myself the issue was when you use the ViewPager
inside the fragment use getChildFragmentManager()
instead of getSupportFragmentManager()
or getFragmentManager()
.
就是这样.
这篇关于Android ViewPager内部片段仅加载一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!