public void setCalendarDateTime(DateTime dateTime) {
    month = dateTime.getMonth() + 1;
    year = dateTime.getYear();

    // Notify listener
    if (caldroidListener != null) {
      caldroidListener.onChangeMonth(month, year);
    }

    refreshView();
  }
Example #2
0
  public static Date convertDateTimeToDate(DateTime dateTime) {
    int year = dateTime.getYear();
    int datetimeMonth = dateTime.getMonth();
    int day = dateTime.getDay();

    Calendar calendar = Calendar.getInstance();
    calendar.clear();

    // datetimeMonth start at 1. Need to minus 1 to get javaMonth
    calendar.set(year, datetimeMonth - 1, day);

    return calendar.getTime();
  }
  /**
   * Retrieve initial arguments to the fragment Data can include: month, year, dialogTitle,
   * showNavigationArrows,(String) disableDates, selectedDates, minDate, maxDate
   */
  protected void retrieveInitialArgs() {
    // Get arguments
    Bundle args = getArguments();
    if (args != null) {
      // Get month, year
      month = args.getInt(MONTH, -1);
      year = args.getInt(YEAR, -1);
      dialogTitle = args.getString(DIALOG_TITLE);
      Dialog dialog = getDialog();
      if (dialog != null) {
        if (dialogTitle != null) {
          dialog.setTitle(dialogTitle);
        } else {
          // Don't display title bar if user did not supply
          // dialogTitle
          dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
      }

      // Get start day of Week. Default calendar first column is SUNDAY
      startDayOfWeek = args.getInt(START_DAY_OF_WEEK, 1);
      if (startDayOfWeek > 7) {
        startDayOfWeek = startDayOfWeek % 7;
      }

      // Should show arrow
      showNavigationArrows = args.getBoolean(SHOW_NAVIGATION_ARROWS, true);

      // Should enable swipe to change month
      enableSwipe = args.getBoolean(ENABLE_SWIPE, true);

      // Get sixWeeksInCalendar
      sixWeeksInCalendar = args.getBoolean(SIX_WEEKS_IN_CALENDAR, true);

      // Get clickable setting
      enableClickOnDisabledDates = args.getBoolean(ENABLE_CLICK_ON_DISABLED_DATES, false);

      // Get disable dates
      ArrayList<String> disableDateStrings = args.getStringArrayList(DISABLE_DATES);
      if (disableDateStrings != null && disableDateStrings.size() > 0) {
        disableDates.clear();
        for (String dateString : disableDateStrings) {
          DateTime dt = CalendarHelper.getDateTimeFromString(dateString, "yyyy-MM-dd");
          disableDates.add(dt);
        }
      }

      // Get selected dates
      ArrayList<String> selectedDateStrings = args.getStringArrayList(SELECTED_DATES);
      if (selectedDateStrings != null && selectedDateStrings.size() > 0) {
        selectedDates.clear();
        for (String dateString : selectedDateStrings) {
          DateTime dt = CalendarHelper.getDateTimeFromString(dateString, "yyyy-MM-dd");
          selectedDates.add(dt);
        }
      }

      // Get min date and max date
      String minDateTimeString = args.getString(MIN_DATE);
      if (minDateTimeString != null) {
        minDateTime = CalendarHelper.getDateTimeFromString(minDateTimeString, null);
      }

      String maxDateTimeString = args.getString(MAX_DATE);
      if (maxDateTimeString != null) {
        maxDateTime = CalendarHelper.getDateTimeFromString(maxDateTimeString, null);
      }
    }
    if (month == -1 || year == -1) {
      DateTime dateTime = DateTime.today(TimeZone.getDefault());
      month = dateTime.getMonth();
      year = dateTime.getYear();
    }
  }
  /**
   * 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);
  }