android - Update Fragment View within FragmentPagerAdapter in SherlockFragmentActivity -
i following example fragmenttabspager under samples actionbarsherlock.
in case, have created simple sherlockactivity tabspager , same code sample fragmentpageradapter.
i created 2 fragments (different classes). first 1 contains 4 buttons. depending of 1 clicked, main activity gets code (e.g. 1,2,3,4). correctly code pressed button.
now, in second fragment want draw depending on code, or same, update dynamically view.
i have been searched solution haven´t got anything. in case have this:
[mainactivity.java]
public static class tabsadapter extends fragmentpageradapter implements tabhost.ontabchangelistener, viewpager.onpagechangelistener { private final context mcontext; private final tabhost mtabhost; private final viewpager mviewpager; private final arraylist<tabinfo> mtabs = new arraylist<tabinfo>(); [...] @override public fragment getitem(int position) { tabinfo info = mtabs.get(position); return fragment.instantiate(mcontext, info.clss.getname(), info.args); }
i managed update bundle info.args new value, don´t know how pass fragment correctly , order update dynamic view.
is possible?
thanks in advance.
update
finally, used sunny kindly explained. kept sparsearray fragments access them know position inside mainactivity:
[mainactivity.java]
//once user presses button on first fragment, callback calls // function public void onimageselected(int inumber){ mysecondfragment msf = (mysecondfragment)mtabsadapter.getfragment(pos_1); if(msf != null){ msf.setnumimage(inumber); msf.updateview(); } }
and in mysecondfragment.java can access layout components (e.g. gridview), initialized during oncreateview, put code needed inside updateview() method.
for instance, update content of gridview:
[mysecondfragment.java]
mygridview.setbackground(getresources().getdrawable(icode));
hth
update 2
i have included following code in order save fragments state bundle of main activity. when application changes portrait landscape or viceversa (it recreated) function getitem not being called, sparsearray map not refilled again necessary fragments:
@override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putstring(current_tab, mtabhost.getcurrenttabtag()); getsupportfragmentmanager().putfragment(outstate, myfirstfragment.class.getname(), mtabsadapter.getfragment(pos_1)); getsupportfragmentmanager().putfragment(outstate, mysecondfragment.class.getname(), mtabsadapter.getfragment(pos_2)); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); if (savedinstancestate != null) { myfirstfragment mff = (myfirstfragment) getsupportfragmentmanager().getfragment(savedinstancestate, myfirstfragment.class.getname()); if(mff != null) mtabsadapter.setfragment(pos_1, mff); mysecondfragment msf = (mysecondfragment) getsupportfragmentmanager().getfragment( savedinstancestate, mysecondfragment.class.getname()); if(msf != null) mtabsadapter.setfragment(pos_2, msf); mtabhost.setcurrenttabbytag(savedinstancestate .getstring(current_tab)); } }
also, inside tabsadapter class:
public fragment getfragment(int pos) { return map.get(pos); } public void setfragment (int position, fragment fragment) { map.put(position, fragment); }
hth
anhanniballake answer might work. might break in future.
to fragment can define sparesarray in tabadapter class
private sparsearray<fragment> map;
and add fragments in array
@override public fragment getitem(int position) { tabinfo info = mtabs.get(position); fragment fragment = fragment.instantiate(mcontext, info.clss.getname(), info.args); map.put(position, fragment); return fragment; }
you need remove fragment map when fragment destroy
@override public void destroyitem(viewgroup container, int position, object object) { map.remove(position); super.destroyitem(container, position, object); }
now define public method in tabadapter class can fragments
public fragment getfragment(int pos) { return map.get(pos); }
now in main sherlockfragementactivity can instance of fragment
myfragment f = (myfragment) mtabsadapter.getfragment(1); // 1 means second fragment left
now call method in fragments using fragment instance got above
f.doanythinginmysecondfragment();
doanythinginmysecondfragment
must declared public :)
Comments
Post a Comment