in application have 1 activity , have fragments , can switch between fragments without problem. 1 of fragments have viewpager
other fragments, after switch between parent fragments abd nested fragment viewpager, nested fragments dont show , must close application , run again show that.
main activity:
public void updatefragment(int selecteditem) { mfragment = null; fts = getsupportfragmentmanager().begintransaction(); switch (selecteditem) { case 0: mfragment = new fragmentmainview(); break; case 1: mfragment = new fragmentaddnewwaybill(); break; } fts.replace(r.id.mainviewfragments, mfragment, "0"); fts.commit(); mdrawerstate = false; }
in code can switch between fragments. fragmentmainview()
have viewpager defined fragments.
public class fragmentmainview extends fragment { private fragmentactivity mycontext; private viewpager vppager; private fragmentmanager fragmanager; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_main_view, container, false); g.currentactivity = fragmentmainview.this.getactivity(); fragmanager = mycontext.getsupportfragmentmanager(); vppager = (viewpager) view.findviewbyid(r.id.vppager); mypageradapter adapterviewpager = new mypageradapter(fragmanager); vppager.setadapter(adapterviewpager); return view; } public static class mypageradapter extends fragmentpageradapter { private static int num_items = 2; public mypageradapter(fragmentmanager fragmentmanager) { super(fragmentmanager); } // returns total number of pages @override public int getcount() { return num_items; } // returns fragment display page @override public fragment getitem(int position) { switch (position) { case 0: return fragmentone.newinstance(0); case 1: return fragmenttwo.newinstance(1); default: return null; } } // returns page title top indicator @override public charsequence getpagetitle(int position) { string tabheadertitle = ""; switch (position) { case 0: tabheadertitle = uc.getstring(r.string.waybill_register_package_owner); break; case 1: tabheadertitle = uc.getstring(r.string.create_new_waybill); break; } return tabheadertitle; } } @override public void onattach(activity activity) { mycontext=(fragmentactivity) activity; super.onattach(activity); } @override public void onresume() { super.onresume(); } }
in first switch on fragmentmainview()
in mainactivity
fragments on fragmentmainview()
viewpager work correctly , show, after switch on fragmentaddnewwaybill()
in mainactivity
, switch again fragmentmainview()
fragments on viewpager dont show , must close application , switch again show that. whats problem?
you have use getchildfragmentmanager()
instead of mycontext.getsupportfragmentmanager()
when using nested fragments.
Comments
Post a Comment