@@ -94,12 +94,15 @@ public class WeekView extends View {
94
94
private int mHeaderColumnBackgroundColor = Color .WHITE ;
95
95
private int mDefaultEventColor ;
96
96
private boolean mIsFirstDraw = true ;
97
+ private boolean mAreDimensionsInvalid = true ;
97
98
@ Deprecated private int mDayNameLength = LENGTH_LONG ;
98
99
private int mOverlappingEventGap = 0 ;
99
100
private int mEventMarginVertical = 0 ;
100
101
private float mXScrollingSpeed = 1f ;
101
102
private Calendar mFirstVisibleDay ;
102
103
private Calendar mLastVisibleDay ;
104
+ private Calendar mScrollToDay = null ;
105
+ private double mScrollToHour = -1 ;
103
106
104
107
// Listeners.
105
108
private EventClickListener mEventClickListener ;
@@ -378,13 +381,23 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
378
381
mWidthPerDay = getWidth () - mHeaderColumnWidth - mColumnGap * (mNumberOfVisibleDays - 1 );
379
382
mWidthPerDay = mWidthPerDay /mNumberOfVisibleDays ;
380
383
381
- // If the week view is being drawn for the first time, then consider the first day of week.
382
- if (mIsFirstDraw && mNumberOfVisibleDays >= 7 ) {
383
- if (mToday .get (Calendar .DAY_OF_WEEK ) != mFirstDayOfWeek ) {
384
+ if (mAreDimensionsInvalid ) {
385
+ mAreDimensionsInvalid = false ;
386
+ double scrollToHour = mScrollToHour ;
387
+
388
+ if (mScrollToDay != null )
389
+ goToDate (mScrollToDay );
390
+ if (scrollToHour >= 0 )
391
+ goToHour (scrollToHour );
392
+ }
393
+ if (mIsFirstDraw ){
394
+ mIsFirstDraw = false ;
395
+
396
+ // If the week view is being drawn for the first time, then consider the first day of the week.
397
+ if (mNumberOfVisibleDays >= 7 && mToday .get (Calendar .DAY_OF_WEEK ) != mFirstDayOfWeek ) {
384
398
int difference = 7 + (mToday .get (Calendar .DAY_OF_WEEK ) - mFirstDayOfWeek );
385
399
mCurrentOrigin .x += (mWidthPerDay + mColumnGap ) * difference ;
386
400
}
387
- mIsFirstDraw = false ;
388
401
}
389
402
390
403
// Consider scroll offset.
@@ -904,6 +917,13 @@ private void deleteFarMonths(Calendar currentDay) {
904
917
mEventRects .addAll (newEvents );
905
918
}
906
919
920
+ @ Override
921
+ public void invalidate () {
922
+ super .invalidate ();
923
+ mAreDimensionsInvalid = true ;
924
+ mScrollToDay = null ;
925
+ mScrollToHour = -1 ;
926
+ }
907
927
908
928
/////////////////////////////////////////////////////////////////
909
929
//
@@ -1351,6 +1371,11 @@ public void goToDate(Calendar date) {
1351
1371
date .set (Calendar .SECOND , 0 );
1352
1372
date .set (Calendar .MILLISECOND , 0 );
1353
1373
1374
+ if (mAreDimensionsInvalid ) {
1375
+ mScrollToDay = date ;
1376
+ return ;
1377
+ }
1378
+
1354
1379
mRefreshEvents = true ;
1355
1380
1356
1381
Calendar today = Calendar .getInstance ();
@@ -1360,8 +1385,7 @@ public void goToDate(Calendar date) {
1360
1385
today .set (Calendar .MILLISECOND , 0 );
1361
1386
1362
1387
int dateDifference = (int ) ((date .getTimeInMillis () - today .getTimeInMillis ()) / (1000 * 60 * 60 * 24 ));
1363
- mCurrentOrigin .x = - dateDifference * (mWidthPerDay + mColumnGap );
1364
-
1388
+ mStickyScroller .startScroll ((int ) mCurrentOrigin .x , 0 , (int ) (-dateDifference *(mWidthPerDay + mColumnGap )-mCurrentOrigin .x ), 0 );
1365
1389
invalidate ();
1366
1390
}
1367
1391
@@ -1378,14 +1402,18 @@ public void notifyDatasetChanged(){
1378
1402
* @param hour The hour to scroll to in 24-hour format. Supported values are 0-24.
1379
1403
*/
1380
1404
public void goToHour (double hour ){
1405
+ int verticalOffset = (int ) (mHourHeight * hour );
1381
1406
if (hour < 0 )
1382
- throw new IllegalArgumentException ( "Cannot scroll to an hour of negative value." ) ;
1407
+ verticalOffset = 0 ;
1383
1408
else if (hour > 24 )
1384
- throw new IllegalArgumentException ("Cannot scroll to an hour of value greater than 24." );
1385
- else if (hour * mHourHeight > mHourHeight * 24 - getHeight () + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom )
1386
- throw new IllegalArgumentException ("Cannot scroll to an hour which will result the calendar to go off the screen." );
1409
+ verticalOffset = mHourHeight * 24 ;
1410
+
1411
+ if (mAreDimensionsInvalid ) {
1412
+ mScrollToHour = hour ;
1413
+ return ;
1414
+ } else if (verticalOffset > mHourHeight * 24 - getHeight () + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom )
1415
+ verticalOffset = (int )(mHourHeight * 24 - getHeight () + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom );
1387
1416
1388
- int verticalOffset = (int ) (mHourHeight * hour );
1389
1417
mCurrentOrigin .y = -verticalOffset ;
1390
1418
invalidate ();
1391
1419
}
0 commit comments