删除FragmentStatePager中的片段?

时间:2015-11-03 08:22:32

标签: android

我正在使用materialviewpager库。我有几个片段,在它们里面是网格视图。当您选择其中一个时,在同一个活动中会有一个新片段替换之前的片段。我通过创建如下函数来完成这项工作。

public  void initialViewPager(final String extra)
    {
 mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager(),extra));

......
    }

并且在带有switch case的getItem方法中返回一个新片段。

运行几次后出现此错误: Throwing OutOfMemoryError "Failed to allocate a 279372 byte allocation with 216600 free bytes and 211KB until OOM

如何在不将片段存储在内存中的情况下编写适配器?或者如何解决这个问题?

MainActivity类如下:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity);
    ButterKnife.inject(this);



    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(null);

    }



    View guillotineMenu = LayoutInflater.from(this).inflate(R.layout.guillotine, null);
    root.addView(guillotineMenu);


    new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(R.id.guillotine_hamburger), contentHamburger)
            .setStartDelay(RIPPLE_DURATION)
            .setActionBarViewForAnimation(toolbar)
            .build();

    if (!BuildConfig.DEBUG)
        Fabric.with(this, new Crashlytics());



    mViewPager = (MaterialViewPager) findViewById(R.id.materialViewPager);
    if(getIntent().getStringExtra("query")!=null) {
        extra2= getIntent().getStringExtra("query");
        if(getIntent().getStringExtra("query")!=null) {
            tab_name = getIntent().getStringExtra("tabname");
        }
    }else{extra2="7";}


    initialViewPager(extra2);

public  void initialViewPager(final String extra)
{


    mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {

        int currentPage = 0;

        private SparseArray<Fragment> mPageReferenceMap = new SparseArray<Fragment>();
        int pos;


        @Override
        public Fragment getItem(int position) {
            switch (position % 3) {
                case 0:
                    pos=0;

                    if( extra.equals("8")||extra.equals("9")){
                        RuleItem.back_frament="Rule";
                        RuleItem.level=1;
                        myFragment = RuleItem.newInstance("select * from rules" +
                                " where category='" + String.valueOf((int) (Math.floor(Integer.parseInt(extra)))) + "'",MainActivity.this);
                        mPageReferenceMap.put(0, myFragment);
                        mFragments.add(new WeakReference<Fragment>(myFragment));
                        return myFragment;

                    }
                    if(extra.equals("exam")){
                        myFragment = Exam.newInstance();
                        mPageReferenceMap.put(0, myFragment);
                        mFragments.add(new WeakReference<Fragment>(myFragment));
                        return myFragment;

                    }

                    if(extra.equals("Rule")){
                        myFragment =Rule.newInstance(MainActivity.this);
                        mPageReferenceMap.put(0, myFragment);
                        mFragments.add(new WeakReference<Fragment>(myFragment));
                        return myFragment;

                    }
                    if(extra.equals("0") ||extra.equals("1")||extra.equals("2")||extra.equals("3")
                            ||extra.equals("4")||extra.equals("5")||extra.equals("6")||
                            extra.equals("10")|| extra.equals("7")||extra.equals("71") ||
                            extra.equals("72")|| extra.equals("05")||extra.equals("08")
                            || extra.equals("36") || extra.equals("37")) {

                        ///////for back bt
                        if(extra.equals("7")){

                            Slider.back_frament="Rule";
                            Slider.level=1;
                            myFragment =Slider.newInstance(extra, MainActivity.this);
                            mPageReferenceMap.put(0, myFragment);
                            mFragments.add(new WeakReference<Fragment>(myFragment));
                            return myFragment;


                        }
                        if(extra.equals("0") ||extra.equals("1")||extra.equals("2")||extra.equals("3")
                                ||extra.equals("4")||extra.equals("5")||extra.equals("6")
                                ||extra.equals("10")){

                            Slider.back_frament="Rule";
                            Slider.level=1;
                            myFragment =Slider.newInstance(extra, MainActivity.this);
                            mPageReferenceMap.put(0, myFragment);
                            mFragments.add(new WeakReference<Fragment>(myFragment));
                            return myFragment;

                        }
                        if(extra.equals("71") ||
                                extra.equals("72")|| extra.equals("05")||extra.equals("08")
                                || extra.equals("36") || extra.equals("37")){
                            back_fragment="Rule";
                            level=1;
                            Slider.back_frament="7";
                            Slider.level=1;

                            myFragment =Slider.newInstance(extra, MainActivity.this);
                            mPageReferenceMap.put(0, myFragment);
                            mFragments.add(new WeakReference<Fragment>(myFragment));
                            return myFragment;

                        }

                    }

                case 1:
                    pos=1;
                    if( extra.equals("8")||extra.equals("9")){

                        myFragment =QuestionPage.newInstance("select * from exam" +
                                " where " + exam.KEY_TYPE + "='" + extra + "'",MainActivity.this);

                        mPageReferenceMap.put(1, myFragment);
                        mFragments.add(new WeakReference<Fragment>(myFragment));
                        return myFragment;
                    }
                    if(extra.equals("7")  ){
                        myFragment =RuleItem.newInstance("select * from rules" +
                                " where category='" + String.valueOf((int) (Math.floor(Integer.parseInt("7")))) + "'",MainActivity.this);
                        mPageReferenceMap.put(1, myFragment);
                        mFragments.add(new WeakReference<Fragment>(myFragment));
                        return myFragment;

                    }
                case 2:
                    pos=2;
                    if(extra.equals("7")  ) {
                        myFragment =QuestionPage.newInstance("select * from exam" +
                                " where " + exam.KEY_TYPE + "='" + "7" + "'",MainActivity.this);

                        mPageReferenceMap.put(1, myFragment);
                        mFragments.add(new WeakReference<Fragment>(myFragment));
                        return myFragment;

                    }
                default:
                    return RecyclerViewFragment.newInstance();

            }
        }

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



        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            super.destroyItem(container, position, object);

            mPageReferenceMap.remove(Integer.valueOf(position));
        }

        public Fragment getCurrentItem() {
            return mPageReferenceMap.get(currentPage);
        }
    });



    mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount());
    mViewPager.getPagerTitleStrip().setViewPager(mViewPager.getViewPager());
    View logo = findViewById(R.id.logo_white);
    if (logo != null)
        logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.notifyHeaderChanged();
                Toast.makeText(getApplicationContext(), "Yes, the title is clickable", Toast.LENGTH_SHORT).show();
            }
        });
    mViewPager.notifyHeaderChanged();
}





}

0 个答案:

没有答案