Example usage for android.view MotionEvent ACTION_CANCEL

List of usage examples for android.view MotionEvent ACTION_CANCEL

Introduction

In this page you can find the example usage for android.view MotionEvent ACTION_CANCEL.

Prototype

int ACTION_CANCEL

To view the source code for android.view MotionEvent ACTION_CANCEL.

Click Source Link

Document

Constant for #getActionMasked : The current gesture has been aborted.

Usage

From source file:br.com.devmix.baseapp.listener.OnSwipeableRecyclerViewTouchListener.java

private boolean handleTouchEvent(MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }/*w  w w.  j a v a2s. co  m*/

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            break;
        }

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mRecyclerView.getChildCount();
        int[] listViewCoords = new int[2];
        mRecyclerView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = mRecyclerView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                mDownView = child;
                break;
            }
        }

        if (mDownView != null && mAnimatingPosition != mRecyclerView.getChildAdapterPosition(mDownView)) {
            mAlpha = ViewCompat.getAlpha(mDownView);
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mRecyclerView.getChildAdapterPosition(mDownView);
            if (mSwipeListener.canSwipe(mDownPosition)) {
                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(motionEvent);
            } else {
                mDownView = null;
            }
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mVelocityTracker == null) {
            break;
        }

        if (mDownView != null && mSwiping) {
            // cancel
            ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime)
                    .setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = ListView.INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        mFinalDelta = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(mFinalDelta) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = mFinalDelta > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (mFinalDelta < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss && mDownPosition != mAnimatingPosition && mDownPosition != ListView.INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            mAnimatingPosition = mDownPosition;
            ViewCompat.animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new ViewPropertyAnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(View view) {
                            performDismiss(downView, downPosition);
                        }
                    });
        } else {
            // cancel
            ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime)
                    .setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = ListView.INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null || mPaused) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        if (!mSwiping && Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
            mSwiping = true;
            mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
        }

        if (mSwiping) {
            ViewCompat.setTranslationX(mDownView, deltaX - mSwipingSlop);
            ViewCompat.setAlpha(mDownView,
                    Math.max(0f, Math.min(mAlpha, mAlpha * (1f - Math.abs(deltaX) / mViewWidth))));
            return true;
        }
        break;
    }
    }

    return false;
}

From source file:com.android.messaging.ui.conversationlist.ConversationListSwipeHelper.java

@Override
public boolean onInterceptTouchEvent(final RecyclerView recyclerView, final MotionEvent event) {
    if (event.getPointerCount() > 1) {
        // Ignore subsequent pointers.
        return false;
    }/*ww  w.j a va  2 s .c  o m*/

    // We are not yet tracking a swipe gesture. Begin detection by spying on
    // touch events bubbling down to our children.
    final int action = event.getActionMasked();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (!hasGestureSwipeTarget()) {
            onGestureStart();

            mVelocityTracker.addMovement(event);
            mInitialX = event.getX();
            mInitialY = event.getY();

            final View viewAtPoint = mRecyclerView.findChildViewUnder(mInitialX, mInitialY);
            final ConversationListItemView child = (ConversationListItemView) viewAtPoint;
            if (viewAtPoint instanceof ConversationListItemView && child != null && child.isSwipeAnimatable()) {
                // Begin detecting swipe on the target for the rest of the gesture.
                mListItemView = child;
                if (mListItemView.isAnimating()) {
                    mListItemView = null;
                }
            } else {
                mListItemView = null;
            }
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (hasValidGestureSwipeTarget()) {
            mVelocityTracker.addMovement(event);

            final int historicalCount = event.getHistorySize();
            // First consume the historical events, then consume the current ones.
            for (int i = 0; i < historicalCount + 1; i++) {
                float currX;
                float currY;
                if (i < historicalCount) {
                    currX = event.getHistoricalX(i);
                    currY = event.getHistoricalY(i);
                } else {
                    currX = event.getX();
                    currY = event.getY();
                }
                final float deltaX = currX - mInitialX;
                final float deltaY = currY - mInitialY;
                final float absDeltaX = Math.abs(deltaX);
                final float absDeltaY = Math.abs(deltaY);

                if (!mIsSwiping && absDeltaY > mTouchSlop
                        && absDeltaY > (ERROR_FACTOR_MULTIPLIER * absDeltaX)) {
                    // Stop detecting swipe for the remainder of this gesture.
                    onGestureEnd();
                    return false;
                }

                if (absDeltaX > mTouchSlop) {
                    // Swipe detected. Return true so we can handle the gesture in
                    // onTouchEvent.
                    mIsSwiping = true;

                    // We don't want to suddenly jump the slop distance.
                    mInitialX = event.getX();
                    mInitialY = event.getY();

                    onSwipeGestureStart(mListItemView);
                    return true;
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (hasGestureSwipeTarget()) {
            onGestureEnd();
        }
        break;
    }

    // Start intercepting touch events from children if we detect a swipe.
    return mIsSwiping;
}

From source file:com.devbrackets.android.recyclerext.widget.FastScroll.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    //Filters out touch events we don't need to handle
    if (!draggingHandle && event.getAction() != MotionEvent.ACTION_DOWN) {
        return super.onTouchEvent(event);
    }/*from   w  ww . ja  va 2  s . c  om*/

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //Verifies the event is within the allowed coordinates
        if (ignoreTouchDown(event.getX(), event.getY())) {
            return false;
        }

        delayHandler.removeCallbacks(handleHideRunnable);
        updateHandleVisibility(true);

        if (bubble.getVisibility() != VISIBLE) {
            updateBubbleVisibility(true);
        }

        draggingHandle = true;
        float halfHandle = handle.getHeight() / 2F;

        fingerCenterOffset = handle.getY() + halfHandle - event.getY();
        fingerCenterOffset = boxValue(fingerCenterOffset, halfHandle);
        handle.setSelected(true);
        //Purposefully falls through

    case MotionEvent.ACTION_MOVE:
        setBubbleAndHandlePosition(event.getY() + fingerCenterOffset);
        setRecyclerViewPosition(event.getY() + fingerCenterOffset);
        return true;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        draggingHandle = false;
        handle.setSelected(false);
        hideBubbleDelayed();
        hideHandleDelayed();
        return true;
    }

    return super.onTouchEvent(event);
}

From source file:com.bytestemplar.tonedef.touchpad.TouchPadActivity.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    for (ButtonDefinition button : _buttons.buttons) {

        if (button.getButtonView().getId() == v.getId()) {

            ToneBank.Entry entry = _tonebank.getEntry(button.getId());

            if (entry != null) {

                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    entry.start();/*from  w  ww. j a  va  2s. com*/
                    // @robo
                    // append button values to EditText field
                    _et_dialing_string.append(String.valueOf(button.getId()));

                    button.getButtonView().setBackgroundResource(R.drawable.touchpadbutton_selected);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    entry.stop();
                    button.getButtonView().setBackgroundResource(R.drawable.touchpadbutton);
                    break;
                default:
                    break;
                }
            }
            break;
        }
    }
    return false;
}

From source file:com.adguard.android.commons.BrowserUtils.java

public static void showBrowserInstallDialog(final Context context) {
    // Touch listener for changing colors of CardViews
    View.OnTouchListener touchListener = new View.OnTouchListener() {
        @Override/* ww w.  j a  v  a 2  s.c o  m*/
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getActionMasked();
            if (action == MotionEvent.ACTION_DOWN) {
                ((CardView) v).setCardBackgroundColor(R.color.card_view_background_pressed);
            } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL
                    || action == MotionEvent.ACTION_OUTSIDE) {
                ((CardView) v).setCardBackgroundColor(R.color.white);
            }
            return false;
        }
    };

    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View dialogLayout = inflater.inflate(R.layout.select_browser_dialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialog);
    builder.setNegativeButton(android.R.string.cancel, null);
    builder.setView(dialogLayout);
    final AlertDialog dialog = builder.create();

    View cardView = dialogLayout.findViewById(R.id.browser_yandex);
    cardView.setOnTouchListener(touchListener);
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityUtils.startMarket(context, YANDEX_BROWSER_PACKAGE, REFERRER);
            dialog.dismiss();
        }
    });

    cardView = dialogLayout.findViewById(R.id.browser_samsung);
    cardView.setOnTouchListener(touchListener);
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityUtils.startMarket(context, SAMSUNG_BROWSER_PACKAGE, null);
            dialog.dismiss();
        }
    });

    dialog.show();
}

From source file:ch.tutti.android.bottomsheet.ResolverDrawerLayout.java

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

    if (canChildScrollUp()) {
        // Fail fast if we're not in a state where a swipe is possible
        return super.onInterceptTouchEvent(ev);
    }/*from ww w .  j  av  a  2s .  co  m*/

    if (action == MotionEvent.ACTION_DOWN) {
        mVelocityTracker.clear();
    }

    mVelocityTracker.addMovement(ev);

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        mInitialTouchX = x;
        mInitialTouchY = mLastTouchY = y;
        mOpenOnClick = isListChildUnderClipped(x, y) && mCollapsibleHeight > 0;
    }
        break;

    case MotionEvent.ACTION_MOVE: {
        final float x = ev.getX();
        final float y = ev.getY();
        final float dy = y - mInitialTouchY;
        boolean isSlidingUp = Math.abs(dy) > mTouchSlop && findChildUnder(x, y) != null && mCollapseOffset > 0;
        boolean isSlidingDown = mCollapseOffset == 0 && dy > mTouchSlop;
        if (isSlidingUp || isSlidingDown) {
            mActivePointerId = ev.getPointerId(0);
            mIsDragging = true;
            mLastTouchY = Math.max(mLastTouchY - mTouchSlop,
                    Math.min(mLastTouchY + dy, mLastTouchY + mTouchSlop));
        }
    }
        break;

    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
    }
        break;

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        resetTouch();
    }
        break;
    }

    if (mIsDragging) {
        mScroller.abortAnimation();
    }
    return mIsDragging || mOpenOnClick;
}

From source file:com.codingfeel.sm.views.superrecyclerview.swipe.SwipeDismissRecyclerViewTouchListener.java

private void caseMotionActionMove(MotionEvent motionEvent) {
    mVelocityTracker.addMovement(motionEvent);
    float deltaX = motionEvent.getRawX() - mDownX;
    float deltaY = motionEvent.getRawY() - mDownY;
    if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
        mSwiping = true;//from ww w. j a v  a 2 s  . c om
        int mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
        mRecyclerView.requestDisallowInterceptTouchEvent(true);

        // Cancel ListView's touch (un-highlighting the item)
        MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
        cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
        mRecyclerView.onTouchEvent(cancelEvent);
        cancelEvent.recycle();
        if (mSwiping) {
            setTranslationX(mDownView, deltaX - mSwipingSlop);
            setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
        }
    }
}

From source file:com.aako.zjp2p.widget.superrecycler.swipe.SwipeDismissRecyclerViewTouchListener.java

@SuppressLint("AndroidLintClickableViewAccessibility")
@Override//from   w  ww. j  a  va  2s  .c o m
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            return false;
        }

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mRecyclerView.getChildCount();
        int[] listViewCoords = new int[2];
        mRecyclerView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = mRecyclerView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                mDownView = child;
                break;
            }
        }

        if (mDownView != null) {
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mRecyclerView.getChildPosition(mDownView);
            if (mCallbacks.canDismiss(mDownPosition)) {
                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(motionEvent);
            } else {
                mDownView = null;
            }
        }
        return false;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mVelocityTracker == null) {
            break;
        }

        if (mDownView != null && mSwiping) {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss && mDownPosition != INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            performDismiss(downView, downPosition);
                        }
                    });
        } else {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null || mPaused) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
            mSwiping = true;
            mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
            mRecyclerView.requestDisallowInterceptTouchEvent(true);

            // Cancel ListView's touch (un-highlighting the item)
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            mRecyclerView.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }

        if (mSwiping) {
            setTranslationX(mDownView, deltaX - mSwipingSlop);
            setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:com.adamin.superrecyclerview.superrecycer.swipe.SwipeDismissRecyclerViewTouchListener.java

@SuppressLint("AndroidLintClickableViewAccessibility")
@Override/*from   www.jav a2s .  com*/
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            return false;
        }

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mRecyclerView.getChildCount();
        int[] listViewCoords = new int[2];
        mRecyclerView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = mRecyclerView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                mDownView = child;
                break;
            }
        }

        if (mDownView != null) {
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mRecyclerView.getChildPosition(mDownView);
            if (mCallbacks.canDismiss(mDownPosition)) {
                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(motionEvent);
            } else {
                mDownView = null;
            }
        }
        return false;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mVelocityTracker == null) {
            break;
        }

        if (mDownView != null && mSwiping) {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss && mDownPosition != INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime)
                    .setListener(new com.nineoldandroids.animation.AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(com.nineoldandroids.animation.Animator animation) {
                            super.onAnimationEnd(animation);
                            performDismiss(downView, downPosition);
                        }
                    });
        } else {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null || mPaused) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
            mSwiping = true;
            mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
            mRecyclerView.requestDisallowInterceptTouchEvent(true);

            // Cancel ListView's touch (un-highlighting the item)
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            mRecyclerView.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }

        if (mSwiping) {
            setTranslationX(mDownView, deltaX - mSwipingSlop);
            setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:cn.meiqu.baseproject.view.superrecyclerview.swipe.SwipeDismissRecyclerViewTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }//from   w  w  w . ja  va 2 s  .  co  m

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            return false;
        }

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mRecyclerView.getChildCount();
        int[] listViewCoords = new int[2];
        mRecyclerView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = mRecyclerView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                mDownView = child;
                break;
            }
        }

        if (mDownView != null) {
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mRecyclerView.getChildPosition(mDownView);
            if (mCallbacks.canDismiss(mDownPosition)) {
                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(motionEvent);
            } else {
                mDownView = null;
            }
        }
        return false;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mVelocityTracker == null) {
            break;
        }

        if (mDownView != null && mSwiping) {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss && mDownPosition != INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            performDismiss(downView, downPosition);
                        }
                    });
        } else {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null || mPaused) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
            mSwiping = true;
            mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
            mRecyclerView.requestDisallowInterceptTouchEvent(true);

            // Cancel ListView's touch (un-highlighting the item)
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            mRecyclerView.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }

        if (mSwiping) {
            setTranslationX(mDownView, deltaX - mSwipingSlop);
            setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}