Java Code Examples for android.support.v7.widget.RecyclerView#scrollBy()

The following examples show how to use android.support.v7.widget.RecyclerView#scrollBy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ItemDragHelper.java    From MultiItem with Apache License 2.0 6 votes vote down vote up
/**
     * 当用户滚动到边缘的时候,计算是否需要滚动
     */
    private boolean scrollIfNecessary(RecyclerView recyclerView, int curX, int curY) {
        if (!isDrag) {
            return false;
        }

        RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();
        int scrollX = 0;
        int scrollY = 0;
        if (lm.canScrollHorizontally()) {
            scrollX = dragListener.calcHorizontalScrollDistance(recyclerView, curX, curY);
        }
        if (lm.canScrollVertically()) {
            scrollY = dragListener.calcVerticalScrollDistance(recyclerView, curX, curY);
        }
//        System.out.println("scroll:::::" + scrollY + "=" + recyclerView.getScrollY() + "curY::" + curY);
//        System.out.println("scroll:::::" + scrollX + "=" + recyclerView.getScrollX() + "curX::" + curX);

        if (scrollX != 0 || scrollY != 0) {
            recyclerView.scrollBy(scrollX, scrollY);
        }
        return scrollX != 0 || scrollY != 0;
    }
 
Example 2
Source File: Utils.java    From Expert-Android-Programming with MIT License 6 votes vote down vote up
public static boolean syncOffset(SmoothAppBarLayout smoothAppBarLayout, View target, int verticalOffset, View scroll) {
  boolean isSelected = target == scroll;
  if (scroll instanceof NestedScrollView) {
    NestedScrollView nestedScrollView = (NestedScrollView) scroll;
    if (nestedScrollView.getScrollY() < verticalOffset || (!isSelected && isScrollToTop(target))) {
      nestedScrollView.scrollTo(0, verticalOffset);
    }
    if (isSelected && (nestedScrollView.getScrollY() < verticalOffset || verticalOffset == 0)) {
      nestedScrollView.scrollTo(0, 0);
      smoothAppBarLayout.syncOffset(0);
    }
  } else if (scroll instanceof RecyclerView) {
    RecyclerView recyclerView = (RecyclerView) scroll;
    boolean isAccuracy = recyclerView.getLayoutManager().findViewByPosition(ObservableRecyclerView.HEADER_VIEW_POSITION) != null;
    if (isAccuracy && recyclerView.computeVerticalScrollOffset() < verticalOffset) {
      recyclerView.scrollBy(0, verticalOffset - recyclerView.computeVerticalScrollOffset());
    } else if (!isSelected && isScrollToTop(target)) {
      recyclerView.scrollToPosition(ObservableRecyclerView.HEADER_VIEW_POSITION);
    }
    if (isAccuracy && isSelected && (recyclerView.computeVerticalScrollOffset() < verticalOffset || verticalOffset == 0)) {
      recyclerView.scrollToPosition(ObservableRecyclerView.HEADER_VIEW_POSITION);
      smoothAppBarLayout.syncOffset(0);
    }
  }
  return true;
}
 
Example 3
Source File: Utils.java    From smooth-app-bar-layout with Apache License 2.0 6 votes vote down vote up
public static boolean syncOffset(SmoothAppBarLayout smoothAppBarLayout, View target, int verticalOffset, View scroll) {
  boolean isSelected = target == scroll;
  if (scroll instanceof NestedScrollView) {
    NestedScrollView nestedScrollView = (NestedScrollView) scroll;
    if (nestedScrollView.getScrollY() < verticalOffset || (!isSelected && isScrollToTop(target))) {
      nestedScrollView.scrollTo(0, verticalOffset);
    }
    if (isSelected && (nestedScrollView.getScrollY() < verticalOffset || verticalOffset == 0)) {
      nestedScrollView.scrollTo(0, 0);
      smoothAppBarLayout.syncOffset(0);
    }
  } else if (scroll instanceof RecyclerView) {
    RecyclerView recyclerView = (RecyclerView) scroll;
    boolean isAccuracy = recyclerView.getLayoutManager().findViewByPosition(ObservableRecyclerView.HEADER_VIEW_POSITION) != null;
    if (isAccuracy && recyclerView.computeVerticalScrollOffset() < verticalOffset) {
      recyclerView.scrollBy(0, verticalOffset - recyclerView.computeVerticalScrollOffset());
    } else if (!isSelected && isScrollToTop(target)) {
      recyclerView.scrollToPosition(ObservableRecyclerView.HEADER_VIEW_POSITION);
    }
    if (isAccuracy && isSelected && (recyclerView.computeVerticalScrollOffset() < verticalOffset || verticalOffset == 0)) {
      recyclerView.scrollToPosition(ObservableRecyclerView.HEADER_VIEW_POSITION);
      smoothAppBarLayout.syncOffset(0);
    }
  }
  return true;
}
 
Example 4
Source File: RecyclerViewScrollActivity.java    From AndroidDemo with MIT License 5 votes vote down vote up
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (isMove && newState == RecyclerView.SCROLL_STATE_IDLE) {
        isMove = false;
        int top = layoutManager.findViewByPosition(scrollPosition).getTop();
        recyclerView.scrollBy(0, top);
    }
}
 
Example 5
Source File: DragSortRecycler.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    debugLog("onTouchEvent");

    if ((e.getAction() == MotionEvent.ACTION_UP) ||
            (e.getAction() == MotionEvent.ACTION_CANCEL)) {
        if ((e.getAction() == MotionEvent.ACTION_UP) && selectedDragItemPos != -1) {
            int newPos = getNewPostion(rv);
            if (moveInterface != null)
                moveInterface.onItemMoved(selectedDragItemPos, newPos);
        }

        setIsDragging(false);
        selectedDragItemPos = -1;
        floatingItem = null;
        rv.invalidateItemDecorations();
        return;
    }


    fingerY = (int) e.getY();

    if (floatingItem != null) {
        floatingItemBounds.top = fingerY - fingerOffsetInViewY;

        if (floatingItemBounds.top < -floatingItemStatingBounds.height() / 2) //Allow half the view out the top
            floatingItemBounds.top = -floatingItemStatingBounds.height() / 2;

        floatingItemBounds.bottom = floatingItemBounds.top + floatingItemStatingBounds.height();

        floatingItem.setBounds(floatingItemBounds);
    }

    //Do auto scrolling at end of list
    float scrollAmount = 0;
    if (fingerY > (rv.getHeight() * (1 - autoScrollWindow))) {
        scrollAmount = (fingerY - (rv.getHeight() * (1 - autoScrollWindow)));
    } else if (fingerY < (rv.getHeight() * autoScrollWindow)) {
        scrollAmount = (fingerY - (rv.getHeight() * autoScrollWindow));
    }
    debugLog("Scroll: " + scrollAmount);

    scrollAmount *= autoScrollSpeed;
    rv.scrollBy(0, (int) scrollAmount);

    rv.invalidateItemDecorations();// Redraw
}
 
Example 6
Source File: ClassifyView.java    From ClassifyView with Apache License 2.0 4 votes vote down vote up
/**
 * If user drags the view to the edge, trigger a scroll if necessary.
 */
private boolean scrollIfNecessary() {
    RecyclerView recyclerView = null;
    if ((mRegion & IN_MAIN_REGION) != 0) {
        recyclerView = mMainRecyclerView;
    }
    if ((mRegion & IN_SUB_REGION) != 0) {
        recyclerView = mSubRecyclerView;
    }
    if (recyclerView == null) return false;
    final long now = System.currentTimeMillis();
    final long scrollDuration = mDragScrollStartTimeInMs
            == Long.MIN_VALUE ? 0 : now - mDragScrollStartTimeInMs;
    RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();

    int scrollX = 0;
    int scrollY = 0;
    if (lm.canScrollHorizontally()) {
        int curX = (int) (mSelectedStartX + mDx);
        final int leftDiff = curX - mEdgeWidth - recyclerView.getPaddingLeft();
        if (mDx < 0 && leftDiff < 0) {
            scrollX = leftDiff;
        } else if (mDx > 0) {
            final int rightDiff =
                    curX + mSelected.getWidth() + mEdgeWidth - (recyclerView.getWidth() - recyclerView.getPaddingRight());
            if (rightDiff > 0) {
                scrollX = rightDiff;
            }
        }
    }
    if (lm.canScrollVertically()) {
        int curY = (int) (mSelectedStartY + mDy);
        final int topDiff = curY - mEdgeWidth - recyclerView.getPaddingTop();
        if (mDy < 0 && topDiff < 0) {
            scrollY = topDiff;
        } else if (mDy > 0) {
            final int bottomDiff = curY + mSelected.getHeight() + mEdgeWidth -
                    (recyclerView.getHeight() - recyclerView.getPaddingBottom());
            if (bottomDiff > 0) {
                scrollY = bottomDiff;
            }
        }
    }
    if (scrollX != 0) {
        scrollX = interpolateOutOfBoundsScroll(recyclerView,
                mSelected.getWidth(), scrollX,
                recyclerView.getWidth(), scrollDuration);
    }
    if (scrollY != 0) {
        scrollY = interpolateOutOfBoundsScroll(recyclerView,
                mSelected.getHeight(), scrollY,
                recyclerView.getHeight(), scrollDuration);
    }
    if (scrollX != 0 || scrollY != 0) {
        if (mDragScrollStartTimeInMs == Long.MIN_VALUE) {
            mDragScrollStartTimeInMs = now;
        }
        recyclerView.scrollBy(scrollX, scrollY);
        return true;
    }
    mDragScrollStartTimeInMs = Long.MIN_VALUE;
    return false;
}
 
Example 7
Source File: DragSortRecycler.java    From Muzesto with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    debugLog("onTouchEvent");

    if ((e.getAction() == MotionEvent.ACTION_UP) ||
            (e.getAction() == MotionEvent.ACTION_CANCEL)) {
        if ((e.getAction() == MotionEvent.ACTION_UP) && selectedDragItemPos != -1) {
            int newPos = getNewPostion(rv);
            if (moveInterface != null)
                moveInterface.onItemMoved(selectedDragItemPos, newPos);
        }

        setIsDragging(false);
        selectedDragItemPos = -1;
        floatingItem = null;
        rv.invalidateItemDecorations();
        return;
    }


    fingerY = (int) e.getY();

    if (floatingItem != null) {
        floatingItemBounds.top = fingerY - fingerOffsetInViewY;

        if (floatingItemBounds.top < -floatingItemStatingBounds.height() / 2) //Allow half the view out the top
            floatingItemBounds.top = -floatingItemStatingBounds.height() / 2;

        floatingItemBounds.bottom = floatingItemBounds.top + floatingItemStatingBounds.height();

        floatingItem.setBounds(floatingItemBounds);
    }

    //Do auto scrolling at end of list
    float scrollAmount = 0;
    if (fingerY > (rv.getHeight() * (1 - autoScrollWindow))) {
        scrollAmount = (fingerY - (rv.getHeight() * (1 - autoScrollWindow)));
    } else if (fingerY < (rv.getHeight() * autoScrollWindow)) {
        scrollAmount = (fingerY - (rv.getHeight() * autoScrollWindow));
    }
    debugLog("Scroll: " + scrollAmount);

    scrollAmount *= autoScrollSpeed;
    rv.scrollBy(0, (int) scrollAmount);

    rv.invalidateItemDecorations();// Redraw
}
 
Example 8
Source File: RecyclerViewEspressoFactory.java    From ChipsLayoutManager with Apache License 2.0 4 votes vote down vote up
@Override
public void performAction(UiController uiController, RecyclerView recyclerView) {
    recyclerView.scrollBy(x, y);
}
 
Example 9
Source File: ExtGridLayoutManager.java    From dpad-aware-recycler-view with Apache License 2.0 4 votes vote down vote up
@Override
public boolean requestChildRectangleOnScreen(@NonNull RecyclerView parent, @NonNull View child, @NonNull Rect rect,
        boolean immediate) {
    if (!mOffsetEnabled) {
        return super.requestChildRectangleOnScreen(parent, child, rect, true);
    }

    final int parentLeft = getPaddingLeft();
    final int parentTop = getPaddingTop();
    final int parentRight = getWidth() - getPaddingRight();
    final int parentBottom = getHeight() - getPaddingBottom();
    final int childLeft = child.getLeft() + rect.left;
    final int childTop = child.getTop() + rect.top;
    final int childRight = childLeft + rect.width();
    final int childBottom = childTop + rect.height();

    int cameraLeft;
    int cameraRight;
    int cameraTop;
    int cameraBottom;

    final int cameraCenterX = (int) ((parentRight + parentLeft) * mOffsetFraction);
    final int childHalfWidth = (int) Math.ceil((childRight - childLeft) * 0.5);
    cameraLeft = cameraCenterX - childHalfWidth;
    cameraRight = cameraCenterX + childHalfWidth;

    final int cameraCenterY = (int) ((parentBottom + parentTop) * mOffsetFraction);
    final int childHalfHeight = (int) Math.ceil((childBottom - childTop) * 0.5);
    cameraTop = cameraCenterY - childHalfHeight;
    cameraBottom = cameraCenterY + childHalfHeight;

    final int offScreenLeft = Math.min(0, childLeft - cameraLeft);
    final int offScreenTop = Math.min(0, childTop - cameraTop);
    final int offScreenRight = Math.max(0, childRight - cameraRight);
    final int offScreenBottom = Math.max(0, childBottom - cameraBottom);

    // Favor the "start" layout direction over the end when bringing one side or the other
    // of a large rect into view. If we decide to bring in end because start is already
    // visible, limit the scroll such that start won't go out of bounds.
    final int dx;
    if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
        dx = offScreenRight != 0 ? offScreenRight
                : Math.max(offScreenLeft, childRight - parentRight);
    } else {
        dx = offScreenLeft != 0 ? offScreenLeft
                : Math.min(childLeft - parentLeft, offScreenRight);
    }

    // Favor bringing the top into view over the bottom. If top is already visible and
    // we should scroll to make bottom visible, make sure top does not go out of bounds.
    final int dy = offScreenTop != 0 ? offScreenTop
            : Math.min(childTop - parentTop, offScreenBottom);

    if (dx != 0 || dy != 0) {
        parent.scrollBy(dx, dy);
        return true;
    }

    return false;
}
 
Example 10
Source File: DragSortRecycler.java    From Slide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    debugLog("onTouchEvent");

    if ((e.getAction() == MotionEvent.ACTION_UP) ||
            (e.getAction() == MotionEvent.ACTION_CANCEL)) {
        if ((e.getAction() == MotionEvent.ACTION_UP) && selectedDragItemPos != -1) {
            int newPos = getNewPostion(rv);
            if (moveInterface != null)
                moveInterface.onItemMoved(selectedDragItemPos, newPos);
        }

        setIsDragging(false);
        selectedDragItemPos = -1;
        floatingItem = null;
        rv.invalidateItemDecorations();
        return;
    }


    fingerY = (int) e.getY();

    if (floatingItem != null) {
        floatingItemBounds.top = fingerY - fingerOffsetInViewY;

        if (floatingItemBounds.top < -floatingItemStatingBounds.height() / 2) //Allow half the view out the top
            floatingItemBounds.top = -floatingItemStatingBounds.height() / 2;

        floatingItemBounds.bottom = floatingItemBounds.top + floatingItemStatingBounds.height();

        floatingItem.setBounds(floatingItemBounds);
    }

    //Do auto scrolling at end of list
    float scrollAmount = 0;
    if (fingerY > (rv.getHeight() * (1 - autoScrollWindow))) {
        scrollAmount = (fingerY - (rv.getHeight() * (1 - autoScrollWindow)));
    } else if (fingerY < (rv.getHeight() * autoScrollWindow)) {
        scrollAmount = (fingerY - (rv.getHeight() * autoScrollWindow));
    }
    debugLog("Scroll: " + scrollAmount);

    scrollAmount *= autoScrollSpeed;
    rv.scrollBy(0, (int) scrollAmount);

    rv.invalidateItemDecorations();// Redraw
}
 
Example 11
Source File: DragSortRecycler.java    From DragSortRecycler with Apache License 2.0 4 votes vote down vote up
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    debugLog("onTouchEvent");

    if ((e.getAction() == MotionEvent.ACTION_UP) ||
            (e.getAction() == MotionEvent.ACTION_CANCEL))
    {
        if ((e.getAction() == MotionEvent.ACTION_UP) && selectedDragItemPos != -1)
        {
            int newPos = getNewPostion(rv);
            if (moveInterface != null)
                moveInterface.onItemMoved(selectedDragItemPos, newPos);
        }

        setIsDragging(false);
        selectedDragItemPos = -1;
        floatingItem = null;
        rv.invalidateItemDecorations();
        return;
    }


    fingerY = (int)e.getY();

    if (floatingItem!=null)
    {
        floatingItemBounds.top = fingerY - fingerOffsetInViewY;

        if (floatingItemBounds.top < -floatingItemStatingBounds.height()/2) //Allow half the view out the top
            floatingItemBounds.top = -floatingItemStatingBounds.height()/2;

        floatingItemBounds.bottom = floatingItemBounds.top + floatingItemStatingBounds.height();

        floatingItem.setBounds(floatingItemBounds);
    }

    //Do auto scrolling at end of list
    float scrollAmount=0;
    if (fingerY > (rv.getHeight() * (1-autoScrollWindow)))
    {
        scrollAmount = (fingerY - (rv.getHeight() * (1-autoScrollWindow)));
    }
    else if (fingerY < (rv.getHeight() * autoScrollWindow))
    {
        scrollAmount = (fingerY - (rv.getHeight() * autoScrollWindow));
    }
    debugLog("Scroll: " + scrollAmount);

    scrollAmount *= autoScrollSpeed;
    rv.scrollBy(0, (int)scrollAmount);

    rv.invalidateItemDecorations();// Redraw
}
 
Example 12
Source File: DragSortRecycler.java    From rox-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    debugLog("onTouchEvent");

    if ((e.getAction() == MotionEvent.ACTION_UP) ||
            (e.getAction() == MotionEvent.ACTION_CANCEL))
    {
        if ((e.getAction() == MotionEvent.ACTION_UP) && selectedDragItemPos != -1)
        {
            int newPos = getNewPostion(rv);
            if (moveInterface != null)
                moveInterface.onItemMoved(selectedDragItemPos, newPos);
        }

        setIsDragging(false);
        selectedDragItemPos = -1;
        floatingItem = null;
        rv.invalidateItemDecorations();
        return;
    }


    fingerY = (int)e.getY();

    if (floatingItem!=null)
    {
        floatingItemBounds.top = fingerY - fingerOffsetInViewY;

        if (floatingItemBounds.top < -floatingItemStatingBounds.height()/2) //Allow half the view out the top
            floatingItemBounds.top = -floatingItemStatingBounds.height()/2;

        floatingItemBounds.bottom = floatingItemBounds.top + floatingItemStatingBounds.height();

        floatingItem.setBounds(floatingItemBounds);
    }

    //Do auto scrolling at end of list
    float scrollAmount=0;
    if (fingerY > (rv.getHeight() * (1-autoScrollWindow)))
    {
        scrollAmount = (fingerY - (rv.getHeight() * (1-autoScrollWindow)));
    }
    else if (fingerY < (rv.getHeight() * autoScrollWindow))
    {
        scrollAmount = (fingerY - (rv.getHeight() * autoScrollWindow));
    }
    debugLog("Scroll: " + scrollAmount);

    scrollAmount *= autoScrollSpeed;
    rv.scrollBy(0, (int)scrollAmount);

    rv.invalidateItemDecorations();// Redraw
}