我对我创建的布局有疑问。我使用导航抽屉作为主要导航模式。它看起来像我想要的那样工作,但问题是在返回到保存ViewPager
的片段后 - 内部片段没有显示。但是,它们会在首次打开应用程序时显示,并默认显示ViewPager
- 暂挂Fragment
。
其他导航抽屉Fragments
显示正常,因此我不希望导航抽屉实施出现任何问题
RecordFragment.java(片段持有ViewPager
Fragments
):
public class RecordFragment extends Fragment {
private ViewPager mViewPager;
public RecordFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.record_fragment, container, false);
//getActivity().setTitle(R.string.record);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewPager = (ViewPager) view.findViewById(R.id.record_pager);
FragmentManager manager = getFragmentManager();
mViewPager.setAdapter(new MyFragmentPagerAdapter(manager));
}
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int item) {
Fragment fragment = null;
if (item == 0) {
fragment = new NumbersFragment();
} else if (item == 1) {
fragment = new MapFragment();
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
String title = new String();
if (position == 0) {
title = "Numbers";
} else if (position == 1) {
title = "Map";
}
return title;
}
}
}
NumbersFragment.java(Fragments
之一RecordFragment
public class NumbersFragment extends Fragment {
public NumbersFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.numbers_fragment, container, false);
//getActivity().setTitle(R.string.record);
return rootView;
}
}
record_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/record_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</LinearLayout>
答案 0 :(得分:5)
您在另一个Fragment
内使用Fragment
,在这种情况下,您需要使用Fragment#getChildFragmentManager()方法:
FragmentManager manager = getChildFragmentManager();
如果它不起作用,您可以尝试切换到FragmentStatePagerAdapter
而不是FragmentPagerAdapter
(但您仍需要使用子片段管理器)。
答案 1 :(得分:-1)
由于ViewPager在Fragment上需要Fragments和ViewPager,我们必须使用getChildFragmentManager()
而不是getSupportFragmentManager()
。