Android Fragment使用viewPager adpter使用一个片段文件查看所有布局

时间:2018-07-22 12:12:04

标签: java android android-fragments android-viewpager

我想使用一个.java文件查看所有片段。所以当在onCreateView中放大时,我只能从bundle中获取布局键。但是当我在模拟器上对其进行刷卡操作时,有时它会保持相同的状态,有时会跳过该状态。 当我使用单独的Java文件实施时,它正在工作。

package com.example.android.viewpager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
    Bundle bundle = new Bundle();
    public SimpleFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        bundle.clear();
        if (position == 0) {
            WeekdayFragment mondayFragment = new WeekdayFragment();
            bundle.putInt("weekDay", R.layout.fragment_monday);
            mondayFragment.setBundle(bundle);
            return mondayFragment;

        } else if (position == 1) {
            WeekdayFragment tuesdayFragment = new WeekdayFragment();
            bundle.putInt("weekDay", R.layout.fragment_tuesday);
            tuesdayFragment.setBundle(bundle);
            return tuesdayFragment;

        } else if (position == 2) {
            WeekdayFragment wednesdayFragment = new WeekdayFragment();
            bundle.putInt("weekDay", R.layout.fragment_wednesday);
            wednesdayFragment.setBundle(bundle);
            return wednesdayFragment;
        } else if (position == 3) {
            WeekdayFragment thursdayFragment = new WeekdayFragment();
            bundle.putInt("weekDay", R.layout.fragment_thursday);
            thursdayFragment.setBundle(bundle);
            return thursdayFragment;
        } else {
            WeekdayFragment fridayFragment = new WeekdayFragment();
            bundle.putInt("weekDay", R.layout.fragment_friday);
            fridayFragment.setBundle(bundle);
            return fridayFragment;
        }
    }

    @Override
    public int getCount() {
        return 5;
    }
}

0 个答案:

没有答案