我的问题
根据 Google 的文档:
You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page. To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions. For example, here’s some code that adds a fragment from within an existing Fragment class:
Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();
所以我创建了自己的 PageFragment
。而我的 PageFragmentAdapter
由 3 页 PageFragment
组成。
我还有六个 fragment :Fragment1
、Fragment2
、Fragment3
、FragmentA
、FragmentB
, FragmentC
。
在我的 MainActivity
中,我初始化了我的六个 fragment ,我的 ViewPager
及其 PageFragmentAdapter
(它为自己初始化了三个 PageFragment
) .
然后我使用此方法将我的 Fragment1
、Fragment2
、Fragment3
附加到每个页面(到每个 PageFragment
) PageFragmentAdapter
:
PageFragmentAdapter tPageFragmentAdapter = new PageFragmentAdapter(getSupportFragmentManager());
tPageFragmentAdapter.setFirstPage(tFragment1);
tPageFragmentAdapter.setSecondPage(tFragment2);
tPageFragmentAdapter.setThirdPage(tFragment3);
我可以接收(通过 PageFragmentAdapter
中的方法) fragment 并将“嵌套 fragment ”放入我的 PageFragment
(当然是在 onCreate()
中)像:
getChildFragmentManager().beginTransaction()
.add(R.id.framelayout_history, mFragment)
.addToBackStack(null)
.commit();
注意:我使用 PageFragment.setNestedFragment()
在其中设置 mFragment
。
而且效果很好! 它将帮助我(但这个故事不是关于它的)简单地替换 ViewPager 中的 fragment ,例如:
tPageFragmentAdapter.replaceFirstPage(tFragmentA);
但我有一个大问题。 再次谷歌的文档:
Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page. This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state.
如您所见,PageFragmentAdapter
可以破坏每个 my PageFragment
,当然还有它的嵌套 fragment 。当它被重新创建时,它将有一个 NULL 而不是 mFragment
值。
我试图通过 getChildFragmentManager().putFragment()
保存它,然后通过 getChildFragmentManager().getFragment()
获取它。但它不起作用。
问题
所以我的问题是:如何将我的嵌套 fragment 保存在我的父 fragment 中?可能吗?
非常感谢您的帮助。亚历克斯。附言真的很抱歉我的英语:)
编辑
感谢@AndroidBegin.com 的回答!
This seems to be a bug in the newly added support for nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity. A short-term workaround that fixed it for me is to add the following to onDetach() of every Fragment which you call getChildFragmentManager() on:
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
最佳答案
在你的 fragment 上试试这个。
public class Fragment2 extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.viewpager_main, container, false);
// Locate the ViewPager in viewpager_main.xml
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
// Set the ViewPagerAdapter into ViewPager
mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
return view;
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
关于android - 带有嵌套 fragment 的 ViewPager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18977923/