@@ -498,18 +498,23 @@ private void horizontalScrollTo(float x) {
498498
499499 private int scrollTo (float oldDragPos , float newDragPos , int [] scrollbarRange , int scrollRange ,
500500 int scrollOffset , int viewLength ) {
501- int scrollbarLength = scrollbarRange [1 ] - scrollbarRange [0 ];
501+ final int scrollbarLength = scrollbarRange [1 ] - scrollbarRange [0 ];
502502 if (scrollbarLength == 0 ) {
503503 return 0 ;
504504 }
505- float percentage = ((newDragPos - oldDragPos ) / (float ) scrollbarLength );
506- int totalPossibleOffset = scrollRange - viewLength ;
507- int scrollingBy = (int ) (percentage * totalPossibleOffset );
508- int absoluteOffset = scrollOffset + scrollingBy ;
505+ final float percentage = ((newDragPos - oldDragPos ) / (float )scrollbarLength );
506+ final int totalPossibleOffset = scrollRange - viewLength ;
507+ final float scrollingBy = percentage * scrollRange ;
508+ final float absoluteOffset = scrollOffset + scrollingBy ;
509+
509510 if (absoluteOffset < totalPossibleOffset && absoluteOffset >= 0 ) {
510- return scrollingBy ;
511+ // Java is down-casting and scrollingBy is a float. Therefore, we need to use ceil() or floor()
512+ // to find out the nearest integer of pixels.
513+ return scrollingBy > 0.0f ? (int ) Math .ceil (scrollingBy ) : (int ) Math .floor (scrollingBy );
511514 } else {
512- return 0 ;
515+ // When scrolling the last part and its absoluteOffset is out of range of scrollbarLength,
516+ // we still can scroll by its remaining scrollOffset.
517+ return absoluteOffset < 0.0f ? -scrollOffset : totalPossibleOffset - scrollOffset ;
513518 }
514519 }
515520
0 commit comments