Example usage for android.view MotionEvent setAction

List of usage examples for android.view MotionEvent setAction

Introduction

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

Prototype

public final void setAction(int action) 

Source Link

Document

Sets this event's action.

Usage

From source file:com.itude.mobile.mobbl.blueprint.app.view.listeners.SwipeDismissRecyclerViewTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

    if (mViewWidth < 2) {
        mViewWidth = mIsVertical ? mRecyclerView.getHeight() : mRecyclerView.getWidth();
    }//from   w ww .j  a  v a 2s.c  o m

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

        // TODO: ensure this is a finger, and set a flag

        // 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;

        mDownView = mRecyclerView.findChildViewUnder(x, y);

        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
            if (mIsVertical) {
                mDownView.animate().translationY(0).alpha(1).setDuration(mAnimationTime).setListener(null);
            } else {
                mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
            }
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = RecyclerView.NO_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (!mSwiping && mDownView != null && mItemTouchCallback != null) {

            mItemTouchCallback.onTouch(mRecyclerView.getChildPosition(mDownView));
            mVelocityTracker.recycle();
            mVelocityTracker = null;
            mDownX = 0;
            mDownY = 0;
            mDownView = null;
            mDownPosition = ListView.INVALID_POSITION;
            mSwiping = false;
            return true;
        }

        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float velocityY = mVelocityTracker.getYVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;

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

            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 != ListView.INVALID_POSITION) {
                // dismiss
                final View downView = mDownView; // mDownView gets null'd before animation ends
                final int downPosition = mDownPosition;
                ++mDismissAnimationRefCount;
                mDownView.animate().translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                        .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                performDismiss(downView, downPosition);
                            }
                        });
            } else {
                // cancel
                mDownView.animate().translationX(0).alpha(1).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 (mIsVertical) {
            if (Math.abs(deltaY) > mSlop && Math.abs(deltaX) < Math.abs(deltaY) / 2) {
                mSwiping = true;
                mSwipingSlop = (deltaY > 0 ? mSlop : -mSlop);
                mRecyclerView.requestDisallowInterceptTouchEvent(true);

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

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

        } else {
            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
                        | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                mRecyclerView.onTouchEvent(cancelEvent);
                cancelEvent.recycle();
            }

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

From source file:com.taobao.weex.ui.view.WXScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!scrollable) {
        return true; // when scrollable is set to false, then eat the touch event
    }//from www .  jav a 2s .  co m
    if (mRedirectTouchToStickyView) {

        if (mScrollRect == null) {
            mScrollRect = new Rect();
            getGlobalVisibleRect(mScrollRect);
        }
        mCurrentStickyView.getLocationOnScreen(stickyViewP);
        ev.offsetLocation(0, -(stickyViewP[1] - mScrollRect.top));
    }

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        mHasNotDoneActionDown = false;
    }

    if (mHasNotDoneActionDown) {
        MotionEvent down = MotionEvent.obtain(ev);
        down.setAction(MotionEvent.ACTION_DOWN);
        mHasNotDoneActionDown = false;
        down.recycle();
    }

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        ox = ev.getX();
        oy = ev.getY();
        // Dispatch touch event to parent view
        startNestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL | ViewCompat.SCROLL_AXIS_VERTICAL);
    }

    if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
        mHasNotDoneActionDown = true;
        // stop nested scrolling dispatch
        stopNestedScroll();
    }

    if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        float clampedX = ev.getX();
        float clampedY = ev.getY();
        int dx = (int) (ox - clampedX);
        int dy = (int) (oy - clampedY);

        if (dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)) {
            // sub dx/dy was consumed by parent view!!!
            ev.setLocation(clampedX + consumed[0], clampedY + consumed[1]);
        }
        ox = ev.getX();
        oy = ev.getY();
    }

    boolean result = super.onTouchEvent(ev);
    if (wxGesture != null) {
        result |= wxGesture.onTouch(this, ev);
    }
    return result;
}

From source file:com.iutiao.sdk.views.PasswordEditText.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (mDrawableSide == null) {
        return super.onTouchEvent(event);
    }//from www.  j  a  v a 2s. co m
    final Rect bounds = mDrawableSide.getBounds();
    final int x = (int) event.getRawX(); // ?

    int iconX = (int) getTopRightCorner().x;

    // Icon?
    int leftIcon = iconX - bounds.width();

    Log.e(TAG, "x: " + x + ", leftIcon: " + leftIcon);

    // Icon?, ??
    if (x >= leftIcon) {
        togglePasswordIconVisibility(); // ???
        event.setAction(MotionEvent.ACTION_CANCEL);
        return false;
    }
    return super.onTouchEvent(event);
}

From source file:ru.mahortov.listviewcustom.ListView.PinnedSectionListView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null)
        return super.onTouchEvent(ev);
    int action = MotionEventCompat.getActionMasked(ev);
    action = ev.getAction();/*from  w  w  w.  j  ava 2  s . c  om*/
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        int oldPos = mTouchPosition;
        mDownX = ev.getX();
        mDownY = ev.getY();
        mTouchState = TOUCH_STATE_NONE;

        mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());

        if (mTouchPosition == oldPos && mTouchView != null && mTouchView.isOpen()) {
            mTouchState = TOUCH_STATE_X;
            mTouchView.onSwipe(ev);
            return true;
        }

        View view = getChildAt(mTouchPosition - getFirstVisiblePosition());

        if (mTouchView != null && mTouchView.isOpen()) {
            mTouchView.smoothCloseMenu();
            mTouchView = null;
            // return super.onTouchEvent(ev);
            // try to cancel the touch event
            MotionEvent cancelEvent = MotionEvent.obtain(ev);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
            onTouchEvent(cancelEvent);
            return true;
        }
        if (view instanceof SwipeMenuLayout) {
            mTouchView = (SwipeMenuLayout) view;
            mTouchView.setSwipeDirection(mDirection);
        }
        if (mTouchView != null) {
            mTouchView.onSwipe(ev);
        }
        break;
    case MotionEvent.ACTION_MOVE:
        float dy = Math.abs((ev.getY() - mDownY));
        float dx = Math.abs((ev.getX() - mDownX));
        if (mTouchState == TOUCH_STATE_X) {
            if (mTouchView != null) {
                mTouchView.onSwipe(ev);
            }
            getSelector().setState(new int[] { 0 });
            ev.setAction(MotionEvent.ACTION_CANCEL);
            super.onTouchEvent(ev);
            return true;
        } else if (mTouchState == TOUCH_STATE_NONE) {
            if (Math.abs(dy) > MAX_Y) {
                mTouchState = TOUCH_STATE_Y;
            } else if (dx > MAX_X) {
                mTouchState = TOUCH_STATE_X;
                if (mOnSwipeListener != null) {
                    mOnSwipeListener.onSwipeStart(mTouchPosition);
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_X) {
            if (mTouchView != null) {
                mTouchView.onSwipe(ev);
                if (!mTouchView.isOpen()) {
                    mTouchPosition = -1;
                    mTouchView = null;
                }
            }
            if (mOnSwipeListener != null) {
                mOnSwipeListener.onSwipeEnd(mTouchPosition);
            }
            ev.setAction(MotionEvent.ACTION_CANCEL);
            super.onTouchEvent(ev);
            return true;
        }
        break;
    }
    return super.onTouchEvent(ev);
}

From source file:com.gome.haoyuangong.views.SwipeRefreshLayout.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

    ensureTarget();/*  ww w.j  av  a  2s  .c  om*/
    boolean handled = false;
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        mInterceptY = (int) ev.getY();
    }
    if (isEnabled() && !canChildScroll(this, (int) ev.getY() - mInterceptY, 1)) {
        if (mReturningToStart && ev.getAction() == MotionEvent.ACTION_DOWN) {
            mReturningToStart = false;
            removeAnimateMove();
        }
        handled = onTouchEvent(ev);
    } else {
        mDownEvent = MotionEvent.obtain(ev);
        mDownTargetTop = mCurrentTargetOffsetTop;
    }
    mInterceptY = (int) ev.getY();
    if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        if (mOldHandled == 1 && !handled) {
            MotionEvent event = MotionEvent.obtain(ev);
            event.setAction(MotionEvent.ACTION_DOWN);
            getChildAt(1).dispatchTouchEvent(event);
            disableSubControls(getChildAt(1));
            event.recycle();
        } else if (mOldHandled == 2 && handled) {
            MotionEvent event = MotionEvent.obtain(ev);
            event.setAction(MotionEvent.ACTION_CANCEL);
            getChildAt(1).dispatchTouchEvent(event);
            event.recycle();
        } else if (mOldHandled == 0 && handled) {
            MotionEvent event = MotionEvent.obtain(ev);
            event.setAction(MotionEvent.ACTION_CANCEL);
            getChildAt(1).dispatchTouchEvent(event);
            event.recycle();
        }
        if (handled) {
            mOldHandled = 1;
        } else {
            mOldHandled = 2;
        }
    } else if (ev.getAction() == MotionEvent.ACTION_UP) {
        mOldHandled = 0;
    }
    if (!handled) {
        super.dispatchTouchEvent(ev);
    }
    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  ww.  j ava2s . co 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);
}

From source file:com.davidtpate.swipelistview.SwipeListView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Store width of this list for usage of swipe distance detection
    if (mViewWidth < 2) {
        mViewWidth = getWidth();//w  w w  .jav a  2s. co  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.
                    ViewPropertyAnimator.animate(mViewPair.mFrontView)
                            .translationX(deltaX >= 0 ? mViewWidth : -mViewWidth).alpha(0)
                            .setDuration(mAnimationTime);
                    ViewPropertyAnimator.animate(mViewPair.mBackView).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);
                    ViewPropertyAnimator.animate(mViewPair.mFrontView).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.
                ViewHelper.setAlpha(mViewPair.mBackView, Math.min(1f, 2f * Math.abs(deltaX) / mViewWidth));
                ViewHelper.setTranslationX(mViewPair.mFrontView, deltaX);
                ViewHelper.setAlpha(mViewPair.mFrontView,
                        Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
                return true;
            }
        }
        break;
    }
    return super.onTouchEvent(event);
}

From source file:com.xyczero.customswipelistview.CustomSwipeListView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Just response single finger action.
    final int action = ev.getAction() & MotionEvent.ACTION_MASK;
    final int x = (int) ev.getX();

    if (action == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        ev.setAction(MotionEvent.ACTION_CANCEL);
        return super.onTouchEvent(ev);
    }/*from   w  w w .  j  a v a 2  s  . c o m*/

    if (mSelectedPosition != INVALID_POSITION) {
        addVelocityTrackerMotionEvent(ev);
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            Log.d(TAG, "onTouchEvent:ACTION_DOWN");
            // If there is a itemswipeview and then don't click it
            // by the next down action,it will first return to original
            // state and cancel to response the following actions.
            if (isItemSwipeViewVisible) {
                if (!isClickItemSwipeView) {
                    mLastItemSwipeView.setVisibility(GONE);
                    mLastItemMainView.scrollTo(0, 0);
                }
                isItemSwipeViewVisible = false;
                ev.setAction(MotionEvent.ACTION_CANCEL);
                return super.onTouchEvent(ev);
            }
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d(TAG, "onTouchEvent:ACTION_MOVE");
            mVelocityTracker.getYVelocity();
            // determine whether the swipe action.
            if (Math.abs(getScrollXVelocity()) > mMinimumVelocity
                    || (Math.abs(ev.getX() - mDownMotionX) > mTouchSlop
                            && Math.abs(ev.getY() - mDownMotionY) < mTouchSlop)) {
                isSwiping = true;
            }
            if (isSwiping) {
                int deltaX = (int) mDownMotionX - x;
                if (deltaX > 0 && mEnableSwipeItemLeft || deltaX < 0 && mEnableSwipeItemRight) {
                    mDownMotionX = x;
                    mCurItemMainView.scrollBy(deltaX, 0);
                }
                // if super.onTouchEvent() that been called there,it might
                // lead to the specified item out of focus due to the
                // function might call itemClick function in the sliding.
                return true;
            }
            break;
        case MotionEvent.ACTION_UP:
            Log.d(TAG, "onTouchEvent:ACTION_UP");
            if (isSwiping) {
                mLastItemMainView = mCurItemMainView;
                mLastItemSwipeView = mCurItemSwipeView;
                final int velocityX = getScrollXVelocity();
                if (velocityX > mMinimumVelocity) {
                    scrollByTouchSwipeMode(TOUCH_SWIPE_RIGHT, -mScreenWidth);
                } else if (velocityX < -mMinimumVelocity) {
                    scrollByTouchSwipeMode(TOUCH_SWIPE_LEFT, getItemSwipeViewWidth(mLastItemSwipeView));
                } else {
                    scrollByTouchSwipeMode(TOUCH_SWIPE_AUTO, Integer.MIN_VALUE);
                }

                recycleVelocityTracker();
                // TODO:To be optimized for not calling computeScroll
                // function.
                if (mScroller.isFinished()) {
                    isSwiping = false;
                }

                // prevent to trigger OnItemClick by transverse sliding
                // distance too slow or too small OnItemClick events when in
                // swipe mode.
                ev.setAction(MotionEvent.ACTION_CANCEL);
                return super.onTouchEvent(ev);
            }
            break;
        default:
            break;
        }
    }
    return super.onTouchEvent(ev);
}

From source file:com.me.harris.androidanimations._06_touch.swipelistview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(View, MotionEvent)
 *//*ww  w  .  jav  a 2 s.co m*/
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (viewWidth < 2) {
        viewWidth = swipeListView.getWidth();
    }

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (paused) {
            return false;
        }
        swipeCurrentAction = SwipeListView.SWIPE_ACTION_NONE;

        int childCount = swipeListView.getChildCount();
        int[] listViewCoords = new int[2];
        swipeListView.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 = swipeListView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                setParentView(child);
                setFrontView(child.findViewById(swipeFrontView));
                downX = motionEvent.getRawX();
                downPosition = swipeListView.getPositionForView(child);

                frontView.setClickable(!opened.get(downPosition));
                frontView.setLongClickable(!opened.get(downPosition));

                velocityTracker = VelocityTracker.obtain();
                velocityTracker.addMovement(motionEvent);
                if (swipeBackView > 0) {
                    setBackView(child.findViewById(swipeBackView));
                }
                break;
            }
        }
        view.onTouchEvent(motionEvent);
        return true;
    }

    case MotionEvent.ACTION_UP: {
        if (velocityTracker == null || !swiping) {
            break;
        }

        float deltaX = motionEvent.getRawX() - downX;
        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        if (!opened.get(downPosition)) {
            if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && velocityTracker.getXVelocity() > 0) {
                velocityX = 0;
            }
            if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && velocityTracker.getXVelocity() < 0) {
                velocityX = 0;
            }
        }
        float velocityY = Math.abs(velocityTracker.getYVelocity());
        boolean swap = false;
        boolean swapRight = false;
        if (minFlingVelocity <= velocityX && velocityX <= maxFlingVelocity && velocityY < velocityX) {
            swapRight = velocityTracker.getXVelocity() > 0;
            if (opened.get(downPosition) && openedRight.get(downPosition) && swapRight) {
                swap = false;
            } else if (opened.get(downPosition) && !openedRight.get(downPosition) && !swapRight) {
                swap = false;
            } else {
                swap = true;
            }
        } else if (Math.abs(deltaX) > viewWidth / 2) {
            swap = true;
            swapRight = deltaX > 0;
        }
        generateAnimate(frontView, swap, swapRight, downPosition);

        velocityTracker.recycle();
        velocityTracker = null;
        downX = 0;
        // change clickable front view
        if (swap) {
            frontView.setClickable(opened.get(downPosition));
            frontView.setLongClickable(opened.get(downPosition));
        }
        frontView = null;
        backView = null;
        this.downPosition = ListView.INVALID_POSITION;
        swiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (velocityTracker == null || paused) {
            break;
        }

        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        float velocityY = Math.abs(velocityTracker.getYVelocity());

        float deltaX = motionEvent.getRawX() - downX;
        float deltaMode = Math.abs(deltaX);
        if (swipeMode == SwipeListView.SWIPE_MODE_NONE) {
            deltaMode = 0;
        } else if (swipeMode != SwipeListView.SWIPE_MODE_BOTH) {
            if (opened.get(downPosition)) {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX < 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX > 0) {
                    deltaMode = 0;
                }
            } else {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX > 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX < 0) {
                    deltaMode = 0;
                }
            }
        }
        if (deltaMode > slop && swipeCurrentAction == SwipeListView.SWIPE_ACTION_NONE
                && velocityY < velocityX) {
            swiping = true;
            boolean swipingRight = (deltaX > 0);
            if (opened.get(downPosition)) {
                swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
            } else {
                if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_DISMISS) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_DISMISS) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_CHECK) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHECK;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_CHECK) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHECK;
                } else {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
                }
            }
            swipeListView.requestDisallowInterceptTouchEvent(true);
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                    | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
            swipeListView.onTouchEvent(cancelEvent);
        }

        if (swiping) {
            if (opened.get(downPosition)) {
                deltaX += openedRight.get(downPosition) ? viewWidth - rightOffset : -viewWidth + leftOffset;
            }
            move(deltaX);
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:com.fortysevendeg.android.swipelistview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
 *//*from  ww w .  ja  v a 2  s .c o m*/
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (!isSwipeEnabled()) {
        return false;
    }

    mViewWidth = mSwipeListView.getWidth();

    switch (motionEvent.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        return actionDown(view, motionEvent);
    }

    case MotionEvent.ACTION_UP: {
        actionUp(motionEvent);
    }

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

        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(mVelocityTracker.getXVelocity());
        float velocityY = Math.abs(mVelocityTracker.getYVelocity());

        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaMode = Math.abs(deltaX);

        int swipeMode = mSwipeMode;
        int changeSwipeMode = mSwipeListView.changeSwipeMode(mDownPosition);
        if (changeSwipeMode >= 0) {
            swipeMode = changeSwipeMode;
        }

        if (swipeMode == SwipeListView.SWIPE_MODE_NONE) {
            deltaMode = 0;
        } else if (swipeMode != SwipeListView.SWIPE_MODE_BOTH) {
            if (mOpened.get(mDownPosition)) {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX < 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX > 0) {
                    deltaMode = 0;
                }
            } else {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX > 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX < 0) {
                    deltaMode = 0;
                }
            }
        }
        if (deltaMode > mSlop && mSwipeCurrentAction == SwipeListView.SWIPE_ACTION_NONE
                && velocityY < velocityX) {
            mSwiping = true;
            boolean swipingRight = (deltaX > 0);
            if (mOpened.get(mDownPosition)) {
                mSwipeListView.onStartClose(mDownPosition, swipingRight);
                mSwipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
            } else {
                if (swipingRight && mSwipeActionRight == SwipeListView.SWIPE_ACTION_DISMISS) {
                    mSwipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (!swipingRight && mSwipeActionLeft == SwipeListView.SWIPE_ACTION_DISMISS) {
                    mSwipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (swipingRight && mSwipeActionRight == SwipeListView.SWIPE_ACTION_CHECK) {
                    mSwipeCurrentAction = SwipeListView.SWIPE_ACTION_CHECK;
                } else if (!swipingRight && mSwipeActionLeft == SwipeListView.SWIPE_ACTION_CHECK) {
                    mSwipeCurrentAction = SwipeListView.SWIPE_ACTION_CHECK;
                } else {
                    mSwipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
                }
                mSwipeListView.onStartOpen(mDownPosition, mSwipeCurrentAction, swipingRight);
            }
            mSwipeListView.requestDisallowInterceptTouchEvent(true);
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            mSwipeListView.onTouchEvent(cancelEvent);
        }

        if (mSwiping) {
            if (mOpened.get(mDownPosition)) {
                deltaX += mOpenedRight.get(mDownPosition) ? mViewWidth - mRightOffset
                        : -mViewWidth + mLeftOffset;
            }
            move(deltaX);
            return true;
        }
        break;
    }
    }
    return false;
}