android - Refresh tab fragment page on scroll -
i have 3 fragment tabs in viewpager.
fragment1 contains listfragment , fragment2 contains listfragment.
after clicking item in fragment2, should go fragment1 , refresh list. (the list populated via web service using gson). fragment2 passes value fragment1 can reload according parameter carried along.
i've tried many ways refresh/update state of list in fragment1 including reloading whole fragment1 oncreateview
, using fragmenttransaction
, calling host activity
within itself. still couldn't find way make it.
also noticed upon scrolling tab 3 , tab 1, refreshes it. doesn't refresh list tab 2 tab 1. read solution in suggests set viewpager.setoffscreenpagelimit(0);
, still problem persists.
here code currently.
in host activity (usershopactivity
), i've implemented interface listener fragment2 (usercategoriesfragment
) obtain category_id
passed fragment1 (useritemsfragment
):
public class usershopactivity extends fragmentactivity implements usercategoriesfragment.onusercategoriesselectedlistener { ... @override public void onusercategoriesselected(string category_id) { settabuseritems(category_id); string useritemsfragment = gettabuseritems(); useritemsfragment uif = (useritemsfragment) this.getsupportfragmentmanager().findfragmentbytag("android:switcher:"+r.id.pager+":0"); if (uif == null) { setsavetouseritems(category_id); log.d("useritemfragment: ", "null"); } else { setsavetouseritems(category_id); uif.updatecat(category_id); log.d("cat id sent: ", useritemsfragment); mviewpager.setcurrentitem(0); } } }
i have passed category_id
. upon calling setcurrentitem(0)
, goes fragment1. want reload list. how?
in tabadapter, i've implemented viewpager.onpagechangelistener
, override onpageselected
method. believe should put refresh coding.
i've tried refresh fragment2 in method so:
@override public void onpageselected(int i) { log.d("onpageselected: ", + ""); mactionbar.setselectednavigationitem(i); if(i == 0) { useritemsfragment uif = new useritemsfragment(); uif.reload(); } }
and in reload()
like:
public void reload(){ // re-call fragment within }
however, got nullpointerexception
error :(
can point me did wrong?
try this:
@override public void onpageselected(int i) { log.d("onpageselected: ", + ""); mactionbar.setselectednavigationitem(i); if(i == 0) { useritemsfragment frg = (useritemsfragment)madapter.instantiateitem(viewpager, i); frg.reload(); } }
Comments
Post a Comment