我有一个旋转器,其值从第1周到第12周。在从微调器中选择任何一周时,7个片段(从星期日到星期一)被加载到我的viewpager和tablayout中。唯一的区别在于每周和每天的数据。
现在问题是每周都会在周日片段中填充正确的数据。当我滑到星期一片段时,填充的数据是星期二,依此类推。
以下是我的viewpagerAdapter类
viewPager = (ViewPager) mView.findViewById(R.id.viewpager);
setupViewPager(viewPager, 1);
TabLayout mTabLayout = (TabLayout) mView.findViewById(R.id.tabs);
mTabLayout.setupWithViewPager(viewPager);
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setupViewPager(viewPager, position + 1);
}
private void setupViewPager(ViewPager viewPager, int weekNo) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
final SunFragment sunFragment = new SunFragment();
Bundle sunBundle = new Bundle();
sunBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
sunFragment.setArguments(sunBundle);
adapter.addFragment(sunFragment, "Sunday");
final MonFragment monFragment = new MonFragment();
Bundle monBundle = new Bundle();
monBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
monFragment.setArguments(monBundle);
adapter.addFragment(monFragment, "Monday");
final TuesFragment tuesFragment = new TuesFragment();
Bundle tuesBundle = new Bundle();
tuesBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
tuesFragment.setArguments(tuesBundle);
adapter.addFragment(tuesFragment, "Tuesday");
final WedFragment wedFragment = new WedFragment();
Bundle wedBundle = new Bundle();
wedBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
wedFragment.setArguments(wedBundle);
adapter.addFragment(wedFragment, "Wednesday");
final ThursFragment thursFragment = new ThursFragment();
Bundle thursBundle = new Bundle();
thursBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
thursFragment.setArguments(thursBundle);
adapter.addFragment(thursFragment, "Thursday");
final FriFragment friFragment = new FriFragment();
Bundle friBundle = new Bundle();
friBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
friFragment.setArguments(friBundle);
adapter.addFragment(friFragment, "Friday");
final SatFragment satFragment = new SatFragment();
Bundle satBundle = new Bundle();
satBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
satFragment.setArguments(satBundle);
adapter.addFragment(satFragment, "Saturday");
viewPager.setAdapter(adapter);
}
static class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
片段按以下顺序加载:
滑动时右键
片段 - &gt;加载数据
太阳太阳mon tues
星期二
结婚
thurs fri
fri sat
坐着坐着向左滑动时
片段 - &gt;加载数据
坐着坐着fri thurs
我们结婚了
星期五
星期一mon sun
太阳太阳