android - Getting the current Fragment instance in the viewpager -
below code has 3 fragment classes
each embedded each of 3 tabs on viewpager
. have menu option. shown in onoptionsitemselected()
, selecting option, need update fragment visible. update have call method in fragment class. can please suggest how call method?
public class mainactivity extends actionbaractivity { viewpager viewpager; tabsadapter tabsadapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); viewpager = new viewpager(this); viewpager.setid(r.id.pager); setcontentview(viewpager); final actionbar bar = getsupportactionbar(); bar.setnavigationmode(actionbar.navigation_mode_tabs); //attaching tabs fragment classes , setting tab title. tabsadapter = new tabsadapter(this, viewpager); tabsadapter.addtab(bar.newtab().settext("fragmentclass1"), fragmentclass1.class, null); tabsadapter.addtab(bar.newtab().settext("fragmentclass2"), fragmentclass2.class, null); tabsadapter.addtab(bar.newtab().settext("fragmentclass3"), fragmentclass3.class, null); if (savedinstancestate != null) { bar.setselectednavigationitem(savedinstancestate.getint("tab", 0)); } } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.addtext: **// here need call method exists in visible fragment class** return true; } return super.onoptionsitemselected(item); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("tab", getsupportactionbar().getselectednavigationindex()); } public static class tabsadapter extends fragmentpageradapter implements actionbar.tablistener, viewpager.onpagechangelistener { private final context mcontext; private final actionbar mactionbar; private final viewpager mviewpager; private final arraylist<tabinfo> mtabs = new arraylist<tabinfo>(); static final class tabinfo { private final class<?> clss; private final bundle args; tabinfo(class<?> _class, bundle _args) { clss = _class; args = _args; } } public tabsadapter(actionbaractivity activity, viewpager pager) { super(activity.getsupportfragmentmanager()); mcontext = activity; mactionbar = activity.getsupportactionbar(); mviewpager = pager; mviewpager.setadapter(this); mviewpager.setonpagechangelistener(this); } public void addtab(actionbar.tab tab, class<?> clss, bundle args) { tabinfo info = new tabinfo(clss, args); tab.settag(info); tab.settablistener(this); mtabs.add(info); mactionbar.addtab(tab); notifydatasetchanged(); } @override public void onpagescrollstatechanged(int state) { // todo auto-generated method stub } @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { // todo auto-generated method stub } @override public void onpageselected(int position) { // todo auto-generated method stub mactionbar.setselectednavigationitem(position); } @override public void ontabreselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } @override public void ontabselected(tab tab, fragmenttransaction ft) { object tag = tab.gettag(); (int i=0; i<mtabs.size(); i++) { if (mtabs.get(i) == tag) { mviewpager.setcurrentitem(i); } } tabposition = tab.getposition(); } @override public void ontabunselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } @override public fragment getitem(int position) { tabinfo info = mtabs.get(position); return fragment.instantiate(mcontext, info.clss.getname(), info.args); } @override public int getcount() { return mtabs.size(); } } }
suppose below fragment class method updatelist()
want call:
public class fragmentclass1{ arraylist<string> originaldata; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view fragmentview = inflater.inflate(r.layout.frag1, container, false); originaldata = getoriginaldatafromdb(); return fragmentview; } public void updatelist(string text) { originaldata.add(text); //here other ui part need added } }
by selecting option, need update fragment visible.
a simple way of doing using trick related fragmentpageradapter
implementation:
case r.id.addtext: fragment page = getsupportfragmentmanager().findfragmentbytag("android:switcher:" + r.id.pager + ":" + viewpager.getcurrentitem()); // based on current position can cast page correct // class , call method: if (viewpager.getcurrentitem() == 0 && page != null) { ((fragmentclass1)page).updatelist("new item"); } return true;
please rethink variable naming convention, using variable name name of class confusing(so no viewpager viewpager
, use viewpager mpager
example).
Comments
Post a Comment