private void scrollToItem(int item, boolean smoothScroll, int velocity) { final ItemInfo curInfo = infoForPosition(item); int destX = 0; if (curInfo != null) { destX = (int) (getClientWidth() * Math.max(firstOffset, Math.min(curInfo.offset, lastOffset))); } if (smoothScroll) { smoothScrollTo(destX, 0, velocity); } else { completeScroll(false); scrollTo(destX, 0); pageScrolled(destX); } }
@Override public void computeScroll() { if (!scroller.isFinished() && scroller.computeScrollOffset()) { int oldX = getScrollX(); int oldY = getScrollY(); int x = scroller.getCurrX(); int y = scroller.getCurrY(); if (oldX != x || oldY != y) { scrollTo(x, y); if (!pageScrolled(x)) { scroller.abortAnimation(); scrollTo(0, y); } } // Keep on drawing until the animation has finished. ViewCompat.postInvalidateOnAnimation(this); return; } // Done with scroll, clean up state. completeScroll(true); }
final int dy = y - sy; if (dx == 0 && dy == 0) { completeScroll(false); populate(); setScrollState(SCROLL_STATE_IDLE);
private void recomputeScrollPosition(int width, int oldWidth, int margin, int oldMargin) { if (oldWidth > 0 && !items.isEmpty()) { final int widthWithMargin = width - getPaddingLeft() - getPaddingRight() + margin; final int oldWidthWithMargin = oldWidth - getPaddingLeft() - getPaddingRight() + oldMargin; final int xpos = getScrollX(); final float pageOffset = (float) xpos / oldWidthWithMargin; final int newOffsetPixels = (int) (pageOffset * widthWithMargin); scrollTo(newOffsetPixels, getScrollY()); if (!scroller.isFinished()) { // We now return to your regularly scheduled scroll, already in progress. final int newDuration = scroller.getDuration() - scroller.timePassed(); ItemInfo targetInfo = infoForPosition(currentItem); if (targetInfo != null) { scroller.startScroll(newOffsetPixels, 0, (int) (targetInfo.offset * width), 0, newDuration); } } } else { final ItemInfo ii = infoForPosition(currentItem); final float scrollOffset = ii != null ? Math.min(ii.offset, lastOffset) : 0; final int scrollPos = (int) (scrollOffset * (width - getPaddingLeft() - getPaddingRight())); if (scrollPos != getScrollX()) { completeScroll(false); scrollTo(scrollPos, getScrollY()); } } }