How To Refresh The Viewpager For Every Tab?
I'm using ViewPager as a main layout and for individual pager views I use Fragments. The tabs are part of Action Bar. I wanna refresh the current tab with the refresh-button in my
Solution 1:
One way is to find the selected tab in the action bar, cast it to its type, and call a method on it.
That is a dependency you might want to avoid.
I prefer to use broadcasts for this scenario. Register a broadcast receiver in the fragments onResume method, unregister it in onPause. Then let a context broadcast an intent for a refresh and the fragment will do that.
This will let you refresh the fragments from everywhere in your application. Even if the refreshing code ends up in a service running in the background.
Update:
You will have some code like this anywhere you want (this
is s context):
LocalBroadcastManagerlbm= LocalBroadcastManager.getInstance(this);
Intenti=newIntent(REFRESH_CONSTANT);
lbm.sendBroadcast(i);
Then in your fragment you listen for this broadcast:
publicclassMyFragmentextendsFragment {
MyReceiver r;
publicvoidrefresh() {
// Do the refresh
}
publicvoidonPause() {
LocalBroadcastManager.getInstance(mContext).unregisterReceiver(r);
}
publicvoidonResume() {
r = newMyReceiver ();
LocalBroadcastManager.getInstance(mContext).registerReceiver(r,
newIntentFilter(REFRESH_CONSTANT));
}
privateclassMyReceiverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
MyFragment.this.refresh();
}
}
}
Post a Comment for "How To Refresh The Viewpager For Every Tab?"