一、先贴两段文档说明,一目了然
- getFragmentManager()
android.support.v4.app.Fragment
@Contract(pure=true)
public final android.support.v4.app.FragmentManager getFragmentManager()
Return the FragmentManager for interacting with fragments associated with this fragment's activity.
Note that this will be non-null slightly before getActivity(),
during the time from when the fragment is placed in a FragmentTransaction until it is committed and attached to its activity.
If this Fragment is a child of another Fragment,
the FragmentManager returned here will be the parent's getChildFragmentManager().
- getChildFragmentManager()
android.support.v4.app.Fragment
public final android.support.v4.app.FragmentManager getChildFragmentManager()
Return a private FragmentManager for placing
and managing Fragments inside of this Fragment.
二、Fragment+ViewPager-->+多个fragment
fragment跳转到带有viewpager的fragment,第一次能够滑动显示多个fragment,退出后再进入,viewpager不显示。
问题就在 new ViewPageAdpater( getFragmentManager() )上面,两段文档已经解决问题。
错误代码
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mSectionsPagerAdapter.addFragments(SongDetailAboutFragment.newInstance(0));
mSectionsPagerAdapter.addFragments(SongDetailAlbumFragment.newInstance(0));
mSectionsPagerAdapter.addFragments(SongDetailLrcFragment.newInstance(0));
viewPager.setAdapter(mSectionsPagerAdapter);
正确代码
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
mSectionsPagerAdapter.addFragments(SongDetailAboutFragment.newInstance(0));
mSectionsPagerAdapter.addFragments(SongDetailAlbumFragment.newInstance(0));
mSectionsPagerAdapter.addFragments(SongDetailLrcFragment.newInstance(0));
viewPager.setAdapter(mSectionsPagerAdapter);