@@ -68,6 +68,7 @@ enum CalendarSelector {
6868 private static final String GRID_SELECTOR_KEY = "GRID_SELECTOR_KEY" ;
6969 private static final String CALENDAR_CONSTRAINTS_KEY = "CALENDAR_CONSTRAINTS_KEY" ;
7070 private static final String CURRENT_MONTH_KEY = "CURRENT_MONTH_KEY" ;
71+ private static final int SMOOTH_SCROLL_MAX = 3 ;
7172
7273 @ VisibleForTesting
7374 @ RestrictTo (Scope .LIBRARY_GROUP )
@@ -83,7 +84,6 @@ enum CalendarSelector {
8384 private RecyclerView recyclerView ;
8485 private View yearFrame ;
8586 private View dayFrame ;
86- private MaterialButton monthDropSelect ;
8787
8888 static <T > MaterialCalendar <T > newInstance (
8989 DateSelector <T > dateSelector , int themeResId , CalendarConstraints calendarConstraints ) {
@@ -140,27 +140,38 @@ public View onCreateView(
140140
141141 View root = themedInflater .inflate (layout , viewGroup , false );
142142 GridView daysHeader = root .findViewById (R .id .mtrl_calendar_days_of_week );
143+ ViewCompat .setAccessibilityDelegate (
144+ daysHeader ,
145+ new AccessibilityDelegateCompat () {
146+ @ Override
147+ public void onInitializeAccessibilityNodeInfo (
148+ View view , AccessibilityNodeInfoCompat accessibilityNodeInfoCompat ) {
149+ super .onInitializeAccessibilityNodeInfo (view , accessibilityNodeInfoCompat );
150+ // Remove announcing row/col info.
151+ accessibilityNodeInfoCompat .setCollectionInfo (null );
152+ }
153+ });
143154 daysHeader .setAdapter (new DaysOfWeekAdapter ());
144155 daysHeader .setNumColumns (earliestMonth .daysInWeek );
145156 daysHeader .setEnabled (false );
146157
147- final RecyclerView monthsPager = root .findViewById (R .id .mtrl_calendar_months );
158+ recyclerView = root .findViewById (R .id .mtrl_calendar_months );
148159
149160 LinearLayoutManager layoutManager =
150161 new LinearLayoutManager (getContext (), orientation , false ) {
151162 @ Override
152163 protected void calculateExtraLayoutSpace (@ NonNull State state , @ NonNull int [] ints ) {
153164 if (orientation == LinearLayoutManager .HORIZONTAL ) {
154- ints [0 ] = monthsPager .getWidth ();
155- ints [1 ] = monthsPager .getWidth ();
165+ ints [0 ] = recyclerView .getWidth ();
166+ ints [1 ] = recyclerView .getWidth ();
156167 } else {
157- ints [0 ] = monthsPager .getHeight ();
158- ints [1 ] = monthsPager .getHeight ();
168+ ints [0 ] = recyclerView .getHeight ();
169+ ints [1 ] = recyclerView .getHeight ();
159170 }
160171 }
161172 };
162- monthsPager .setLayoutManager (layoutManager );
163- monthsPager .setTag (MONTHS_VIEW_GROUP_TAG );
173+ recyclerView .setLayoutManager (layoutManager );
174+ recyclerView .setTag (MONTHS_VIEW_GROUP_TAG );
164175
165176 final MonthsPagerAdapter monthsPagerAdapter =
166177 new MonthsPagerAdapter (
@@ -177,14 +188,14 @@ public void onDayClick(long day) {
177188 listener .onSelectionChanged (dateSelector .getSelection ());
178189 }
179190 // TODO(b/134663744): Look into monthsPager.getAdapter().notifyItemRangeChanged();
180- monthsPager .getAdapter ().notifyDataSetChanged ();
191+ recyclerView .getAdapter ().notifyDataSetChanged ();
181192 if (yearSelector != null ) {
182193 yearSelector .getAdapter ().notifyDataSetChanged ();
183194 }
184195 }
185196 }
186197 });
187- monthsPager .setAdapter (monthsPagerAdapter );
198+ recyclerView .setAdapter (monthsPagerAdapter );
188199
189200 int columns =
190201 themedContext .getResources ().getInteger (R .integer .mtrl_calendar_year_selector_span );
@@ -202,9 +213,9 @@ public void onDayClick(long day) {
202213 }
203214
204215 if (!MaterialDatePicker .isFullscreen (themedContext )) {
205- new LinearSnapHelper ().attachToRecyclerView (monthsPager );
216+ new LinearSnapHelper ().attachToRecyclerView (recyclerView );
206217 }
207- monthsPager .scrollToPosition (monthsPagerAdapter .getPosition (current ));
218+ recyclerView .scrollToPosition (monthsPagerAdapter .getPosition (current ));
208219 return root ;
209220 }
210221
@@ -275,16 +286,20 @@ CalendarConstraints getCalendarConstraints() {
275286 * CalendarConstraints}.
276287 */
277288 void setCurrentMonth (Month moveTo ) {
278- setCurrentMonth (moveTo , /* smooth= */ true );
279- }
280-
281- void setCurrentMonth (Month moveTo , boolean smooth ) {
289+ MonthsPagerAdapter adapter = (MonthsPagerAdapter ) recyclerView .getAdapter ();
290+ int moveToPosition = adapter .getPosition (moveTo );
291+ int distance = moveToPosition - adapter .getPosition (current );
292+ boolean jump = Math .abs (distance ) > SMOOTH_SCROLL_MAX ;
293+ boolean isForward = distance > 0 ;
282294 current = moveTo ;
283- int moveToPosition = ((MonthsPagerAdapter ) recyclerView .getAdapter ()).getPosition (current );
284- if (smooth ) {
295+ if (jump && isForward ) {
296+ recyclerView .scrollToPosition (moveToPosition - SMOOTH_SCROLL_MAX );
297+ recyclerView .smoothScrollToPosition (moveToPosition );
298+ } else if (jump ) {
299+ recyclerView .scrollToPosition (moveToPosition + SMOOTH_SCROLL_MAX );
285300 recyclerView .smoothScrollToPosition (moveToPosition );
286301 } else {
287- recyclerView .scrollToPosition (moveToPosition );
302+ recyclerView .smoothScrollToPosition (moveToPosition );
288303 }
289304 }
290305
@@ -334,8 +349,7 @@ void toggleVisibleSelector() {
334349
335350 private void addActionsToMonthNavigation (
336351 final View root , final MonthsPagerAdapter monthsPagerAdapter ) {
337- recyclerView = root .findViewById (R .id .mtrl_calendar_months );
338- monthDropSelect = root .findViewById (R .id .month_navigation_fragment_toggle );
352+ final MaterialButton monthDropSelect = root .findViewById (R .id .month_navigation_fragment_toggle );
339353 ViewCompat .setAccessibilityDelegate (
340354 monthDropSelect ,
341355 new AccessibilityDelegateCompat () {
@@ -362,13 +376,11 @@ public void onInitializeAccessibilityNodeInfo(
362376 new OnScrollListener () {
363377 @ Override
364378 public void onScrolled (@ NonNull RecyclerView recyclerView , int dx , int dy ) {
365- LinearLayoutManager layoutManager =
366- (LinearLayoutManager ) recyclerView .getLayoutManager ();
367379 int currentItem ;
368380 if (dx < 0 ) {
369- currentItem = layoutManager .findFirstVisibleItemPosition ();
381+ currentItem = getLayoutManager () .findFirstVisibleItemPosition ();
370382 } else {
371- currentItem = layoutManager .findLastVisibleItemPosition ();
383+ currentItem = getLayoutManager () .findLastVisibleItemPosition ();
372384 }
373385 monthDropSelect .setText (monthsPagerAdapter .getPageTitle (currentItem ));
374386 }
@@ -398,9 +410,7 @@ public void onClick(View view) {
398410 new OnClickListener () {
399411 @ Override
400412 public void onClick (View view ) {
401- int currentItem =
402- ((LinearLayoutManager ) recyclerView .getLayoutManager ())
403- .findFirstVisibleItemPosition ();
413+ int currentItem = getLayoutManager ().findFirstVisibleItemPosition ();
404414 if (currentItem + 1 < recyclerView .getAdapter ().getItemCount ()) {
405415 setCurrentMonth (monthsPagerAdapter .getPageMonth (currentItem + 1 ));
406416 }
@@ -410,13 +420,15 @@ public void onClick(View view) {
410420 new OnClickListener () {
411421 @ Override
412422 public void onClick (View view ) {
413- int currentItem =
414- ((LinearLayoutManager ) recyclerView .getLayoutManager ())
415- .findLastVisibleItemPosition ();
423+ int currentItem = getLayoutManager ().findLastVisibleItemPosition ();
416424 if (currentItem - 1 >= 0 ) {
417425 setCurrentMonth (monthsPagerAdapter .getPageMonth (currentItem - 1 ));
418426 }
419427 }
420428 });
421429 }
430+
431+ LinearLayoutManager getLayoutManager () {
432+ return (LinearLayoutManager ) recyclerView .getLayoutManager ();
433+ }
422434}
0 commit comments