更改屏幕旋转时,视图页导航片段消失
我正在开发包含s的Android应用程序,如下所示:ViewPager
Fragment
MainActivity
(MainActivityFragment
(screenSlideViewPager
(Fragment
s))),这意味着:
Activity
包含 包含 包含 sFragment
ViewPager
Fragment
现在我的问题是,当旋转设备或更改屏幕旋转时,ViewPager中的所有片段都消失了。
有什么想法来解决这个问题吗?
编辑 1
主要活动.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
final FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(android.R.id.content, new MainActivityFragment(), TAG);
ft.commit();
}
}
主要活动碎屑.java:
public class MainActivityFragment extends Fragment {
private ViewPager mPager = null;
private PagerAdapter mPageAdapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View reVal = inflater.inflate(R.layout.activity_main, container, false);
mPager = (ViewPager) reVal.findViewById(R.id.pagerMainContent);
mPageAdapter = new ScreenSlidePagerAdapter(getActivity().getFragmentManager());
mPager.setAdapter(mPageAdapter);
return reVal;
}
private MainGridViewFragment mainGridFragment;
private AlphabetGridViewFragment alphabetGridFragment;
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public android.app.Fragment getItem(int position) {
switch (position) {
case 1:
mainGridFragment = new MainGridViewFragment();
return mainGridFragment;
case 0:
alphabetGridFragment = new AlphabetGridViewFragment();
return alphabetGridFragment;
default:
return null;
}
}
@Override
public int getCount() {
return 2;
}
}
}
编辑 2
显然,并且已加载,并且证明是 The ,注意也已加载,因为您仍然可以在页面之间导航(蓝色指示灯表示寻呼机到达最后一页),但您无法看到它的内容。MainActivity
MainActivityFragment
ActionBar
ViewPager