Example usage for android.view MotionEvent ACTION_POINTER_INDEX_SHIFT

List of usage examples for android.view MotionEvent ACTION_POINTER_INDEX_SHIFT

Introduction

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

Prototype

int ACTION_POINTER_INDEX_SHIFT

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

Click Source Link

Document

Bit shift for the action bits holding the pointer index as defined by #ACTION_POINTER_INDEX_MASK .

Usage

From source file:com.daycle.daycleapp.custom.swipelistview.itemmanipulation.swipedismiss.SwipeTouchListener.java

private boolean handleMoveEvent(@Nullable final View view, @NonNull final MotionEvent motionEvent) {
    if (mVelocityTracker == null || mCurrentView == null) {
        return false;
    }//from  w ww  . j a  va  2s  .  com

    //  ?? ?? ?  ? ? 
    mVelocityTracker.addMovement(motionEvent);

    //  ? ?
    float deltaX = motionEvent.getX() - mDownX;
    float deltaY = motionEvent.getY() - mDownY;

    boolean dismissToRight = deltaX < 0;
    mflingToRight = dismissToRight;

    // ? ?  ?? ?,  ??  ? ? ??? ?
    if (Math.abs(deltaX) > mSlop && Math.abs(deltaX) > Math.abs(deltaY)) {

        // ? ? ??
        if (!mSwiping) {
            mActiveSwipeCount++;
        }

        // ? ?? 
        mSwiping = true;
        mListViewWrapper.getListView().requestDisallowInterceptTouchEvent(true);

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

    // ????
    if (mSwiping) {

        // ? ?
        if (mCanDismissCurrent) {

            // ? ?? 
            onStartSwipe(mCurrentView, mCurrentPosition, dismissToRight);

            //   ??
            ViewHelper.setTranslationX(mSwipingView, deltaX);

            // ??? ? .
            //ViewHelper.setAlpha(mSwipingView, Math.max(mMinimumAlpha, Math.min(1, 1 - 2 * Math.abs(deltaX) / mViewWidth)));

            if (dismissToRight) {

                mLeftSwipingView.setVisibility(View.GONE);
                mRightSwipingView.setVisibility(View.VISIBLE);
            } else {
                mLeftSwipingView.setVisibility(View.VISIBLE);
                mRightSwipingView.setVisibility(View.GONE);
            }

        } else {

            //  ? ? ?
            ViewHelper.setTranslationX(mSwipingView, deltaX * 0.1f);
        }
        return true;
    }
    return false;
}

From source file:com.leeon.blank.widget.RangeBarNew.java

private void handleTouchDown(MotionEvent event) {
    final int actionIndex = (event.getAction()
            & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    final int downX = (int) event.getX(actionIndex);
    final int downY = (int) event.getY(actionIndex);

    if (mLeftCursorRect.contains(downX, downY)) {
        if (mLeftHit) {
            return;
        }//from ww  w .j  ava  2s  . co  m
        // If hit, change state of drawable, and record id of touch pointer.
        mLeftPointerLastX = downX;
        mLeftCursorBG.setState(mPressedEnableState);
        mLeftPointerID = event.getPointerId(actionIndex);
        mLeftHit = true;
        invalidate();
    } else if (mRightCursorRect.contains(downX, downY)) {
        if (mRightHit) {
            return;
        }
        mRightPointerLastX = downX;
        mRightCursorBG.setState(mPressedEnableState);
        mRightPointerID = event.getPointerId(actionIndex);
        mRightHit = true;
        invalidate();
    }
}

From source file:com.leeon.blank.widget.RangeBarNew.java

private void handleTouchUp(MotionEvent event) {
    final int actionIndex = (event.getAction()
            & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    final int actionID = event.getPointerId(actionIndex);
    if (actionID == mLeftPointerID) {
        if (!mLeftHit) {
            return;
        }//from w  w  w  .ja v a  2s. com
        // If cursor between in tow mark locations, it should be located on the lower or higher one.
        if (getDiffByX(event.getX()) == 1) {
            // step 1:Calculate the offset with lower mark.
            final float offset = mLeftCursorIndex % 5;
            // step 2:Decide which mark will go to.
            if (offset < 2.5f) {
                // If left cursor want to be located on lower mark, go ahead
                // guys.
                // Because right cursor will never appear lower than the
                // left one.
                mLeftCursorNextIndex = (int) (mLeftCursorIndex / 5) * 5;
            } else {
                mLeftCursorNextIndex = (int) (mLeftCursorIndex / 5) * 5 + 1;
                // If left cursor want to be located on higher mark,
                // situation becomes a little complicated.
                // We should check that whether distance between left and
                // right cursor is less than 1, and next index of left
                // cursor is difference with current
                // of right one.
                if (Math.abs(mLeftCursorIndex - mRightCursorIndex) <= 5
                        && mLeftCursorNextIndex == mRightCursorNextIndex) {
                    // Left can not go to the higher, just to the lower one.
                    mLeftCursorNextIndex = (int) (mLeftCursorIndex / 5) * 5;
                }
            }
            // step 3: Move to.
            if (!mLeftScroller.computeScrollOffset()) {
                final int fromX = (int) (mLeftCursorIndex * mTinyPartLength);
                mLeftScroller.startScroll(fromX, 0, (int) (mLeftCursorNextIndex * mTinyPartLength - fromX), 0,
                        mDuration);
            }
            if (mLeftCursorNextIndex != mLeftCursorPreIndex) {
                mLeftCursorPreIndex = mLeftCursorNextIndex;
            }
        }

        // Reset values of parameters
        mLeftPointerLastX = 0;
        mLeftCursorBG.setState(mUnPressedEanabledState);
        mLeftPointerID = -1;
        mLeftHit = false;
        invalidate();
    } else if (actionID == mRightPointerID) {
        if (!mRightHit) {
            return;
        }

        if (getDiffByX(event.getX()) == 1) {
            final float offset = mRightCursorIndex % 5;
            if (offset > 2.5f) {
                mRightCursorNextIndex = (int) (mRightCursorIndex / 5) * 5 + 1;
            } else {
                mRightCursorNextIndex = (int) (mRightCursorIndex / 5) * 5;
                if (Math.abs(mLeftCursorIndex - mRightCursorIndex) <= 5
                        && mRightCursorNextIndex == mLeftCursorNextIndex) {
                    mRightCursorNextIndex = (int) (mRightCursorIndex / 5) * 5 + 1;
                }
            }
            if (!mRightScroller.computeScrollOffset()) {
                final int fromX = (int) (mRightCursorIndex * mTinyPartLength);
                mRightScroller.startScroll(fromX, 0, (int) (mRightCursorNextIndex * mTinyPartLength - fromX), 0,
                        mDuration);
            }
            if (mRightCursorNextIndex != mRightCursorPreIndex) {
                mRightCursorPreIndex = mRightCursorNextIndex;
            }
        }

        mRightPointerLastX = 0;
        mLeftCursorBG.setState(mUnPressedEanabledState);
        mRightPointerID = -1;
        mRightHit = false;
        invalidate();
    }
}

From source file:de.timroes.swipetodismiss.SwipeDismissList.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }//from  w w w.  j  a  va2 s.  c  om

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

        // 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 = mListView.getChildCount();
        int[] listViewCoords = new int[2];
        mListView.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 = mListView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                if (mSwipeLayout != 0) {
                    View swipeView = child.findViewById(mSwipeLayout);
                    if (swipeView != null) {
                        mDownView = swipeView;
                        break;
                    }
                }
                mDownView = child;
                break;
            }
        }

        if (mDownView != null) {
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mListView.getPositionForView(mDownView);

            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        }
        view.onTouchEvent(motionEvent);
        return true;
    }

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

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(mVelocityTracker.getXVelocity());
        float velocityY = 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 <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX
                && mSwiping && isDirectionValid(mVelocityTracker.getXVelocity())
                && deltaX >= mViewWidth * 0.2f) {
            dismiss = true;
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            mPaused = true;
            animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performDismiss(downView, downPosition);
                        }
                    });
        } else {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        resetSwipeState();
        break;
    }

    case MotionEvent.ACTION_MOVE: {

        if (mUndoPopup.isShowing()) {
            // Send a delayed message to hide popup
            mHandler.sendMessageDelayed(mHandler.obtainMessage(mDelayedMsgId), mAutoHideDelay);
        }

        if (mVelocityTracker == null || mPaused || !mEnabled) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        // Only start swipe in correct direction
        if (isDirectionValid(deltaX)) {
            if (Math.abs(deltaX) > mSlop && Math.abs(deltaX) > Math.abs(deltaY)) {
                mSwiping = true;
                mListView.requestDisallowInterceptTouchEvent(true);

                // Cancel ListView's touch (un-highlighting the item)
                MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
                cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                        .getActionIndex(motionEvent) << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                mListView.onTouchEvent(cancelEvent);
                cancelEvent.recycle();
            }
        } else {
            // If we swiped into wrong direction, act like this was the new
            // touch down point
            mDownX = motionEvent.getRawX();
            deltaX = 0;
        }

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

From source file:it.mb.whatshare.Utils.java

/**
 * Returns a string that represents the symbolic name of the specified
 * action such as "ACTION_DOWN", "ACTION_POINTER_DOWN(3)" or an equivalent
 * numeric constant such as "35" if unknown. By Google.
 * //from ww w  . j  av  a  2 s.c  o  m
 * @param action
 *            The action.
 * @return The symbolic name of the specified action.
 */
public static String actionToString(int action) {
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        return "ACTION_DOWN";
    case MotionEvent.ACTION_UP:
        return "ACTION_UP";
    case MotionEvent.ACTION_CANCEL:
        return "ACTION_CANCEL";
    case MotionEvent.ACTION_OUTSIDE:
        return "ACTION_OUTSIDE";
    case MotionEvent.ACTION_MOVE:
        return "ACTION_MOVE";
    case MotionEvent.ACTION_HOVER_MOVE:
        return "ACTION_HOVER_MOVE";
    case MotionEvent.ACTION_SCROLL:
        return "ACTION_SCROLL";
    case MotionEvent.ACTION_HOVER_ENTER:
        return "ACTION_HOVER_ENTER";
    case MotionEvent.ACTION_HOVER_EXIT:
        return "ACTION_HOVER_EXIT";
    }
    int index = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_POINTER_DOWN:
        return "ACTION_POINTER_DOWN(" + index + ")";
    case MotionEvent.ACTION_POINTER_UP:
        return "ACTION_POINTER_UP(" + index + ")";
    default:
        return Integer.toString(action);
    }
}

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

/**
 * @see View.OnTouchListener#onTouch(View, MotionEvent)
 *///w w w  . j av  a  2s .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.bulletnoid.android.widget.StaggeredGridView.StaggeredGridView2.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = (ev.getAction()
            & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    final int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        // TODO: Make this decision more intelligent.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastTouchX = (int) ev.getX(newPointerIndex);
        mLastTouchY = (int) ev.getY(newPointerIndex);
        mMotionCorrection = 0;/*w  w w  .ja v a 2s  . c  o  m*/
        mActivePointerId = ev.getPointerId(newPointerIndex);
    }
}

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();/* ww  w  . j a  v  a2  s.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.  ja  v  a 2  s  . 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.
                    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:net.osmand.plus.views.controls.SwipeDismissListViewTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent ev) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }//w w w.ja  v  a 2s .  c om

    switch (ev.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (mSwipePaused) {
            return false;
        }

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mListView.getChildCount();
        int[] listViewCoords = new int[2];
        mListView.getLocationOnScreen(listViewCoords);
        int x = (int) ev.getRawX() - listViewCoords[0];
        int y = (int) ev.getRawY() - listViewCoords[1];
        View child;
        for (int i = mListView.getHeaderViewsCount(); i < childCount; i++) {
            child = mListView.getChildAt(i);
            if (child != null) {
                child.getHitRect(rect);
                if (rect.contains(x, y)) {
                    // if a specific swiping layout has been giving, use this to swipe.
                    if (mSwipingLayout > 0) {
                        View swipingView = child.findViewById(mSwipingLayout);
                        if (swipingView != null) {
                            mSwipeDownView = swipingView;
                            mSwipeDownChild = child;
                            break;
                        }
                    }
                    // If no swiping layout has been found, swipe the whole child
                    mSwipeDownView = mSwipeDownChild = child;
                    break;
                }
            }
        }

        if (mSwipeDownView != null) {
            // test if the item should be swiped
            int position = mListView.getPositionForView(mSwipeDownView) - mListView.getHeaderViewsCount();
            if (mCallbacks == null || mCallbacks.canDismiss(position)) {
                mDownX = ev.getRawX();
                mDownY = ev.getRawY();
                mDownPosition = position;

                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(ev);
            } else {
                // set back to null to revert swiping
                mSwipeDownView = mSwipeDownChild = null;
            }
        }
        return false;
    }

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

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

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

        float deltaX = ev.getRawX() - mDownX;
        mVelocityTracker.addMovement(ev);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(mVelocityTracker.getXVelocity());
        float velocityY = 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 <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX
                && mSwiping && isSwipeDirectionValid(mVelocityTracker.getXVelocity())
                && deltaX >= mViewWidth * 0.2f) {
            dismiss = true;
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss) {
            // dismiss
            slideOutView(mSwipeDownView, mSwipeDownChild, mDownPosition, dismissRight);
        } else if (mSwiping) {
            // Swipe back to regular position
            ViewCompat.animate(mSwipeDownView).translationX(0).alpha(1).setDuration(mAnimationTime)
                    .setListener(null);
        }
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mSwipeDownView = null;
        mSwipeDownChild = null;
        mDownPosition = AbsListView.INVALID_POSITION;
        mSwiping = false;
        break;
    }

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

        mVelocityTracker.addMovement(ev);
        float deltaX = ev.getRawX() - mDownX;
        float deltaY = ev.getRawY() - mDownY;
        // Only start swipe in correct direction
        if (isSwipeDirectionValid(deltaX)) {
            ViewParent parent = mListView.getParent();
            if (parent != null) {
                // If we swipe don't allow parent to intercept touch (e.g. like NavigationDrawer does)
                // otherwise swipe would not be working.
                parent.requestDisallowInterceptTouchEvent(true);
            }
            if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
                mSwiping = true;
                mListView.requestDisallowInterceptTouchEvent(true);

                // Cancel ListView's touch (un-highlighting the item)
                MotionEvent cancelEvent = MotionEvent.obtain(ev);
                cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                        | (ev.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                mListView.onTouchEvent(cancelEvent);
            }
        } else {
            // If we swiped into wrong direction, act like this was the new
            // touch down point
            mDownX = ev.getRawX();
            deltaX = 0;
        }

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