java - How do I use the navigation drawer to select a viewpager -


i'm new android app development , i'm working on project school.

from understanding far, can use navigation drawer swap out ui fragments. made nav drawer that's working far based on tutorial given here:

https://developer.android.com/training/implementing-navigation/nav-drawer.html

i'm trying have switch views each item that's selected. made view pager fragment activity , want program replace view view pager upon selecting 4th item in nav drawer (index 3). added switch statement in fragment class determine layout load.

here's activity nav drawer:

package com.mkge.mkg;  import android.content.res.configuration; import android.os.bundle; import android.support.v4.app.actionbardrawertoggle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.widget.drawerlayout; import android.view.layoutinflater; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview;  public class mainactivity extends fragmentactivity {     private drawerlayout mdrawerlayout;     private listview mdrawerlist;     private actionbardrawertoggle mdrawertoggle;      private charsequence mtitle;     private string[] mpagetitles;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          mtitle = gettitle();         mpagetitles = getresources().getstringarray(r.array.nav_draw_array);         mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout);         mdrawerlist = (listview) findviewbyid(r.id.left_drawer);          mdrawerlist.setadapter(new arrayadapter<string>(this, r.layout.drawer_list_item, mpagetitles));         mdrawerlist.setonitemclicklistener(new draweritemclicklistener());          getactionbar().setdisplayhomeasupenabled(true);         getactionbar().sethomebuttonenabled(true);          mdrawertoggle = new actionbardrawertoggle(                 this,                 mdrawerlayout,                 r.drawable.ic_drawer,                 r.string.drawer_open,                 r.string.drawer_close                 ) {             public void ondrawerclosed(view view) {                 getactionbar().settitle(mtitle);                 invalidateoptionsmenu();             }              public void ondraweropened(view drawerview) {                 getactionbar().settitle(r.string.drawer_open);                 invalidateoptionsmenu();             }         };         mdrawerlayout.setdrawerlistener(mdrawertoggle);          if (savedinstancestate == null) {             selectitem(0);         }     }      @override     public boolean oncreateoptionsmenu(menu menu) {         menuinflater inflater = getmenuinflater();         inflater.inflate(r.menu.main, menu);         return super.oncreateoptionsmenu(menu);     }      @override     public boolean onoptionsitemselected(menuitem item) {         if (mdrawertoggle.onoptionsitemselected(item)) return true;         return super.onoptionsitemselected(item);     }      private class draweritemclicklistener implements listview.onitemclicklistener {         @override         public void onitemclick(adapterview<?> parent, view view, int position, long id) {             selectitem(position);         }     }      private void selectitem(int position) {         fragment fragment = new pagefragment();         bundle args = new bundle();         args.putint(pagefragment.arg_page_number, position);         fragment.setarguments(args);          fragmentmanager fragmentmanager = getsupportfragmentmanager();         fragmentmanager.begintransaction().replace(r.id.content_frame, fragment).commit();          mdrawerlist.setitemchecked(position, true);         settitle(mpagetitles[position]);         mdrawerlayout.closedrawer(mdrawerlist);     }      public static class pagefragment extends fragment {         public static final string arg_page_number = "page_number";          public pagefragment() {         }          @override         public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {             int = getarguments().getint(arg_page_number);             view rootview;             switch(i) {             case 0:                 rootview = inflater.inflate(r.layout.page_home, container, false);                 break;             case 1:                 rootview = inflater.inflate(r.layout.page_services, container, false);                 break;             case 2:                 rootview = inflater.inflate(r.layout.page_packages, container, false);                 break;             case 3:             view view = new meetviewpager().getviewpager();             rootview = view;             break;             case 4:                 rootview = inflater.inflate(r.layout.page_about, container, false);                 break;             case 5:                 rootview = inflater.inflate(r.layout.page_contact, container, false);                 break;             case 6:                 rootview = inflater.inflate(r.layout.page_extras, container, false);                 break;             default:                 rootview = null;             }              string page = getresources().getstringarray(r.array.nav_draw_array)[i];             getactivity().settitle(page);             return rootview;         }     }      @override     public void settitle(charsequence title) {         mtitle = title;         getactionbar().settitle(mtitle);     }      @override     protected void onpostcreate(bundle savedinstancestate) {         super.onpostcreate(savedinstancestate);         mdrawertoggle.syncstate();     }      @override     public void onconfigurationchanged(configuration newconfig) {         super.onconfigurationchanged(newconfig);         mdrawertoggle.onconfigurationchanged(newconfig);     } } 

i made fragment activity here create view pager. tried taking view pager object made here , passing switch statement nav drawer activity.

package com.mkge.mkg;  import java.util.arraylist; import java.util.list;  import android.annotation.suppresslint; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup;  @suppresslint("validfragment")     public class meetviewpager extends fragmentactivity {         public viewpager mviewpager;         public fragmentpageradapter mviewpageradapter;          public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.page_meet);             list<fragment> fragments = getfragments();             mviewpager = (viewpager) findviewbyid(r.id.pager);             mviewpageradapter = new mpageadapter(getsupportfragmentmanager(), fragments);             mviewpager.setadapter(mviewpageradapter);         }          public view getviewpager() {             return mviewpager;         }          private list<fragment> getfragments() {             list<fragment> flist = new arraylist<fragment>();             fragment fragment = new mfragment();             bundle args = new bundle();             args.putint("position", 0);             fragment.setarguments(args);             flist.add(fragment);              fragment = new mfragment();             args = new bundle();             args.putint("position", 1);             fragment.setarguments(args);             flist.add(fragment);              return flist;         }          private class mpageadapter extends fragmentpageradapter {             private list<fragment> fragments;              public mpageadapter(fragmentmanager f, list<fragment> fragments) {                 super(f);                 this.fragments = fragments;             }              @override             public fragment getitem(int position) {                 return this.fragments.get(position);             }              @override             public int getcount() {                 return this.fragments.size();             }         }          @suppresslint("validfragment")         public static class mfragment extends fragment {              public mfragment() {             }              public static final mfragment newinstance(int i) {                 mfragment f = new mfragment();                 bundle args = new bundle(1);                 args.putint("position", i);                 f.setarguments(args);                 return f;             }              public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {                 view v;                 int = getarguments().getint("postion");                 switch(i) {                 case 0:                     v = inflater.inflate(r.layout.page_meet_0, container, false);                     break;                 case 1:                     v = inflater.inflate(r.layout.page_meet_1, container, false);                     break;                 default:                     v = null;                 }                 return v;             }         }     } 

any appreciated thanks.

mviewpager.setcurrentitem(0);  

this should need make viewpager change first tab.

this called inside selectitem() method.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -