简体   繁体   中英

Setting custom PagerAdapter in Container Activity to destroy a ViewPagers fragments

So I have an activity with a ViewPager in it. I want it to destroy all the ViewPager's Fragments on button press in the activity and update the ViewPager's Fragments with new text/Views.

I've written the code in the activity as below.

 SectionsPagerAdapter mSectionsPagerAdapter;

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), null);

    ViewPager mViewPager = (ViewPager)findViewById(R.id.vp_main);
      final PagerTabStrip strip = (PagerTabStrip)findViewById(R.id.pts_main);         
        mViewPager.setAdapter(mSectionsPagerAdapter);

        ArrayList<PagerItem> pagerItems = new ArrayList<PagerItem>();
        pagerItems.add(new PagerItem("Fragment1", new Fragment1())); //Error here when...
        pagerItems.add(new PagerItem("Fragment2", new Fragment2()));
        pagerItems.add(new PagerItem("Fragment1", new Fragment3()));
        pagerItems.add(new PagerItem("Fragment2", new Fragment4()));
        pagerItems.add(new PagerItem("Fragment1", new Fragment5()));
        pagerItems.add(new PagerItem("Fragment2", new Fragment6()));


      mSectionsPagerAdapter.setPagerItems(pagerItems); // I changed to my custom adapter here
      mSectionsPagerAdapter.notifyDataSetChanged();

ERROR MESSAGE IS... No enclosing instance of type SectionsPagerAdapter is accessible. Must qualify the allocation with an enclosing instance of type SectionsPagerAdapter (egxnew A() where x is an instance of SectionsPagerAdapter).

Can someone shed light on this issue?? Much appreciated

Make sure the class SectionsPagerAdapter exists and its PUBLIC and its constructor is PUBLIC.

Like this...

public class SectionsPagerAdapter {
      public SectionsPagerAdapter() {
      }
}

And replace this

new SectionsPagerAdapter(getSupportFragmentManager(), null);

with this

this.new SectionsPagerAdapter(getSupportFragmentManager(), null);

Or you can set SectionsPagerAdapter as STATIC. eg

public static class SectionsPagerAdapter {
      public SectionsPagerAdapter() {
      }
}

Good Luck. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM