Reload a Fragment that is Part of ViewPager

I need help with reloading a Fragment that is part of a ViewPager. Here is my current setup: ReportActivity extends FragmentActivity and uses ViewPager to host 4 Tabs TAB 1 to 4. Each Tab is a Fragment with seperate layout.

ViewPager uses TabsPagerAdapter to switch between the tabs and this works as expected.

TAB1Fragment displays a graph using a static dataset. Now I have added a spinner to dynamically change the dataset and I am looking for ways to reload the Fragment so it can re-display the graph with the correct dataset. The following ints correspond to the values of the 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 a method that correctly calculates the correct data based on the the int passed in, what I am struggling with is how to create an Intent or any other method that will recreate TAB1Fragment and passes it an int parameter.

Here is where I want to re-create the Fragment

        mReportPeriodSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            switch (position){
                case 0:
                    //How can I re-load the 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 you give me an idea of how to write the code that goes into the method reloadFragment(Int period)


I would recommend re-configuring the existing fragment, rather than constructing a new fragment with the new report period.

I'm guessing that you currently configure your graph inside TAB1Fragment's onCreateView. If you move that configuration code to onResume, then you can call the same function when the user selects something in the 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 all the code that draws your graphs here, using reportPeriod
    }

    public void reloadFragment(int reportPeriod) {
        this.reportPeriod = reportPeriod;
        configureGraph();
    }
}

If this approach really doesn't work for you, and you really need a new fragment each time, then this question explains how to get the tag for a fragment so that you can tell the FragmentManager to replace it: Replace Fragment inside a ViewPager

链接地址: http://www.djcxy.com/p/79056.html

上一篇: Heroku上的Rails应用程序无法写入PostgreSQL数据库,只能读取

下一篇: 重新加载属于ViewPager的片段