Android - Reload a Fragment that is Part of ViewPager -
i need reloading fragment part of viewpager. here current setup: reportactivity extends fragmentactivity , uses viewpager host 4 tabs tab 1 4. each tab fragment seperate layout.
viewpager uses tabspageradapter switch between tabs , works expected.
tab1fragment displays graph using static dataset. have added spinner dynamically change dataset , looking ways reload fragment can re-display graph correct dataset. following ints correspond values of spinner.
static final int report_period_day = 0; static final int report_period_week = 1; static final int report_period_month = 2; static final int report_period_year = 3;
i have method correctly calculates correct data based on the int passed in, struggling how create intent or other method recreate tab1fragment , passes int parameter.
here want re-create fragment
mreportperiodspinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { switch (position){ case 0: //how can re-load fragment here? reloadfragment(report_period_day); break; case 1: reloadfragment(report_period_week); break; case 2: reloadfragment(report_period_month); break; case 3: reloadfragment(report_period_year); break; } }
please can give me idea of how write code goes method reloadfragment(int period)
i recommend re-configuring existing fragment, rather constructing new fragment new report period.
i'm guessing configure graph inside tab1fragment's oncreateview. if move configuration code onresume, can call same function when user selects in spinner.
public class tab1fragment extends fragment { int reportperiod; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = ... reportperiod = report_period_day; // default report period return rootview; } @override public void onresume() { super.onresume(); configuregraph(); } private void configuregraph() { //todo put code draws graphs here, using reportperiod } public void reloadfragment(int reportperiod) { this.reportperiod = reportperiod; configuregraph(); } }
if approach doesn't work you, , need new fragment each time, question explains how tag fragment can tell fragmentmanager replace it: replace fragment inside viewpager
Comments
Post a Comment