我正在编写日历视图。当我按下月份按钮时,我调用mCalendarContainerLL.invalidate();但我的自定义视图未重绘。我的自定义视图中的onDraw方法未被调用。
Ps:如果我直接使所有日历单元格视图无效。它有效。 为什么会这样?
以下是代码:
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;
}
答案 0 :(得分:5)
如果在布局级别调用invalidate()
,而不是布局决定,则必须重绘它的哪些部分。因此,它仅对受影响/脏的子视图调用invalidate()
。布局中看不到对一个单元格的更改,因此布局看不到使任何子项无效的必要性。
当需要重绘时,您应该让视图本身失效。在您的情况下,我会在invalidateSelf()
方法中添加对setParamView
的调用。据我了解代码,这是负责在日历中装饰一个单元格的方法。