Skip to content Skip to sidebar Skip to footer

Viewpager In Android To Switch Between Days Endlessly

I am currently making a app where students can view there lesson grid. The lessons are retrieved using a json file. Now the date changing is done via buttons in the actionbar, but

Solution 1:

I have fixed this problems a little time ago, this code is a bit based on the answer from Kuffs but I don't get the date in an array because it's a lot of performance to put a lot of days in array.

Summary of code (1) The code makes a FragmentPager with around 10.000 pages in it. On the main activity init the app set the current page to the middle(5000).

pager.setAdapter(new BootstrapPagerAdapter(getResources(), getSupportFragmentManager()));
pager.setCurrentItem(5000, false);
pager.post(new Runnable() {
    public void run() {
        pager.setCurrentItem(5000, false);
    }
});
pager.getAdapter().notifyDataSetChanged();
pager.setOffscreenPageLimit(0);

Summary of code (2) in the bootstrappageradapter

The code looks what position the user has scrolled and looks for the date now

DateTimepagerdate= DateTime.now(TimeZone.getDefault());
DateTimedays= pagerdate.plusDays(position - 5000);

e.g. user swiped three days next. position is 5003, so the sum is 5003-5000=3 days

The days count easily plus 3 days with date4j library (I highly recommend it!) then I make a new fragment with the date in its bundle!

publicclassBootstrapPagerAdapterextendsFragmentPagerAdapter
{
    /**
    * Create pager adapter
    *
    * @param resources
    * @param fragmentManager
    */publicBootstrapPagerAdapter(Resources resources, FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @OverridepublicintgetCount() {
        return10000;
    }

    @OverridepublicintgetItemPosition(Object object){
        return FragmentStatePagerAdapter.POSITION_NONE;
    }

    @Overridepublic Fragment getItem(int position) {
        DateTimepagerdate= DateTime.now(TimeZone.getDefault());
        DateTimedays= pagerdate.plusDays(position - 5000);

        Bundlebundle=newBundle();
        bundle.putString("date", days.format("YYYY-MM-DD").toString());

        RoosterFragmentroosterFragment=newRoosterFragment();
        roosterFragment.setArguments(bundle);

        return roosterFragment;
    }
}

You could fake the endlessly count with more high numbers like

  • year 0001
  • year 3999

Around the: 1.500.000 days believe me no one is gonna go this far all the way swiping!

I've tried the a lot infinite examples but this is the only working example I think!

Upvote if it worked for you and don't be shy to ask for more examples or help with your app!

However it would be cool if someone got something like a really infinte viewpager without fake counts and so on ;)

Solution 2:

Create a pageradapter that contains an array of the dates you want to support. You can pass this array in on the constructor.

Override getCount to return the correct number of dates in the array.

Override getItem and return a fragment from it with the date initialised based on the position parameter.

Post a Comment for "Viewpager In Android To Switch Between Days Endlessly"