Example usage for android.widget EdgeEffect onPull

List of usage examples for android.widget EdgeEffect onPull

Introduction

In this page you can find the example usage for android.widget EdgeEffect onPull.

Prototype

public void onPull(float deltaDistance) 

Source Link

Document

A view should call this when content is pulled away from an edge by the user.

Usage

From source file:net.simonvt.staggeredgridview.StaggeredGridView.java

/**
 * @param deltaY Pixels that content should move by
 * @return true if the movement completed, false if it was stopped prematurely.
 *//*from   ww  w  .  j a va 2  s .  c o  m*/
private boolean trackMotionScroll(int deltaY, boolean allowOverScroll) {
    final boolean contentFits = contentFits();
    final int allowOverhang = Math.abs(deltaY);

    final int overScrolledBy;
    final int movedBy;
    if (!contentFits) {
        final int overhang;
        final boolean up;
        populating = true;
        if (deltaY > 0) {
            overhang = fillUp(firstPosition - 1, allowOverhang);
            up = true;
        } else {
            overhang = fillDown(firstPosition + getChildCount(), allowOverhang);
            up = false;
        }
        movedBy = Math.min(overhang, allowOverhang);
        offsetChildren(up ? movedBy : -movedBy);
        recycleOffscreenViews();
        populating = false;
        overScrolledBy = allowOverhang - overhang;
    } else {
        overScrolledBy = allowOverhang;
        movedBy = 0;
    }

    if (allowOverScroll) {
        final int overScrollMode = getOverScrollMode();

        if (overScrollMode == OVER_SCROLL_ALWAYS
                || (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && !contentFits)) {

            if (overScrolledBy > 0) {
                EdgeEffect edge = deltaY > 0 ? topEdge : bottomEdge;
                edge.onPull((float) Math.abs(deltaY) / getHeight());
                postInvalidateOnAnimation();
            }
        }
    }

    if (!awakenScrollBars()) {
        invalidate();
    }

    correctTooLow();

    return deltaY == 0 || movedBy != 0;
}

From source file:com.android.internal.widget.ViewPager.java

private boolean performDrag(float x) {
    boolean needsInvalidate = false;

    final int width = getPaddedWidth();
    final float deltaX = mLastMotionX - x;
    mLastMotionX = x;/*from   ww  w  .  j a  v a  2  s  . c o m*/

    final EdgeEffect startEdge;
    final EdgeEffect endEdge;
    if (isLayoutRtl()) {
        startEdge = mRightEdge;
        endEdge = mLeftEdge;
    } else {
        startEdge = mLeftEdge;
        endEdge = mRightEdge;
    }

    // Translate scroll to relative coordinates.
    final float nextScrollX = getScrollX() + deltaX;
    final float scrollStart;
    if (isLayoutRtl()) {
        scrollStart = MAX_SCROLL_X - nextScrollX;
    } else {
        scrollStart = nextScrollX;
    }

    final float startBound;
    final ItemInfo startItem = mItems.get(0);
    final boolean startAbsolute = startItem.position == 0;
    if (startAbsolute) {
        startBound = startItem.offset * width;
    } else {
        startBound = width * mFirstOffset;
    }

    final float endBound;
    final ItemInfo endItem = mItems.get(mItems.size() - 1);
    final boolean endAbsolute = endItem.position == mAdapter.getCount() - 1;
    if (endAbsolute) {
        endBound = endItem.offset * width;
    } else {
        endBound = width * mLastOffset;
    }

    final float clampedScrollStart;
    if (scrollStart < startBound) {
        if (startAbsolute) {
            final float over = startBound - scrollStart;
            startEdge.onPull(Math.abs(over) / width);
            needsInvalidate = true;
        }
        clampedScrollStart = startBound;
    } else if (scrollStart > endBound) {
        if (endAbsolute) {
            final float over = scrollStart - endBound;
            endEdge.onPull(Math.abs(over) / width);
            needsInvalidate = true;
        }
        clampedScrollStart = endBound;
    } else {
        clampedScrollStart = scrollStart;
    }

    // Translate back to absolute coordinates.
    final float targetScrollX;
    if (isLayoutRtl()) {
        targetScrollX = MAX_SCROLL_X - clampedScrollStart;
    } else {
        targetScrollX = clampedScrollStart;
    }

    // Don't lose the rounded component.
    mLastMotionX += targetScrollX - (int) targetScrollX;

    scrollTo((int) targetScrollX, getScrollY());
    pageScrolled((int) targetScrollX);

    return needsInvalidate;
}