33
33
import android .view .View ;
34
34
import android .view .View .OnClickListener ;
35
35
import android .view .ViewGroup ;
36
+ import android .view .accessibility .AccessibilityEvent ;
37
+ import android .view .accessibility .AccessibilityManager ;
36
38
import android .widget .GridView ;
37
39
import androidx .annotation .NonNull ;
38
40
import androidx .annotation .Nullable ;
@@ -92,6 +94,8 @@ enum CalendarSelector {
92
94
private View monthNext ;
93
95
private View yearFrame ;
94
96
private View dayFrame ;
97
+ private MaterialButton monthDropSelect ;
98
+ private AccessibilityManager accessibilityManager ;
95
99
96
100
@ NonNull
97
101
public static <T > MaterialCalendar <T > newInstance (
@@ -149,6 +153,9 @@ public View onCreateView(
149
153
calendarStyle = new CalendarStyle (themedContext );
150
154
LayoutInflater themedInflater = layoutInflater .cloneInContext (themedContext );
151
155
156
+ accessibilityManager =
157
+ (AccessibilityManager ) requireContext ().getSystemService (Context .ACCESSIBILITY_SERVICE );
158
+
152
159
Month earliestMonth = calendarConstraints .getStart ();
153
160
154
161
int layout ;
@@ -335,18 +342,23 @@ CalendarConstraints getCalendarConstraints() {
335
342
void setCurrentMonth (Month moveTo ) {
336
343
MonthsPagerAdapter adapter = (MonthsPagerAdapter ) recyclerView .getAdapter ();
337
344
int moveToPosition = adapter .getPosition (moveTo );
338
- int distance = moveToPosition - adapter .getPosition (current );
339
- boolean jump = Math .abs (distance ) > SMOOTH_SCROLL_MAX ;
340
- boolean isForward = distance > 0 ;
341
- current = moveTo ;
342
- if (jump && isForward ) {
343
- recyclerView .scrollToPosition (moveToPosition - SMOOTH_SCROLL_MAX );
344
- postSmoothRecyclerViewScroll (moveToPosition );
345
- } else if (jump ) {
346
- recyclerView .scrollToPosition (moveToPosition + SMOOTH_SCROLL_MAX );
347
- postSmoothRecyclerViewScroll (moveToPosition );
345
+ if (accessibilityManager != null && accessibilityManager .isEnabled ()) {
346
+ current = moveTo ;
347
+ recyclerView .scrollToPosition (moveToPosition );
348
348
} else {
349
- postSmoothRecyclerViewScroll (moveToPosition );
349
+ int distance = moveToPosition - adapter .getPosition (current );
350
+ boolean jump = Math .abs (distance ) > SMOOTH_SCROLL_MAX ;
351
+ boolean isForward = distance > 0 ;
352
+ current = moveTo ;
353
+ if (jump && isForward ) {
354
+ recyclerView .scrollToPosition (moveToPosition - SMOOTH_SCROLL_MAX );
355
+ postSmoothRecyclerViewScroll (moveToPosition );
356
+ } else if (jump ) {
357
+ recyclerView .scrollToPosition (moveToPosition + SMOOTH_SCROLL_MAX );
358
+ postSmoothRecyclerViewScroll (moveToPosition );
359
+ } else {
360
+ postSmoothRecyclerViewScroll (moveToPosition );
361
+ }
350
362
}
351
363
updateNavigationButtonsEnabled (moveToPosition );
352
364
}
@@ -394,6 +406,12 @@ void setSelector(CalendarSelector selector) {
394
406
}
395
407
}
396
408
409
+ void sendAccessibilityFocusEventToMonthDropdown () {
410
+ if (monthDropSelect != null ) {
411
+ monthDropSelect .sendAccessibilityEvent (AccessibilityEvent .TYPE_VIEW_FOCUSED );
412
+ }
413
+ }
414
+
397
415
void toggleVisibleSelector () {
398
416
if (calendarSelector == CalendarSelector .YEAR ) {
399
417
setSelector (CalendarSelector .DAY );
@@ -408,7 +426,7 @@ void toggleVisibleSelector() {
408
426
409
427
private void addActionsToMonthNavigation (
410
428
@ NonNull final View root , @ NonNull final MonthsPagerAdapter monthsPagerAdapter ) {
411
- final MaterialButton monthDropSelect = root .findViewById (R .id .month_navigation_fragment_toggle );
429
+ monthDropSelect = root .findViewById (R .id .month_navigation_fragment_toggle );
412
430
monthDropSelect .setTag (SELECTOR_TOGGLE_TAG );
413
431
ViewCompat .setAccessibilityDelegate (
414
432
monthDropSelect ,
0 commit comments