0

I have a daily scheduler function that you can access it. The schedule is for a week, so each fragment of the FragmentStatePagerAdapter corresponds to a day. When I access it, it is always on MONDAY, then I can swipe left and to TUESDAY. Please help, I have been searching the internet for a long time!

PROBLEM: Once I know the day, how do I make the FragmentStatePagerAdapter start with the fragment for that day, and show the schedule that was pre-designed for the day?

EX: If it is THURSDAY, it open THUR, then I can swipe left to WEDNESDAY or swipe right to FRIDAY?


public PagerAdapterScheduler (Context context, FragmentManager fm, String[] arrayDayInWeek) {
        super(fm);
        this.mContext = context;
        this.fragmentManager = fm;
        this.arrayDayInWeek = arrayDayInWeek;
    }

@Override
public Fragment getItem(int i) {
    return FragmentDay.newInstance(i, arrayDayInWeek[i]);
}

@Override
public int getCount() {
    return this.arrayDayInWeek.length;
}

Here is the instantiation of the individual fragment of a day (in another FragmentDay.class):

public static final FragmentDay newInstance (int dayNumber, String dayLabel)    {
    FragmentDay fragment = new FragmentDay();
    Bundle bundle = new Bundle();
    bundle.putInt( "dayNumber", dayNumber );
    bundle.putString( "dayLabel", dayLabel );
    fragment.setArguments( bundle );
    return fragment;
}

SOLUTION:

Thank you for suggesting this. I have tried it before but now it works. Here is the catch:

You need to SET-ADAPTER before you SET-CURRENT-ITEM:

mViewPager.setAdapter(mPagerAdapterScheduler);
mViewPager.setCurrentItem( Calendar.getInstance().get(Calendar.DAY_OF_WEEK) );
Sven Sven
  • 1
  • 1

1 Answers1

0

Just apply mViewPager.setCurrentItem(yourWeekDay) after setting up you ViewPager.

the_dani
  • 2,466
  • 2
  • 20
  • 46
  • hello, this works now. I tried it before and it did not work. Here is the catch: you need to setCurrentItem after you setAdapter: mViewPager.setAdapter(mPagerAdapterScheduler); mViewPager.setCurrentItem( Calendar.getInstance().get(Calendar.DAY_OF_WEEK) ); – Sven Sven Feb 02 '19 at 17:51
  • Sure, you can only set the current page after the adapter was applied, otherwise you'd not have a page to scroll to! – the_dani Feb 03 '19 at 12:17