Example usage for android.support.v4.view MotionEventCompat getActionMasked

List of usage examples for android.support.v4.view MotionEventCompat getActionMasked

Introduction

In this page you can find the example usage for android.support.v4.view MotionEventCompat getActionMasked.

Prototype

public static int getActionMasked(MotionEvent event) 

Source Link

Document

Call MotionEvent#getAction , returning only the #ACTION_MASK portion.

Usage

From source file:com.ayuget.redface.ui.activity.ReplyActivity.java

/**
 * Initializes both the smiley selector/*from   w  w w .  j a v a 2 s. co m*/
 */
protected void setupSmileySelector() {
    smileyList.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int action = MotionEventCompat.getActionMasked(event);

            switch (action) {
            case MotionEvent.ACTION_DOWN: {
                final int pointerIndex = MotionEventCompat.getActionIndex(event);
                lastTouchY = MotionEventCompat.getY(event, pointerIndex);
                activePointerId = MotionEventCompat.getPointerId(event, 0);
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                if (smileyList.getScrollY() == 0) {
                    final int pointerIndex = MotionEventCompat.findPointerIndex(event, activePointerId);

                    if (pointerIndex != -1) {
                        final float y = MotionEventCompat.getY(event, pointerIndex);

                        // Distance
                        float dy = y - lastTouchY;
                        isUpwardMovement = dy < 0;
                        float targetY = smileysSelector.getY() + dy;

                        if (targetY < toolbarHeight) {
                            float difference = toolbarHeight - targetY;
                            dy += difference;
                        } else if (targetY > smileySelectorTopOffset) {
                            float difference = targetY - smileySelectorTopOffset;
                            dy -= difference;
                        }

                        smileysSelector.setY(smileysSelector.getY() + dy);

                        // Show or hide the smileys toolbar based on current position
                        if (isUpwardMovement && smileysSelector.getY() < replyWindowMaxHeight) {
                            showSmileysToolbar();
                        } else {
                            hideSmileysToolbar();
                        }
                    }

                    break;
                }
            }
            case MotionEvent.ACTION_UP: {
                int upAnimationThreshold = replyWindowMaxHeight - toolbarHeight;

                float yTranslation;
                ViewPropertyAnimator viewPropertyAnimator = smileysSelector.animate();

                if (isUpwardMovement && smileysSelector.getY() == upAnimationThreshold) {
                    // Do not move in that case
                    yTranslation = 0;
                } else if (isUpwardMovement && smileysSelector.getY() < upAnimationThreshold) {
                    // Moving too far, let's avoid this
                    yTranslation = -(smileysSelector.getY() - toolbarHeight);
                } else {
                    // Replace the smiley selector at its original position
                    yTranslation = smileySelectorTopOffset - smileysSelector.getY();
                }

                if (yTranslation != 0) {
                    viewPropertyAnimator.translationYBy(yTranslation).setDuration(150).start();
                }

                break;
            }
            }

            boolean touchConsumed;
            if (smileysSelector.getY() != smileySelectorTopOffset) {
                touchConsumed = (smileysSelector.getY() != toolbarHeight);
            } else {
                touchConsumed = false;
            }

            return touchConsumed;
        }
    });
}

From source file:com.wordplat.ikvstockchart.InteractiveKLineView.java

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    boolean onHorizontalMove = gestureCompat.onTouchEvent(event, event.getX(), event.getY());
    final int action = MotionEventCompat.getActionMasked(event);

    onVerticalMove = false;/*from w  w w  .  j a v  a  2s  . c  om*/

    if (action == MotionEvent.ACTION_MOVE) {
        if (!onHorizontalMove && !onLongPress && !onDoubleFingerPress && gestureCompat.isDragging()) {
            onTouch = false;
            onVerticalMove = true;
        }
    }

    getParent().requestDisallowInterceptTouchEvent(!onVerticalMove);

    return super.dispatchTouchEvent(event);
}

From source file:com.baitouwei.swiperefresh.ASwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isDragging) {
        return super.onTouchEvent(ev);
    }//from  www .  j  av  a 2  s.com

    final int action = MotionEventCompat.getActionMasked(ev);
    switch (action) {
    case MotionEvent.ACTION_MOVE:
        final float y = getMotionEventY(ev, activePointerId);
        final float dragOffset = (int) (y - initialMotionY);

        if (isSwipeStatusEnsure) {
            if ((dragOffset < 0 && isSwipeDown == true) || (dragOffset > 0 && isSwipeDown == false)) {
                return false;
            }
        }
        if (dragOffset > 0) {//SwipeDown,PS:dragPercent and dragOffset is positive
            if (headerView.getVisibility() != VISIBLE) {
                headerView.setVisibility(VISIBLE);
            }

            isSwipeDown = true;
            dragPercent = dragOffset / (contentViewSwipeDownOffsetRange);
            if (dampAbleOfSwipeDown) {
                int targetY = dampCompute(Math.abs(dragOffset * parallaxFactorOfContentSwipeDown),
                        contentViewSwipeDownOffsetRange, swipeDownDampRate);
                offsetContent(targetY - currentContentOffset, 1);
            } else {
                if (dragOffset < contentViewSwipeDownOffsetRange) {
                    offsetContent((int) dragOffset - currentContentOffset, parallaxFactorOfContentSwipeDown);
                }
            }
            offsetHeader((int) (dragPercent * headerView.getOffsetRange() - headerView.getCurrentOffset()));

            if (swipeLayoutConfigListener.isReadyToRefresh(this, dragOffset)) {
                headerView.updateStatus(SwipeStatus.READY);
            } else {
                headerView.updateStatus(SwipeStatus.NORMAL);
            }
            headerView.updatePercent(dragPercent);
        } else {//SwipeUp,PS:dragPercent and dragOffset is negative
            if (footerView.getVisibility() != VISIBLE) {
                footerView.setVisibility(VISIBLE);
            }

            isSwipeDown = false;
            dragPercent = dragOffset / (contentViewSwipeUpOffsetRange);

            if (dampAbleOfSwipeUp) {
                int targetY = dampCompute(Math.abs(dragOffset * parallaxFactorOfContentSwipeUp),
                        Math.abs(contentViewSwipeUpOffsetRange), swipeUpDampRate);
                offsetContent(-targetY - currentContentOffset, 1);
            } else {
                if (dragOffset > contentViewSwipeUpOffsetRange) {
                    offsetContent((int) dragOffset - currentContentOffset, parallaxFactorOfContentSwipeUp);
                }
            }
            offsetFooter((int) (dragPercent * footerView.getOffsetRange() - footerView.getCurrentOffset()));

            if (swipeLayoutConfigListener.isReadyToLoadMore(this, dragOffset)) {
                footerView.updateStatus(SwipeStatus.READY);
            } else {
                footerView.updateStatus(SwipeStatus.NORMAL);
            }
            footerView.updatePercent(dragPercent);
        }
        if (isSwipeStatusEnsure == false) {
            isSwipeStatusEnsure = true;
        }
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (isSwipeDown) {
            if (headerView.getSwipeStatus() == SwipeStatus.READY) {
                refreshStart();
            } else {
                animateContentOffsetToPos(true, true, new OnAnimateContentOffsetListener() {
                    @Override
                    public void onOffsetEnd() {
                        headerView.updateStatus(SwipeStatus.NORMAL);
                    }
                });
                animateHeaderOffsetToPos(true, new OnAnimateContentOffsetListener() {
                    @Override
                    public void onOffsetEnd() {
                        headerView.updateStatus(SwipeStatus.NORMAL);
                    }
                });
            }
        } else {
            if (footerView.getSwipeStatus() == SwipeStatus.READY) {
                loadMoreStart();
            } else {
                animateContentOffsetToPos(true, false, new OnAnimateContentOffsetListener() {
                    @Override
                    public void onOffsetEnd() {
                        footerView.updateStatus(SwipeStatus.NORMAL);
                    }
                });
                animateFooterOffsetToPos(true, new OnAnimateContentOffsetListener() {
                    @Override
                    public void onOffsetEnd() {
                        footerView.updateStatus(SwipeStatus.NORMAL);
                    }
                });
            }
        }

        isDragging = false;
        activePointerId = INVALID_POINTER;
        isSwipeStatusEnsure = false;
        break;
    }

    return true;
}

From source file:com.lovejjfg.powerrefresh.PowerRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    int pointerIndex;

    if (!isEnabled() || !canChildScrollUp() || isLoading || isRefreshing || mNestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }/*w  w w . j a  v a2 s.  c om*/

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = ev.getPointerId(0);
        mIsBeingDragged = false;
        break;

    case MotionEvent.ACTION_MOVE: {
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }

        final float y = ev.getY(pointerIndex);
        startDragging(y);
        //in this case ,just can refresh.
        if (mIsBeingDragged) {
            final float overscrollTop = (y - mInitialMotionY);
            if (overscrollTop < 0 && getScrollY() > 0) {
                ev.setAction(MotionEvent.ACTION_CANCEL);
                return false;
            }
            currentStatus = STATE_REFRESH;
            goToRefresh((int) overscrollTop);
        }
        mInitialMotionY = y;
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        pointerIndex = MotionEventCompat.getActionIndex(ev);
        if (pointerIndex < 0) {
            return false;
        }
        mActivePointerId = ev.getPointerId(pointerIndex);
        break;
    }

    //            case MotionEventCompat.ACTION_POINTER_UP:
    //                onSecondaryPointerUp(ev);
    //                break;

    case MotionEvent.ACTION_UP: {
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }

        if (mIsBeingDragged) {
            mIsBeingDragged = false;
            resetScroll();
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    case MotionEvent.ACTION_CANCEL:
        resetScroll();
        return false;
    }

    return true;
}

From source file:com.wordplat.ikvstockchart.InteractiveKLineView.java

@Override
public boolean onTouchEvent(MotionEvent e) {
    final int action = MotionEventCompat.getActionMasked(e);

    gestureDetector.onTouchEvent(e);/*w w  w .  j a v a  2  s  .  c  o m*/
    scaleDetector.onTouchEvent(e);

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        onTouch = true;
        onDragging = false;
        break;
    }

    case MotionEvent.ACTION_POINTER_DOWN: {
        onDoubleFingerPress = true;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        onDragging = true;
        if (onLongPress) {
            highlight(e.getX(), e.getY());
        }
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        onLongPress = false;
        onDoubleFingerPress = false;
        onTouch = false;
        onDragging = false;

        cancelHighlight();

        break;
    }
    }

    return true;
}

From source file:com.gu.swiperefresh.SwipeRefreshPlush.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();//from   w  ww . j  a  va 2s.  c o m

    final int action = MotionEventCompat.getActionMasked(ev);
    int pointerIndex;

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }
    if (!isEnabled() || (!canLoadMore() && !canRefresh()) || mReturningToStart || mRefreshController.isRefresh()
            || mNestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mRefreshController.setTargetOffsetTopAndBottom(
                mRefreshController.getCurrentTargetOffsetTop() - mRefreshView.getTop(), true);
        mActivePointerId = ev.getPointerId(0);
        mIsBeingDragUp = false;
        mIsBeingDragDown = false;

        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }
        mInitialDownY = ev.getY(pointerIndex);
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        break;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }
        mVelocityTracker.addMovement(ev);
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            return false;
        }
        final float y = ev.getY(pointerIndex);
        startDragging(y);
        break;

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        float initialVelocity = velocityTracker.getYVelocity(mActivePointerId);
        Log.d(TAG, "fling:" + initialVelocity);
        if (Math.abs(initialVelocity) > mMinimumVelocity) {
            flingWithNestedDispatch(0, -initialVelocity);
        }
        releaseVelocityTracker();
        break;
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragUp = false;
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return mIsBeingDragUp || mIsBeingDragDown;
}

From source file:com.hayukleung.app.view.refresh.SwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/* w  w w . ja  v  a2s  . c  o  m*/
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        break;

    case MotionEvent.ACTION_MOVE:
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = y - mInitialMotionY;

        if (!mIsBeingDragged && yDiff > mTouchSlop) {
            mIsBeingDragged = true;
        }

        if (mIsBeingDragged) {
            // User velocity passed min velocity; trigger a refresh
            if (yDiff > mDistanceToTriggerSync) {
                // User movement passed distance; trigger a refresh
                mIsRefreshable = true;
            } else {
                // Just track the user's movement
                mIsRefreshable = false;
                if (mLastMotionY > y && mTarget.getTop() == getPaddingTop()) {
                    // If the user puts the view back at the top, we
                    // don't need to. This shouldn't be considered
                    // cancelling the gesture as the user can restart from the top.
                    removeCallbacks(mCancel);
                } else {
                    updatePositionTimeout();
                }
            }
            float diff = Math.min(yDiff, mDistanceToTriggerSync);
            setTriggerPercentage(mAccelerateInterpolator.getInterpolation(diff / mDistanceToTriggerSync));
            updateContentOffsetTop((int) (yDiff));
            mLastMotionY = y;
        }
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionY = MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mIsRefreshable) {
            startRefresh();
        }
        mIsRefreshable = false;
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        mActivePointerId = INVALID_POINTER;
        mCancel.run();
        return false;
    }

    return true;
}

From source file:com.sohu.xzd.widget.SwipeRefresh.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/*from   w ww  .j av a  2 s. c o m*/
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        break;

    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        if (mIsBeingDragged) {
            float originalDragPercent = overscrollTop / mTotalDragDistance;
            if (originalDragPercent < 0) {
                return false;
            }
            float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
            float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
            float extraOS = Math.abs(overscrollTop) - mTotalDragDistance;
            float slingshotDist = mSpinnerFinalOffset;
            float tensionSlingshotPercent = Math.max(0, Math.min(extraOS, slingshotDist * 2) / slingshotDist);
            float tensionPercent = (float) ((tensionSlingshotPercent / 4)
                    - Math.pow((tensionSlingshotPercent / 4), 2)) * 2f;
            float extraMove = (slingshotDist) * tensionPercent * 2;

            int targetY = mOriginalTargetOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
            if (mRefreshHeader.getVisibility() != View.VISIBLE) {
                mRefreshHeader.setVisibility(View.VISIBLE);
            }
            if (overscrollTop < mTotalDragDistance) {

            } else {

            }
            float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
            mRefreshHeader.onPull(overscrollTop, mTotalDragDistance, rotation);
            setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop, true /* requires update */);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        if (mActivePointerId == INVALID_POINTER) {
            if (action == MotionEvent.ACTION_UP) {
                Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            }
            return false;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
        mIsBeingDragged = false;
        if (overscrollTop > mTotalDragDistance) {
            setRefreshing(true, true /* notify */);
        } else {
            mRefreshing = false;
            AnimationListener listener = null;
            listener = new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    startScaleDownAnimation(null);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            };
            animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    }

    return true;
}

From source file:com.linsq.androiddemo.refresh2.SwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);// ?
    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        // ???/*from   www  .j  av  a2 s . c om*/
        mReturningToStart = false;
    }
    if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
        // viewGroup????view??
        // view
        return false;
    }
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        mCurrPercentage = 0;
        break;
    case MotionEvent.ACTION_MOVE:
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            // ???
            Logger.e("Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = y - mInitialMotionY;// down??
        if (!mIsBeingDragged && yDiff > mTouchSlop) {
            mIsBeingDragged = true;
        }
        if (mIsBeingDragged) {
            if (yDiff > mDistanceToTriggerSync) {
                // ????starRefresh
                startRefresh();
            } else {
                // ????
                // ?
                setTriggerPercentage(mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                updateContentOffsetTop((int) (yDiff));// view??
                if (mLastMotionY > y && mTarget.getTop() == getPaddingTop()) {
                    // ?????
                    removeCallbacks(mCancel);
                } else {
                    updatePositionTimeout();
                }
            }
            mLastMotionY = y;
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {// ????
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionY = MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);// ?
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    return true;
}

From source file:com.antew.redditinpictures.library.widget.SwipeListView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Store width of this list for usage of swipe distance detection
    if (mViewWidth < 2) {
        mViewWidth = getWidth();//from   w w  w .  j  a v  a  2s.  c  o m
    }

    switch (MotionEventCompat.getActionMasked(event)) {
    case MotionEvent.ACTION_DOWN:
        int[] viewCoords = new int[2];
        // Figure out where the touch occurred.
        getLocationOnScreen(viewCoords);

        int touchX = (int) event.getRawX() - viewCoords[0];
        int touchY = (int) event.getRawY() - viewCoords[1];

        Rect rect = new Rect();
        View child;

        int childCount = getChildCount();
        for (int i = getHeaderViewsCount(); i <= childCount; i++) {
            // Go through each child view (excluding headers) and see if our touch pressed it.
            child = getChildAt(i);

            if (child != null) {
                //Get the child hit rectangle.
                child.getHitRect(rect);
                //If the child would be hit by this press.
                if (rect.contains(touchX, touchY)) {
                    // DIRECT HIT! You sunk my battleship. Now that we know which view was touched, store it off for use if a move occurs.
                    View frontView = child.findViewById(mFrontViewId);
                    View backView = child.findViewById(mBackViewId);
                    // Create our view pair.
                    mViewPair = new SwipeableViewPair(frontView, backView);
                    break;
                }
            }
        }

        if (mViewPair != null) {
            // If we have a view pair, record details about the inital touch for use later.
            mDownX = event.getRawX();
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(event);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mVelocityTracker != null) {
            // Add the movement so we can calculate velocity.
            mVelocityTracker.addMovement(event);
            mVelocityTracker.computeCurrentVelocity(1000);

            float deltaX = event.getRawX() - mDownX;
            float velocityX = Math.abs(mVelocityTracker.getXVelocity());
            float velocityY = Math.abs(mVelocityTracker.getYVelocity());

            if (mViewPair != null) {
                boolean shouldSwipe = false;

                // If the view has been moved a significant enough distance or if the view was flung, check to see if we should swipe it.
                if ((Math.abs(deltaX) > mViewWidth / 2 && mState == State.SWIPING)
                        || (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity
                                && velocityX > velocityY)) {
                    if (mSwipeDirection == SWIPE_DIRECTION_BOTH) {
                        // If the list is setup to swipe in either direction, just let it go.
                        shouldSwipe = true;
                    } else if (mSwipeDirection == SWIPE_DIRECTION_LEFT && deltaX < 0) {
                        // If the list is only setup to swipe left, then only allow swiping to the left.
                        shouldSwipe = true;
                    } else if (mSwipeDirection == SWIPE_DIRECTION_RIGHT && deltaX > 0) {
                        // If the list is only setup to swipe right, then only allow swiping to the right.
                        shouldSwipe = true;
                    }
                }

                if (shouldSwipe) {
                    // If a swipe should occur meaning someone has let go of a view they were moving and it was far/fast enough for us to consider it a swipe start the animations.
                    mViewPair.mFrontView.animate().translationX(deltaX >= 0 ? mViewWidth : -mViewWidth).alpha(0)
                            .setDuration(mAnimationTime);
                    mViewPair.mBackView.animate().alpha(1).setDuration(mAnimationTime);
                    // Now that the item is open, store it off so we can close it when we scroll if needed.
                    mSwipedViews.put(mViewPair.hashCode(), mViewPair);
                    // Clear out current variables as they are no longer needed and recycle the velocity tracker.
                    resetState();
                } else {
                    // If the user let go of the view and we don't think the swipe was intended to occur (it was cancelled basically) reset the views.
                    // Make sure the back disappears, since if it has buttons these can intercept touches from the front view.
                    mViewPair.mBackView.setVisibility(View.GONE);
                    mViewPair.mFrontView.animate().translationX(0).alpha(1).setDuration(mAnimationTime);
                    // Clear out current variables as they are no longer needed and recycle the velocity tracker.
                    resetState();
                }
            }
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (mVelocityTracker != null && mState != State.SCROLLING) {
            // If this is an initial movement and we aren't already swiping.
            // Add the movement so we can calculate velocity.
            mVelocityTracker.addMovement(event);
            mVelocityTracker.computeCurrentVelocity(1000);

            float deltaX = event.getRawX() - mDownX;
            float velocityX = Math.abs(mVelocityTracker.getXVelocity());
            float velocityY = Math.abs(mVelocityTracker.getYVelocity());

            // If the movement has been more than what is considered slop, and they are clearing moving horizontal not vertical.
            if (Math.abs(deltaX) > mTouchSlop && velocityX > velocityY) {
                boolean initiateSwiping = false;

                if (mSwipeDirection == SWIPE_DIRECTION_BOTH) {
                    // If the list is setup to swipe in either direction, just let it go.
                    initiateSwiping = true;
                } else if (mSwipeDirection == SWIPE_DIRECTION_LEFT && deltaX < 0) {
                    // If the list is only setup to swipe left, then only allow swiping to the left.
                    initiateSwiping = true;
                } else if (mSwipeDirection == SWIPE_DIRECTION_RIGHT && deltaX > 0) {
                    // If the list is only setup to swipe right, then only allow swiping to the right.
                    initiateSwiping = true;
                }

                if (initiateSwiping) {
                    ViewParent parent = getParent();
                    if (parent != null) {
                        // Don't allow parent to intercept touch (prevents NavigationDrawers from intercepting when near the bezel).
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                    // Change our state to swiping to start tranforming the item.
                    changeState(State.SWIPING);
                    // Make sure that touches aren't intercepted.
                    requestDisallowInterceptTouchEvent(true);

                    // Cancel ListView's touch to prevent it from being focused.
                    MotionEvent cancelEvent = MotionEvent.obtain(event);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                            | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                    super.onTouchEvent(cancelEvent);
                } else {
                    // Otherwise we need to cancel the touch event to prevent accidentally selecting the item and also preventing the swipe in the wrong direction or an incomplete touch from moving the view.
                    MotionEvent cancelEvent = MotionEvent.obtain(event);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                            | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                    super.onTouchEvent(cancelEvent);
                }
            }

            if (mState == State.SWIPING && mViewPair != null) {
                // Make sure the back is visible.
                mViewPair.mBackView.setVisibility(View.VISIBLE);
                //Fade the back in and front out as they move.
                mViewPair.mBackView.setAlpha(Math.min(1f, 2f * Math.abs(deltaX) / mViewWidth));
                mViewPair.mFrontView.setTranslationX(deltaX);
                mViewPair.mFrontView
                        .setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
                return true;
            }
        }
        break;
    }
    return super.onTouchEvent(event);
}