Question

I'm writing a calendar view.When i pressed the next month button,i call mCalendarContainerLL.invalidate();But my custom views was not redrawed.The onDraw method in my custom view was not called.

Ps:If i directly invalidate all calendar cell view.It works. Why this happened???

Here's the code:

private CalendarWidgetDayCell updateCalendarView() {

        CalendarWidgetDayCell dayCellSelected = null;
        boolean isSelected = false;

        final boolean isHasSelection = (mCalendarSelected.getTimeInMillis() != 0);

        final int selectedYear = mCalendarSelected.get(Calendar.YEAR);
        final int selectedMonth = mCalendarSelected.get(Calendar.MONTH);
        final int selectedDay = mCalendarSelected.get(Calendar.DAY_OF_MONTH);

        Calendar dateXOfCalendar = Calendar.getInstance();
        dateXOfCalendar.setTimeInMillis(mStartDateCurrentMonth.getTimeInMillis());

        Log.d(tag, "updateCalendarView cpt_func_ " + "mDayCellList.size(): " + mDayCellList.size());
        for (int i = 0; i < mDayCellList.size(); i++) {
            final int yearOfCellItem = dateXOfCalendar.get(Calendar.YEAR);
            final int monthOfCellItem = dateXOfCalendar.get(Calendar.MONTH);
            final int dayOfCellItem = dateXOfCalendar.get(Calendar.DAY_OF_MONTH);
            final int dayOfWeekOfCellItem = dateXOfCalendar.get(Calendar.DAY_OF_WEEK);
            CalendarWidgetDayCell dayCellItem = mDayCellList.get(i);

            boolean isToday = false;
            if (mCalendarToday.get(Calendar.YEAR) == yearOfCellItem) {
                if (mCalendarToday.get(Calendar.MONTH) == monthOfCellItem) {
                    if (mCalendarToday.get(Calendar.DAY_OF_MONTH) == dayOfCellItem) {
                        isToday = true;
                    }
                }
            }

            // check holiday
            boolean isHoliday = false;
            if ((dayOfWeekOfCellItem == Calendar.SATURDAY) || (dayOfWeekOfCellItem == Calendar.SUNDAY))
                isHoliday = true;
            if ((monthOfCellItem == Calendar.JANUARY) && (dayOfCellItem == 1))
                isHoliday = true;

            isSelected = false;
            if (isHasSelection) {
                if ((selectedDay == dayOfCellItem) && (selectedMonth == monthOfCellItem)
                        && (selectedYear == yearOfCellItem)) {
                    isSelected = true;
                }
            }
            dayCellItem.setSelected(isSelected);

            boolean hasRecord = false;

            if (isSelected)
                dayCellSelected = dayCellItem;

            dayCellItem.setViewParam(yearOfCellItem, monthOfCellItem, dayOfCellItem, isToday, isHoliday,
                    mCurrentMonthInCalendar, hasRecord);

            dayCellItem.invalidate();//It works well.
            dateXOfCalendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        // mCalendarContainerLL.invalidate();//This is not working as expected.
        return dayCellSelected;
    }
Was it helpful?

Solution

If invalidate() is called on the Layout level, than the layout decides, which parts of it has to be redrawn. It therefore calls invalidate() only on the child views, that are affected/dirty. Non of the changes to one cell are visible to the layout, so the layout doesn't see the necessity to invalidate any child.

You should let the view itself get invalidated, when it needs to be redrawn. In your case I would add a call to invalidateSelf() in the setParamView method. As far as I understand the code, this is the method that is responsible to decorate one cell as a day in the calendar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top