-
- if( itemCount > maxItemIndex && currentItem < itemBase + 2 && itemBase > 0 )
- itemBase = currentItem - 2;
- if( itemCount > maxItemIndex && currentItem > itemBase + itemCount - 2 && itemBase < maxItemIndex-1 )
- itemBase = currentItem - itemCount + 2;
+
+ // Scroll only if needed
+ if( items_in_view < maxItemIndex )
+ {
+ // - If the current item is above the second item shown, and we're not at the top
+ if( currentItem < itemBase + 2 && itemBase > 0 ) {
+ itemBase = currentItem - 2;
+ if(itemBase < 0) itemBase = 0;
+ }
+ // - If the current item is below the second item show, and we're not at the bottom
+ if( currentItem > itemBase + items_in_view - 2 && itemBase + items_in_view < maxItemIndex ) {
+ itemBase = currentItem - items_in_view + 2;
+ if( itemBase > maxItemIndex - items_in_view )
+ itemBase = maxItemIndex - items_in_view;
+ }
+ }