想要制作如下的自定义日历

时间:2018-01-03 09:40:18

标签: android android-recyclerview calendar

Calender preview 想制作具有三个Recyclerview的上述日历。弗里斯特有一年。第二个月。根据第二个Recyclerview选择,它将显示日期/日期。如何将这个年,月,日日期添加到所有三个recylerview。

2 个答案:

答案 0 :(得分:0)

  1. Init第一年Recyclerview
  2. 选中默认年份(现年)
  3. Init第二个月Recyclerview。
  4. 已检查默认月份(现在是月份)
  5. 每个月按月获取多少天,您可以初始化第三次Recycleview。
  6. 检查默认日期(现在是日期)
  7. 当第一个Recyclerview项目点击更改了第二个Recyclerview数据并更改了第三个Recyclerview。
  8. 好的。我希望它可以帮到你!感谢

答案 1 :(得分:0)

我首先找到制作自定义日历的方法,我从日历中获取当天日期,然后执行以下计算

    private void printMonth(int mm, int yy) {
    Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
    int trailingSpaces = 0;
    int daysInPrevMonth = 0;
    int prevMonth = 0;
    int prevYear = 0;
    int nextMonth = 0;
    int nextYear = 0;

    int currentMonth = mm - 1;
    String currentMonthName = getMonthAsString(currentMonth);
    daysInMonth = getNumberOfDaysOfMonth(currentMonth);

    Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
            + daysInMonth + " days.");

    GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
    Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

    if (currentMonth == 11) {
        prevMonth = currentMonth - 1;
        daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
        nextMonth = 0;
        prevYear = yy;
        nextYear = yy + 1;
        Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
                + prevMonth + " NextMonth: " + nextMonth
                + " NextYear: " + nextYear);
    } else if (currentMonth == 0) {
        prevMonth = 11;
        prevYear = yy - 1;
        nextYear = yy;
        daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
        nextMonth = 1;
        Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
                + prevMonth + " NextMonth: " + nextMonth
                + " NextYear: " + nextYear);
    } else {
        prevMonth = currentMonth - 1;
        nextMonth = currentMonth + 1;
        nextYear = yy;
        prevYear = yy;
        daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
        Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
                + prevMonth + " NextMonth: " + nextMonth
                + " NextYear: " + nextYear);
    }

    int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
    trailingSpaces = currentWeekDay;

    Log.d(tag, "Week Day:" + currentWeekDay + " is "
            + getWeekDayAsString(currentWeekDay));
    Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
    Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

    if (cal.isLeapYear(cal.get(Calendar.YEAR)))
        if (mm == 2)
            ++daysInMonth;
        else if (mm == 3)
            ++daysInPrevMonth;

    // Trailing Month days
    for (int i = 0; i < trailingSpaces; i++) {
        Log.d(tag,
                "PREV MONTH:= "
                        + prevMonth
                        + " => "
                        + getMonthAsString(prevMonth)
                        + " "
                        + String.valueOf((daysInPrevMonth
                        - trailingSpaces + DAY_OFFSET)
                        + i));
        list.add(String
                .valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
                        + i)
                + "-GREY"
                + "-"
                + getMonthAsString(prevMonth)
                + "-"
                + prevYear);
    }

    // Current Month Days
    for (int i = 1; i <= daysInMonth; i++) {
        Log.d(currentMonthName, String.valueOf(i) + " "
                + getMonthAsString(currentMonth) + " " + yy);
        if (i == getCurrentDayOfMonth()) {
            list.add(String.valueOf(i) + "-BLUE" + "-"
                    + getMonthAsString(currentMonth) + "-" + yy);
        } else {
            list.add(String.valueOf(i) + "-WHITE" + "-"
                    + getMonthAsString(currentMonth) + "-" + yy);
        }
    }

    // Leading Month days
    for (int i = 0; i < list.size() % 7; i++) {
        Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
        list.add(String.valueOf(i + 1) + "-GREY" + "-"
                + getMonthAsString(nextMonth) + "-" + nextYear);
    }