android - Update fragments in viewpager when a Spinner item is selected -
my goal create simple, multi-language application uses viewpager
display fragments
. have created :
-"frensh_frag1,frensh_frag2,frensh_frag3 " french fragments
-"english_frag1,english_frag3,english_frag3 " english fragments
what want add spinner displays languages (fr, en), when choose "en" viewpager
should display english fragments. how implement spinner code?! code far :
public class mainactivity extends fragmentactivity { private viewpager pager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string[] language={"fr", "en", "sp"}; arrayadapter<string> stringarrayadapter=new arrayadapter<string> (this,android.r.layout.simple_spinner_dropdown_item,language); spinner spinner = (spinner) findviewbyid(r.id.spinner); spinner.setadapter(stringarrayadapter); spinner.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parentview, view selecteditemview, int position, long id) { // code here } @override public void onnothingselected(adapterview<?> parentview) { // code here } }); pager = (viewpager) findviewbyid(r.id.viewpager); pager.setadapter(new mypageradapter(getsupportfragmentmanager())); pager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { invalidateoptionsmenu(); } }); } public boolean oncreateoptionsmenu(menu menu) { super.oncreateoptionsmenu(menu); getmenuinflater().inflate(r.menu.activity_screen_slide, menu); menu.finditem(r.id.action_previous).setenabled(pager.getcurrentitem() > 0); // add either "next" or "finish" button action bar, depending on page // selected. menuitem item = menu.add(menu.none, r.id.action_next, menu.none, (pager.getcurrentitem() == pager.getadapter().getcount() - 1) ? r.string.action_finish : r.string.action_next); item.setshowasaction(menuitem.show_as_action_if_room | menuitem.show_as_action_with_text); return true; } public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: // navigate "up" demo structure launchpad activity. // see http://developer.android.com/design/patterns/navigation.html more. navutils.navigateupto(this, new intent(this, mainactivity.class)); return true; case r.id.action_previous: // go previous step in wizard. if there no previous step, // setcurrentitem nothing. pager.setcurrentitem(pager.getcurrentitem() - 1); return true; case r.id.action_next: // advance next step in wizard. if there no next step, setcurrentitem // nothing. pager.setcurrentitem(pager.getcurrentitem() + 1); return true; } return super.onoptionsitemselected(item); } private class mypageradapter extends fragmentpageradapter { public mypageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int pos) { switch(pos) { case 0: return fr_firstfragment.newinstance("page : 1"); case 1: return fr_secondfragment.newinstance("page : 2"); case 2: return fr_thirdfragment.newinstance("page : 3"); case 3: return fr_fourthfragment.newinstance("page : 4"); default: return null; } } @override public int getcount() { return 4; } } }
Comments
Post a Comment