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:de.stadtrallye.rallyesoft.widget.GalleryPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (touchEnabled) {
        return super.onInterceptTouchEvent(ev);
    } else {/*from   w w  w  .  j a v a2s .  co m*/
        if (!cancelled) {
            MotionEvent e = MotionEvent.obtain(ev);
            e.setAction(MotionEvent.ACTION_CANCEL);
            super.onInterceptTouchEvent(e);
            cancelled = true;
            e.recycle();
        }
        return false;
    }
}

From source file:mobi.cangol.mobile.navigation.PagerEnabledSlidingPaneLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    switch (MotionEventCompat.getActionMasked(ev)) {
    case MotionEvent.ACTION_DOWN: {
        mInitialMotionX = ev.getX();/*from   ww w. j ava2  s .c o  m*/
        mInitialMotionY = ev.getY();
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final float x = ev.getX();
        final float y = ev.getY();
        // The user should always be able to "close" the pane, so we only check
        // for child scrollability if the pane is currently closed.
        if (mInitialMotionX > mEdgeSlop && !isOpen()
                && canScroll(this, false, Math.round(x - mInitialMotionX), Math.round(x), Math.round(y))) {

            // How do we set super.mIsUnableToDrag = true?

            // send the parent a cancel event
            MotionEvent cancelEvent = MotionEvent.obtain(ev);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
            return super.onInterceptTouchEvent(cancelEvent);
        }
    }
    }

    return super.onInterceptTouchEvent(ev);
}

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

/**
 * @see View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
 *///from ww  w  . java2 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);
        if (mRefreshSwipeListener != null && mSwipeListView.isListAtTop()) {
            mRefreshSwipeListener.setScroll(false);
        }
        break;
    }

    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;
            if (mRefreshSwipeListener != null) {
                mRefreshSwipeListener.setScroll(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;
}

From source file:com.android.hareime.accessibility.AccessibleKeyboardViewProxy.java

/**
 * Simulates a transition between two {@link Key}s by sending a HOVER_EXIT
 * on the previous key, a HOVER_ENTER on the current key, and a HOVER_MOVE
 * on the current key./*  ww w  . j av a 2s  . co  m*/
 *
 * @param currentKey The currently hovered key.
 * @param previousKey The previously hovered key.
 * @param event The event that triggered the transition.
 * @return {@code true} if the event was handled.
 */
private boolean onTransitionKey(Key currentKey, Key previousKey, MotionEvent event) {
    final int savedAction = event.getAction();

    event.setAction(MotionEvent.ACTION_HOVER_EXIT);
    onHoverKey(previousKey, event);

    event.setAction(MotionEvent.ACTION_HOVER_ENTER);
    onHoverKey(currentKey, event);

    event.setAction(MotionEvent.ACTION_HOVER_MOVE);
    final boolean handled = onHoverKey(currentKey, event);

    event.setAction(savedAction);

    return handled;
}

From source file:com.personal.taskmanager2.utilities.RecyclerViewTouchListener.java

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_CANCEL:
        Log.d(TAG, "Cancel Event");
        break;//  ww w .j a  v a  2 s .  c om
    case MotionEvent.ACTION_DOWN:
        Log.d(TAG, "Press Event");
        break;
    case MotionEvent.ACTION_UP:
        Log.d(TAG, "Up Event");

        if (mVelocityTracker == null) {
            Log.d(TAG, "velocity tracker is null in action up");
            break;
        }

        Log.d(TAG, "Up Intercept");
        float deltaX = e.getRawX() - mDownX;
        float absDeltaX = Math.abs(deltaX);
        mVelocityTracker.addMovement(e);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (absDeltaX > mViewWidth / 2) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }

        if (dismiss) {
            dismiss(mChildView, mChildPosition, dismissRight);
        } else {
            mChildView.animate().alpha(1).translationX(0).setDuration(mAnimationTime).setListener(null);
        }

        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDeltaX = 0;
        mChildView = null;
        mChildPosition = RecyclerView.NO_POSITION;
        mSwiping = false;
        break;
    case MotionEvent.ACTION_MOVE:
        Log.d(TAG, "Move Event");
        mRecyclerView.requestDisallowInterceptTouchEvent(true);
        mRefreshLayout.requestDisallowInterceptTouchEvent(true);

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

        mChildView.setTranslationX(mDeltaX);
        /*mChildView.setAlpha(Math.max(0.15f, Math.min(1f,
                                                     1f - 2f * Math.abs(mDeltaX) /
                                                          mViewWidth)));*/
        break;
    }
}

From source file:com.kerkr.edu.recycleView.SwipeToDismissTouchListener.java

private boolean move(MotionEvent motionEvent) {
    if (mSwipeView == null || mVelocityTracker == null || mPaused) {
        return false;
    }/*from   w  ww  .j  a  v  a 2  s  .  c  o  m*/

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

        MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
        cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
        mSwipeView.onTouchEvent(cancelEvent);
    }

    //Prevent swipes to disallowed directions
    if ((deltaX < 0 && mAllowedSwipeDirection == SwipeDirection.RIGHT)
            || (deltaX > 0 && mAllowedSwipeDirection == SwipeDirection.LEFT)) {
        resetMotion();
        return false;
    }

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

From source file:com.github.shareme.gwsswwipetodismiss.library.SwipeDismissTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    // offset because the view is translated during swipe
    motionEvent.offsetLocation(mTranslationX, 0);

    if (mViewWidth < 2) {
        mViewWidth = mView.getWidth();/* w  ww  . ja  v a  2s . c  o  m*/
    }

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        // TODO: ensure this is a finger, and set a flag
        mDownX = motionEvent.getRawX();
        mDownY = motionEvent.getRawY();
        mVelocityTracker = VelocityTracker.obtain();
        mVelocityTracker.addMovement(motionEvent);
        view.onTouchEvent(motionEvent);
        return false;
    }

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

        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        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(deltaY) < Math.abs(deltaX)) {
            if (Math.abs(deltaX) > Math.round(mViewWidth * SWIPE_SENSITIVITY)) {
                dismiss = true;
                dismissRight = deltaX > 0;
            } else if (Math.abs(deltaX) > mSlop && mMinFlingVelocity <= velocityX
                    && velocityX <= mMaxFlingVelocity && velocityY < velocityX) {
                dismiss = true;
                dismissRight = mVelocityTracker.getXVelocity() > 0;
            }
        }
        if (dismiss) {
            // dismiss
            mView.animate().translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performDismiss();
                        }
                    });
        } else {
            // cancel
            mView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker = null;
        mTranslationX = 0;
        mDownX = 0;
        mDownY = 0;
        mSwiping = false;
        break;
    }

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

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        if (Math.abs(deltaX) > mSlop) {
            mSwiping = true;
            mView.getParent().requestDisallowInterceptTouchEvent(true);

            // Cancel listview's touch
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            mView.onTouchEvent(cancelEvent);
        }

        if (mSwiping) {
            mTranslationX = deltaX;
            setTranslationX(mView, deltaX);
            // TODO: use an ease-out interpolator or such
            setAlpha(mView, Math.max(0f, Math.min(1f, 1f - Math.abs(deltaX) / SWIPE_SENSITIVITY * mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}

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

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

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

From source file:com.malinskiy.superrecyclerview.swipe.SwipeDismissRecyclerViewTouchListener.java

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

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

From source file:com.thelastcrusade.soundstream.components.PlaylistFragment.java

@Override
public void onResume() {
    super.onResume();
    getActivity().setTitle(getTitle());//from   w  w w  .ja v a 2s  .c  om

    final GestureDetectorCompat songGesture = new GestureDetectorCompat(getActivity(),
            new PlaylistSongGestureListener(getListView()));

    getListView().setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (songGesture.onTouchEvent(event)) {
                if (event.getAction() != MotionEvent.ACTION_DOWN) {
                    MotionEvent cancelEvent = MotionEvent.obtain(event);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
                    v.onTouchEvent(cancelEvent);
                }
                return true;
            }
            return false;
        }
    });
}