note: have implemented tabs using viewpager. creating application has 3 tabs(fragments) in viewpager. want create navigation drawer can see these 3 tabs , clicking on navigates me corresponding tab.
main activity extends actionbaractivity , has following code
private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { fragment fragment = new categoryfragment(); bundle args = new bundle(); args.putint(categoryfragment.category_number, position); fragment.setarguments(args); fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); transaction.replace(r.id.pager, fragment); transaction.addtobackstack(null); transaction.commit(); getsupportfragmentmanager().executependingtransactions(); // update selected item , title, close drawer mdrawerlist.setitemchecked(position, true); settitle(moptionsnavdrawer[position]); mdrawerlayout.closedrawer(mdrawerlist); }
categoryfragment looks
public static class categoryfragment extends android.support.v4.app.fragment { public static final string category_number = "category_number"; public categoryfragment() { // empty constructor required fragment subclasses } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { int fragmentnumber = getarguments().getint(category_number); view rootview=null; if(fragmentnumber==0) { rootview = inflater.inflate(r.layout.f1, container, false); } else if (fragmentnumber==1) { rootview = inflater.inflate(r.layout.f2, container, false); } else if(fragmentnumber==2) { rootview = inflater.inflate(r.layout.f3, container, false); } else { rootview = inflater.inflate(r.layout.f4, container, false); } return rootview; } } oncreateview function inside categoryfragment getting called fragments(f1. f2. f3 ) not getting set. there method of invoking these fragments? each of these fragments respective tabs.
you need method in activity helps switch between tabs. like:
public void displayview(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: fragment = new fragmenta(); break; case 1: fragment = new fragmentb(); break; case 2: fragment = new fragmentc(); break; default: break; } if (fragment != null) { fragmenttransaction transaction = fragmentmanager.begintransaction(); transaction.addtobackstack(null); transaction.replace(r.id.frame_container, fragment); transaction.commit(); each fragment should independent of each other, no need pass bundle info categoryfragment
Comments
Post a Comment