@@ -4305,14 +4305,15 @@ void start(final int position) {
43054305 final int lastPos = firstPos + childCount - 1 ;
43064306
43074307 int viewTravelCount ;
4308- if (position < firstPos ) {
4309- viewTravelCount = firstPos - position + 1 ;
4308+ int clampedPosition = Math .max (0 , Math .min (getCount () - 1 , position ));
4309+ if (clampedPosition < firstPos ) {
4310+ viewTravelCount = firstPos - clampedPosition + 1 ;
43104311 mMode = MOVE_UP_POS ;
4311- } else if (position > lastPos ) {
4312- viewTravelCount = position - lastPos + 1 ;
4312+ } else if (clampedPosition > lastPos ) {
4313+ viewTravelCount = clampedPosition - lastPos + 1 ;
43134314 mMode = MOVE_DOWN_POS ;
43144315 } else {
4315- scrollToVisible (position , INVALID_POSITION , SCROLL_DURATION );
4316+ scrollToVisible (clampedPosition , INVALID_POSITION , SCROLL_DURATION );
43164317 return ;
43174318 }
43184319
@@ -4321,7 +4322,7 @@ void start(final int position) {
43214322 } else {
43224323 mScrollDuration = SCROLL_DURATION ;
43234324 }
4324- mTargetPos = position ;
4325+ mTargetPos = clampedPosition ;
43254326 mBoundPos = INVALID_POSITION ;
43264327 mLastSeenPos = INVALID_POSITION ;
43274328
@@ -4356,14 +4357,15 @@ void start(final int position, final int boundPosition) {
43564357 final int lastPos = firstPos + childCount - 1 ;
43574358
43584359 int viewTravelCount ;
4359- if (position < firstPos ) {
4360+ int clampedPosition = Math .max (0 , Math .min (getCount () - 1 , position ));
4361+ if (clampedPosition < firstPos ) {
43604362 final int boundPosFromLast = lastPos - boundPosition ;
43614363 if (boundPosFromLast < 1 ) {
43624364 // Moving would shift our bound position off the screen. Abort.
43634365 return ;
43644366 }
43654367
4366- final int posTravel = firstPos - position + 1 ;
4368+ final int posTravel = firstPos - clampedPosition + 1 ;
43674369 final int boundTravel = boundPosFromLast - 1 ;
43684370 if (boundTravel < posTravel ) {
43694371 viewTravelCount = boundTravel ;
@@ -4372,14 +4374,14 @@ void start(final int position, final int boundPosition) {
43724374 viewTravelCount = posTravel ;
43734375 mMode = MOVE_UP_POS ;
43744376 }
4375- } else if (position > lastPos ) {
4377+ } else if (clampedPosition > lastPos ) {
43764378 final int boundPosFromFirst = boundPosition - firstPos ;
43774379 if (boundPosFromFirst < 1 ) {
43784380 // Moving would shift our bound position off the screen. Abort.
43794381 return ;
43804382 }
43814383
4382- final int posTravel = position - lastPos + 1 ;
4384+ final int posTravel = clampedPosition - lastPos + 1 ;
43834385 final int boundTravel = boundPosFromFirst - 1 ;
43844386 if (boundTravel < posTravel ) {
43854387 viewTravelCount = boundTravel ;
@@ -4389,7 +4391,7 @@ void start(final int position, final int boundPosition) {
43894391 mMode = MOVE_DOWN_POS ;
43904392 }
43914393 } else {
4392- scrollToVisible (position , boundPosition , SCROLL_DURATION );
4394+ scrollToVisible (clampedPosition , boundPosition , SCROLL_DURATION );
43934395 return ;
43944396 }
43954397
@@ -4398,7 +4400,7 @@ void start(final int position, final int boundPosition) {
43984400 } else {
43994401 mScrollDuration = SCROLL_DURATION ;
44004402 }
4401- mTargetPos = position ;
4403+ mTargetPos = clampedPosition ;
44024404 mBoundPos = boundPosition ;
44034405 mLastSeenPos = INVALID_POSITION ;
44044406
@@ -4431,7 +4433,7 @@ void startWithOffset(final int position, int offset, final int duration) {
44314433
44324434 offset += getPaddingTop ();
44334435
4434- mTargetPos = position ;
4436+ mTargetPos = Math . max ( 0 , Math . min ( getCount () - 1 , position )) ;
44354437 mOffsetFromTop = offset ;
44364438 mBoundPos = INVALID_POSITION ;
44374439 mLastSeenPos = INVALID_POSITION ;
@@ -4441,13 +4443,13 @@ void startWithOffset(final int position, int offset, final int duration) {
44414443 final int lastPos = firstPos + childCount - 1 ;
44424444
44434445 int viewTravelCount ;
4444- if (position < firstPos ) {
4445- viewTravelCount = firstPos - position ;
4446- } else if (position > lastPos ) {
4447- viewTravelCount = position - lastPos ;
4446+ if (mTargetPos < firstPos ) {
4447+ viewTravelCount = firstPos - mTargetPos ;
4448+ } else if (mTargetPos > lastPos ) {
4449+ viewTravelCount = mTargetPos - lastPos ;
44484450 } else {
44494451 // On-screen, just scroll.
4450- final int targetTop = getChildAt (position - firstPos ).getTop ();
4452+ final int targetTop = getChildAt (mTargetPos - firstPos ).getTop ();
44514453 smoothScrollBy (targetTop - offset , duration , true );
44524454 return ;
44534455 }
0 commit comments