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; spinner spinner; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); pager = (viewpager) findviewbyid(r.id.viewpager); spinner = (spinner) findviewbyid(r.id.spinner); string[] language={"fr", "en", "sp"}; arrayadapter<string> stringarrayadapter=new arrayadapter<string> (this,android.r.layout.simple_spinner_dropdown_item,language); spinner.setadapter(stringarrayadapter); spinner.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parentview, view selecteditemview, int position, long id) { // code here if(spinner.getitematposition(spinner.getselecteditemposition()).tostring().equals("fr")) { } else if(spinner.getitematposition(spinner.getselecteditemposition()).tostring().equals("en")) { } else if(spinner.getitematposition(spinner.getselecteditemposition()).tostring().equals("sp")) { } } @override public void onnothingselected(adapterview<?> parentview) { // code here } }); 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); 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: navutils.navigateupto(this, new intent(this, mainactivity.class)); return true; case r.id.action_previous: pager.setcurrentitem(pager.getcurrentitem() - 1); return true; case r.id.action_next: 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; } } }
i gratefull if me. been more week , still no results
Comments
Post a Comment