Skip to content

Commit 9f40520

Browse files
committed
[DatePicker] Make the month buttons navigate through months, not pages
1 parent e37ccbb commit 9f40520

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

lib/java/com/google/android/material/datepicker/CalendarConstraints.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ boolean isWithinBounds(long date) {
8888
return start.getDay(1) <= date && date <= end.getDay(end.daysInMonth);
8989
}
9090

91+
boolean isWithinBounds(Month month) {
92+
return start.compareTo(month) <= 0 && end.compareTo(month) >= 0;
93+
}
94+
9195
/**
9296
* Returns the {@link DateValidator} that determines whether a date can be clicked and selected.
9397
*/

lib/java/com/google/android/material/datepicker/MaterialCalendar.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -463,26 +463,20 @@ public void onClick(View view) {
463463
}
464464
});
465465

466-
monthNext.setOnClickListener(
467-
new OnClickListener() {
468-
@Override
469-
public void onClick(View view) {
470-
int currentItem = getLayoutManager().findFirstVisibleItemPosition();
471-
if (currentItem + 1 < recyclerView.getAdapter().getItemCount()) {
472-
setCurrentMonth(monthsPagerAdapter.getPageMonth(currentItem + 1));
473-
}
474-
}
475-
});
476-
monthPrev.setOnClickListener(
477-
new OnClickListener() {
478-
@Override
479-
public void onClick(View view) {
480-
int currentItem = getLayoutManager().findLastVisibleItemPosition();
481-
if (currentItem - 1 >= 0) {
482-
setCurrentMonth(monthsPagerAdapter.getPageMonth(currentItem - 1));
483-
}
484-
}
485-
});
466+
monthNext.setOnClickListener(view -> {
467+
Month currentMonth = getCurrentMonth();
468+
Month nextMonth = currentMonth.monthsLater(1);
469+
if (calendarConstraints.isWithinBounds(nextMonth)) {
470+
setCurrentMonth(nextMonth);
471+
}
472+
});
473+
monthPrev.setOnClickListener(view -> {
474+
Month currentMonth = getCurrentMonth();
475+
Month prevMonth = currentMonth.monthsLater(-1);
476+
if (calendarConstraints.isWithinBounds(prevMonth)) {
477+
setCurrentMonth(prevMonth);
478+
}
479+
});
486480
}
487481

488482
private void postSmoothRecyclerViewScroll(final int position) {

0 commit comments

Comments
 (0)