android - How update a fragment in viewpager tab? -
i have created fragmentactivity whith eclipse show actionbar tabs. in first tab show fragment list, , in next tab fragment text. want that, when selected 1 item of first tab, change second fragment list fragment. here code:
tabactivity created eclipse , me:
public class tabactivity extends fragmentactivity implements actionbar.tablistener { static final int num_tabs = mytab.values().length; public enum mytab{ categorias(0), productos(1); private final int index; mytab(int i) { this.index = i; } } sectionspageradapter msectionspageradapter; viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_pedido); // set action bar. final actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); msectionspageradapter = new sectionspageradapter( getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); // when swiping between different sections, select corresponding // tab. can use actionbar.tab#select() if have // reference tab. mviewpager .setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); } }); (int = 0; < msectionspageradapter.getcount(); i++) { actionbar.addtab(actionbar.newtab() .settext(msectionspageradapter.getpagetitle(i)) .settablistener(this)); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.pedido, menu); return true; } @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { mviewpager.setcurrentitem(position); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } @override public void ontabreselected(tab tab, fragmenttransaction ft) { } /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ public class sectionspageradapter extends fragmentpageradapter { private fragmentmanager mfragmentmanager; private textofragment mtextofragment; public sectionspageradapter(fragmentmanager fm) { super(fm); mfragmentmanager = fm; } @override public fragment getitem(int position) { return tabfragment.nuevofragment(position); } @override public int getcount() { return num_tabs; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); switch (mytab.values()[position]) { case categorias: return getstring(r.string.title_categorias).touppercase(l); case productos: return getstring(r.string.title_productos).touppercase(l); } return ""; } } /** * dummy fragment representing section of app, * displays dummy text. */ public static class tabfragment extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; static fragment nuevofragment(int position) { fragment f; bundle args = new bundle(); switch (pestaña.values()[position]) { case categorias: f = new listafragment(); args.putint("tipoobjeto", tipoobjeto.categoria.ordinal()); break; case productos: if (estadoht.categoriaactual == null) { f = new textofragment(); args.putstring(textofragment.arg_mensaje, "elija primero una categoría"); } else { f = new listafragment(); args.putint(listafragment.arg_tipo_objeto, tipoobjeto.producto.ordinal()); } break; default: f = new tabfragment(); args = new bundle(); args.putint(tabfragment.arg_section_number, position); } f.setarguments(args); return f; } private view lanzarestadopedido(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.tab_estado_pedido, container, false); // muestra el layout textview lblmesa = (textview) rootview.findviewbyid(r.id.lblmesa); lblmesa.settext("nada"); return rootview; } public tabfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { int valorpestaña = getarguments().getint(arg_section_number, 0); switch (mytab.values()[valorpestaña]) { case pedido: return lanzarestadopedido(inflater, container, savedinstancestate); default: return null; } } } }
in listafragment:
public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3) { intent dato = new intent(); iobjetoht o = (iobjetoht) lvlista.getitematposition(position); switch (tipo) { case categoria: app.categoriaactual = (categoria) o; tabactivity actpedido = (tabactivity) getactivity(); actionbar actionbar = actpedido.getactionbar(); actpedido.mviewpager.setcurrentitem(tabactivity.mytab.productos.ordinal()); break; case producto: break; default: dato.putextra(arg_objeto_a_devolver, o); act.setresult(act.result_ok, dato); act.finish(); }
then listafragment want upload second tab fragment.. possible?
you can use fragment manager keep track of fragments being added tabpageradapter.
public class mainactivity extends fragmentactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tabadapter = new tabpageradapter(getsupportfragmentmanager(), context); tab = (viewpager)findviewbyid(r.id.pager); tab.setonpagechangelistener( new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar = getactionbar(); actionbar.setselectednavigationitem(position); if(position == 1) { android.support.v4.app.fragmentmanager fragmentmanager = getsupportfragmentmanager(); **myactivity fragment = (myactivity) fragmentmanager.findfragmentbytag("your tag 1");** fragment.get_recent(); toast.maketext(context, "im visible", toast.length_long).show(); } } }); tab.setadapter(tabadapter); actionbar = getactionbar(); //enable tabs on action bar actionbar.setnavigationmode(actionbar.navigation_mode_tabs); actionbar.addtab(actionbar.newtab().settext("tab name 1").settablistener(tablistener)); actionbar.addtab(actionbar.newtab().settext("tab name 2").settablistener(tablistener)); actionbar.addtab(actionbar.newtab().settext("tab name 3").settablistener(tablistener)); android.support.v4.app.fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmentmanager.begintransaction().add(tabadapter.getitem(0), "your tag 1").commit(); fragmentmanager.begintransaction().add(tabadapter.getitem(1), "your tag 2").commit(); fragmentmanager.begintransaction().add(tabadapter.getitem(2), "your tag 3").commit(); }
public class tabpageradapter extends fragmentstatepageradapter { private context context; public tabpageradapter(fragmentmanager fm, context context) { super(fm); this.context = context; // todo auto-generated constructor stub } @override public fragment getitem(int i) { switch (i) { case 0: return new youractivity1(); case 1: return new youractivity2(); case 2: return new youractivity3(); } return null; } @override public int getcount() { // todo auto-generated method stub return 3; //no of tabs } }
be careful if you're using actionbarsherlock because need explicitly state using android.support.v4.app.fragmentmanager.
Comments
Post a Comment