简体   繁体   English

避免在Viewpager中重新创建片段

[英]Avoid fragment recreation in viewpager

My application have an activity which contains Fragment with view pager. 我的应用程序有一个活动,其中包含带有视图寻呼机的Fragment。 View pager instantiate fragments, in which i load data from internet and display it. 查看寻呼机实例化片段,在其中我从Internet加载数据并显示它。 I have about 60 fragments always in view pager(its dates: today + 30 days before, 30 days after). 我总是在视图寻呼机中查看大约60个片段(其日期:今天+前30天,后30天)。

Problem is that when i changes orientation data begin download again. 问题是,当我更改方向数据时,将再次开始下载。 How to save state of this fragment? 如何保存此片段的状态?

Here is view pager adapter : 这是查看寻呼机适配器:

public class TimesheetPagerAdapter extends FragmentStatePagerAdapter {
    private DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    private DateFormat dateTitleFormatted = new SimpleDateFormat("MMM dd - EEEE", Locale.US);

    private ArrayList<Date> mDates;

    public TimesheetPagerAdapter(FragmentManager fm, ArrayList<Date> mDates) {
        super(fm);
        this.mDates = mDates;
    }

    @Override
    public Fragment getItem(int i) {
        return TimesheetPage.newInstance(dateFormat.format(mDates.get(i)));
    }

    @Override
    public int getCount() {
        return mDates.size();
    }

}

And ViewPager Fragment : 和ViewPager片段:

public static TimesheetPage newInstance(String page) {
        TimesheetPage TimesheetPage = new TimesheetPage();
        Bundle arguments = new Bundle();
        arguments.putString("date", page);
        TimesheetPage.setArguments(arguments);
        return TimesheetPage;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
        dateString = getArguments().getString("date");
        Random rnd = new Random();
        backColor = Color.argb(50, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_pager_timesheet, null);
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.llPageLayout);
        tvPage = (TextView) view.findViewById(R.id.tvAmountHours);
        mLoadingLayout = LoadingLayout.wrap(layout, android.R.attr.progressBarStyle);
        lvTimesheet = (ListView) view.findViewById(R.id.lvTimesheet);
        tvPage.setBackgroundColor(backColor);
        long id = getController().getUserAccount().getCompany().getCompanyId();
        mLoadingLayout.showLoading();
        Log.i("onCreateView", "TimesheetPage" + dateString);
        getController().getNetworkApi().getCompanyTimeSheet(id, dateString, timesheetCallback);
        return view;
    }

I would recommend you to let the fragment recreate, but save your network call in your application cache to avoid querying every time. 我建议您重新创建该片段,但将网络调用保存在应用程序缓存中以避免每次查询。 You can use a library like ion that use disk+memory cache: https://github.com/koush/ion 您可以使用像ion这样的库,该库使用磁盘+内存缓存: https : //github.com/koush/ion

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM