我正在利用 这个库 来设置自定义日历,但似乎在每次单击日期时,currentSelection 都设置为空。我有以下 daybinder 设置。
calendarView.setDayBinder(new MonthDayBinder<DayViewContainer>() {
// Called only when a new container is needed.
@NonNull
@Override
public DayViewContainer create(@NonNull View view) {
return new DayViewContainer(view);
}
// Called every time we need to reuse a container.
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void bind(@NonNull DayViewContainer container, CalendarDay data) {
container.day = data;
CalendarDay day = data;
selectedDate = container.selectedDate;
container.calendarView = calendarView;
Log.d(TAG, "bind: "+ container.day);
TextView textView = container.textView;
textView.setText(String.valueOf(data.getDate().getDayOfMonth()));
if (day.getPosition() == DayPosition.MonthDate) {
textView.setVisibility(View.VISIBLE);
LocalDate date = day.getDate();
// If user selects same date, clear highlight
if (date.equals(selectedDate)) {
// New date was selected.
textView.setTextColor(Color.WHITE);
textView.setBackgroundColor(Color.rgb(98,0,238));
} else {
textView.setVisibility(View.VISIBLE);
textView.setTextColor(Color.BLACK);
textView.setBackgroundColor(Color.TRANSPARENT);
}
} else {
textView.setVisibility(View.INVISIBLE);
}
}
});
calendarView.setMonthHeaderBinder(new MonthHeaderFooterBinder<MonthViewContainer>() {
@NonNull
@Override
public MonthViewContainer create(@NonNull View view) {
return new MonthViewContainer(view);
}
@Override
public void bind(@NonNull MonthViewContainer container, @NonNull CalendarMonth data) {
if (container.getTitlesContainer().getTag() == null) {
container.getTitlesContainer().setTag(data.getYearMonth());
TextView textView = container.getTitlesContainer();
String title = String.valueOf(data.getYearMonth());
textView.setText(title);
}
}
});
和相关的 DayViewContainer 类:
public class DayViewContainer extends ViewContainer {
public TextView textView;
@SuppressLint("NewApi")
public CalendarDay day;
public LocalDate selectedDate;
public LocalDate currentSelection;
public CalendarView calendarView;
public DayViewContainer(View view) {
super(view);
textView = view.findViewById(R.id.calendarDayText);
// With ViewBinding
CalendarDayLayoutBinding binding = CalendarDayLayoutBinding.bind(view);
textView = binding.calendarDayText;
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (day.getPosition() == DayPosition.MonthDate) {
currentSelection = selectedDate;
Log.d(TAG, "onClick: Clicked inDate");
// If user selects same date, clear eventContainer
if (currentSelection != null && currentSelection.equals(day.getDate())) {
selectedDate = null;
textView.post(() -> calendarView.notifyDateChanged(currentSelection));
Log.d(TAG, "onClick: Clicked the same date that was selected");
} else {
selectedDate = day.getDate();
if (currentSelection != null){
textView.post(() -> calendarView.notifyDateChanged(currentSelection));
Log.d(TAG, "onClick: Updating old date");
}
textView.post(() -> calendarView.notifyDateChanged(selectedDate));
Log.d(TAG, "onClick: Updating new date");
}
}
}
});
}
}
如果我理解正确,每个日期都有自己的 dayviewcontainer,其中有 selectedDate。我相信将它存储在我的活动中可能会解决这个问题,但我不确定如何这样做。