@@ -498,18 +498,23 @@ private void horizontalScrollTo(float x) {
498
498
499
499
private int scrollTo (float oldDragPos , float newDragPos , int [] scrollbarRange , int scrollRange ,
500
500
int scrollOffset , int viewLength ) {
501
- int scrollbarLength = scrollbarRange [1 ] - scrollbarRange [0 ];
501
+ final int scrollbarLength = scrollbarRange [1 ] - scrollbarRange [0 ];
502
502
if (scrollbarLength == 0 ) {
503
503
return 0 ;
504
504
}
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
+
509
510
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 );
511
514
} 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 ;
513
518
}
514
519
}
515
520
0 commit comments