更改屏幕旋转时,视图页导航片段消失

我正在开发包含s的Android应用程序,如下所示:ViewPagerFragment

MainActivity(MainActivityFragment(screenSlideViewPager(Fragments))),这意味着:

Activity 包含 包含 包含 sFragmentViewPagerFragment

现在我的问题是,当旋转设备或更改屏幕旋转时,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 Activity After Screen Rotation

显然,并且已加载,并且证明是 The ,注意也已加载,因为您仍然可以在页面之间导航(蓝色指示灯表示寻呼机到达最后一页),但您无法看到它的内容。MainActivityMainActivityFragmentActionBarViewPager


答案 1

看起来你的主要活动是使用getSupportFragmentManager(),而你的片段是使用getFragmentManager()。

不确定这是否值得一个答案帖子,但我的评分太低,无法以任何其他方式回复。:)

编辑:我相信您可能还需要使用支持库扩展 FragmentActivity。

请参见: https://stackoverflow.com/a/10609839/2640693

编辑 2:

public class MainActivityFragment extends FragmentActivity {
    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(getChildFragmentManager());
        mPager.setAdapter(mPageAdapter);
        return reVal;
    }

    private MainGridViewFragment mainGridFragment;
    private AlphabetGridViewFragment alphabetGridFragment;

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 1:
                    mainGridFragment = new MainGridViewFragment();
                    return (Fragment)mainGridFragment;
                case 0:
                    alphabetGridFragment = new AlphabetGridViewFragment();
                    return (Fragment)alphabetGridFragment;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
}

答案 2

我尝试了许多解决方案,但在任何一个解决方案中都取得了成功。最后,我修复了以下内容:

  • 使用 getActivity().getSupportFragmentManager() 而不是 getChildFragmentManager()

  • 覆盖 ViewPagerAdapter 类中的 getItemId(int position) 方法,如下所示:

    @Override
    public long getItemId(int position) {
        return System.currentTimeMillis();
    }
    

对我来说,它像魅力一样工作,也请在这里评论你的观点。


推荐