ViewPager 的 6 个片段 hided/shown 没有被解雇

6 Fragments hided/shown by ViewPager without being dismissed

我在 activity 中创建了 6 个片段 我想从一个滑动到另一个,需要它们不被解雇,但只需要 hide/show 和 viewpager。

有没有办法派生出 viewpager class 或 SectionsPagerAdapter 来做到这一点。

我已经添加了

mViewPager.setOffscreenPageLimit(6); 

但是没用。从第 4 个片段滑动到第 3 个或第 5 个片段时,应用程序崩溃。

我的代码的相关部分简化并重命名了 Fragment classes 以使代码可读:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // create the fragment once for all !
    if (savedInstanceState == null) {
        fragment1 = Fragment1.newInstance(1);
        fragment2 = Fragment2.newInstance(2);
        fragment3 = Fragment3.newInstance(3);
        fragment4 = Fragment4.newInstance(4);
        fragment5 = Fragment5.newInstance(5);
        fragment6 = Fragment6.newInstance(6);
     }
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(6);
    });


    public static class Fragment1 extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public Fragment1() {
    }

    public static Fragment1 newInstance(int sectionNumber) {
        Fragment1fragment = new Fragment1();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        fragment.setRetainInstance( true);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_1, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        // "Main Frag"
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        // The activity is about to become visible again, recalcule  
    }
}



   public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //return PlaceholderFragment.newInstance(position + 1);
        switch (position)
        {
            case 0 : return fragment1;   //instead  of Fragment1.newInstance(1);
            case 1 : return fragment2;
            case 2 : return fragment3; 
            case 3 : return fragment4;
            case 4 : return fragment5;
            case 5 : return fragment6;
            default: return null;
        }
    }

    @Override
    public int getCount() {
        // Show 6 total pages.
        return 6;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
            case 3:
                return "SECTION 4";
            case 4:
                return "SECTION 5";
            case 5:
                return "SECTION 6";
        }
        return null;
    }
}

通过 FragmentSatePagerAdaper 扩展您的部分寻呼机适配器 并设置 mViewPager.setOffscreenPageLimit(6);