/**
   * Move calendar to specified dateTime, with animation
   *
   * @param dateTime
   */
  public void moveToDateTime(DateTime dateTime) {

    DateTime firstOfMonth = new DateTime(year, month, 1, 0, 0, 0, 0);
    DateTime lastOfMonth = firstOfMonth.getEndOfMonth();

    // To create a swipe effect
    // Do nothing if the dateTime is in current month

    // Calendar swipe left when dateTime is in the past
    if (dateTime.lt(firstOfMonth)) {
      // Get next month of dateTime. When swipe left, month will
      // decrease
      DateTime firstDayNextMonth = dateTime.plus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);

      // Refresh adapters
      pageChangeListener.setCurrentDateTime(firstDayNextMonth);
      int currentItem = dateViewPager.getCurrentItem();
      pageChangeListener.refreshAdapters(currentItem);

      // Swipe left
      dateViewPager.setCurrentItem(currentItem - 1);
    }

    // Calendar swipe right when dateTime is in the future
    else if (dateTime.gt(lastOfMonth)) {
      // Get last month of dateTime. When swipe right, the month will
      // increase
      DateTime firstDayLastMonth =
          dateTime.minus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);

      // Refresh adapters
      pageChangeListener.setCurrentDateTime(firstDayLastMonth);
      int currentItem = dateViewPager.getCurrentItem();
      pageChangeListener.refreshAdapters(currentItem);

      // Swipe right
      dateViewPager.setCurrentItem(currentItem + 1);
    }
  }
 public void setSixWeeksInCalendar(boolean sixWeeksInCalendar) {
   this.sixWeeksInCalendar = sixWeeksInCalendar;
   dateViewPager.setSixWeeksInCalendar(sixWeeksInCalendar);
 }
 public void setEnableSwipe(boolean enableSwipe) {
   this.enableSwipe = enableSwipe;
   dateViewPager.setEnabled(enableSwipe);
 }
 /** Set calendar to next month */
 public void nextMonth() {
   dateViewPager.setCurrentItem(pageChangeListener.getCurrentPage() + 1);
 }
 /** Set calendar to previous month */
 public void prevMonth() {
   dateViewPager.setCurrentItem(pageChangeListener.getCurrentPage() - 1);
 }
 /** Get current virtual position of the month being viewed */
 public int getCurrentVirtualPosition() {
   int currentPage = dateViewPager.getCurrentItem();
   return pageChangeListener.getCurrent(currentPage);
 }
  /**
   * Setup 4 pages contain date grid views. These pages are recycled to use memory efficient
   *
   * @param view
   */
  private void setupDateGridPages(View view) {
    // Get current date time
    DateTime currentDateTime = new DateTime(year, month + 1, 1, 0, 0, 0, 0);

    // Set to pageChangeListener
    pageChangeListener = new DatePageChangeListener();
    pageChangeListener.setCurrentDateTime(currentDateTime);

    // Setup adapters for the grid views
    // Current month
    CaldroidGridAdapter adapter0 =
        getNewDatesGridAdapter(currentDateTime.getMonth(), currentDateTime.getYear());

    // Setup dateInMonthsList
    dateInMonthsList = adapter0.getDatetimeList();

    // Next month
    DateTime nextDateTime = currentDateTime.plus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
    CaldroidGridAdapter adapter1 =
        getNewDatesGridAdapter(nextDateTime.getMonth(), nextDateTime.getYear());

    // Next 2 month
    DateTime next2DateTime = nextDateTime.plus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
    CaldroidGridAdapter adapter2 =
        getNewDatesGridAdapter(next2DateTime.getMonth(), next2DateTime.getYear());

    // Previous month
    DateTime prevDateTime =
        currentDateTime.minus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
    CaldroidGridAdapter adapter3 =
        getNewDatesGridAdapter(prevDateTime.getMonth(), prevDateTime.getYear());

    // Add to the array of adapters
    datePagerAdapters.add(adapter0);
    datePagerAdapters.add(adapter1);
    datePagerAdapters.add(adapter2);
    datePagerAdapters.add(adapter3);

    // Set adapters to the pageChangeListener so it can refresh the adapter
    // when page change
    pageChangeListener.setCaldroidGridAdapters(datePagerAdapters);

    // Setup InfiniteViewPager and InfinitePagerAdapter. The
    // InfinitePagerAdapter is responsible
    // for reuse the fragments
    dateViewPager = (InfiniteViewPager) view.findViewById(R.id.months_infinite_pager);

    // Set enable swipe
    dateViewPager.setEnabled(enableSwipe);

    // Set if viewpager wrap around particular month or all months (6 rows)
    dateViewPager.setSixWeeksInCalendar(sixWeeksInCalendar);

    // Set the numberOfDaysInMonth to dateViewPager so it can calculate the
    // height correctly
    dateViewPager.setDatesInMonth(dateInMonthsList);

    // MonthPagerAdapter actually provides 4 real fragments. The
    // InfinitePagerAdapter only recycles fragment provided by this
    // MonthPagerAdapter
    final MonthPagerAdapter pagerAdapter = new MonthPagerAdapter(getChildFragmentManager());

    // Provide initial data to the fragments, before they are attached to
    // view.
    fragments = pagerAdapter.getFragments();
    for (int i = 0; i < NUMBER_OF_PAGES; i++) {
      DateGridFragment dateGridFragment = fragments.get(i);
      CaldroidGridAdapter adapter = datePagerAdapters.get(i);
      dateGridFragment.setGridAdapter(adapter);
      dateGridFragment.setOnItemClickListener(getDateItemClickListener());
      dateGridFragment.setOnItemLongClickListener(getDateItemLongClickListener());
    }

    // Setup InfinitePagerAdapter to wrap around MonthPagerAdapter
    InfinitePagerAdapter infinitePagerAdapter = new InfinitePagerAdapter(pagerAdapter);

    // Use the infinitePagerAdapter to provide data for dateViewPager
    dateViewPager.setAdapter(infinitePagerAdapter);

    // Setup pageChangeListener
    dateViewPager.setOnPageChangeListener(pageChangeListener);
  }