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.xpread.swipelistview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(android.view.View,
 *      android.view.MotionEvent)//from   w  w w . j  av a 2 s .  co m
 */
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

    if (!isSwipeEnabled()) {
        return false;
    }

    if (viewWidth < 2) {
        viewWidth = swipeListView.getWidth();
    }

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        if (paused && downPosition != ListView.INVALID_POSITION) {
            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);

            int childPosition = swipeListView.getPositionForView(child);

            // dont allow swiping if this is on the header or footer or
            // IGNORE_ITEM_VIEW_TYPE or enabled is false on the adapter
            boolean allowSwipe = swipeListView.getAdapter().isEnabled(childPosition)
                    && swipeListView.getAdapter().getItemViewType(childPosition) >= 0;

            if (allowSwipe && rect.contains(x, y)) {
                setParentView(child);

                setFrontView(child.findViewById(swipeFrontView), childPosition);
                boolean result = downPosition == SwipeListView.INVALID_POSITION;

                downPosition = childPosition;

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

                changeSwipeMode = swipeListView.changeSwipeMode(downPosition);

                if (changeSwipeMode == SwipeListView.SWIPE_MODE_LEFT) {
                    right2Open.set(downPosition, false);
                } else if (changeSwipeMode == SwipeListView.SWIPE_MODE_RIGHT) {
                    right2Open.set(downPosition, true);
                }

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

    case MotionEvent.ACTION_UP: {
        if (velocityTracker == null || !swiping || downPosition == ListView.INVALID_POSITION) {
            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 * 2 < velocityX) {
            swapRight = velocityTracker.getXVelocity() > 0;
            if (SwipeListView.DEBUG) {
                Log.d(SwipeListView.TAG, "swapRight: " + swapRight + " - swipingRight: " + swipingRight);
            }
            if (swapRight != swipingRight && swipeActionLeft != swipeActionRight) {
                swap = false;
            } else if (opened.get(downPosition) && openedRight.get(downPosition) && swapRight) {
                swap = false;
            } else if (opened.get(downPosition) && !openedRight.get(downPosition) && !swapRight) {
                swap = false;
            } else {
                int swipeMode = swipeListView.changeSwipeMode(downPosition);
                if (!opened.get(downPosition) && swipeMode == SwipeListView.SWIPE_MODE_RIGHT && !swapRight) {
                    swap = false;
                } else if (!opened.get(downPosition) && swipeMode == SwipeListView.SWIPE_MODE_LEFT
                        && swapRight) {
                    swap = false;
                } else {
                    swap = true;
                }

            }

        } else if (Math.abs(deltaX) > viewWidth / 2) {
            swap = true;
            swapRight = deltaX > 0;
        }

        initPos = true;
        generateAnimate(frontView, swap, swapRight, downPosition);
        if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
            swapChoiceState(downPosition);
        }

        velocityTracker.recycle();
        velocityTracker = null;
        downX = 0;

        swiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (velocityTracker == null || paused || downPosition == ListView.INVALID_POSITION) {
            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);

        int swipeMode = this.swipeMode;

        if (changeSwipeMode >= 0) {
            swipeMode = changeSwipeMode;
        }

        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;
            swipingRight = (deltaX > 0);
            if (SwipeListView.DEBUG) {
                Log.d(SwipeListView.TAG, "deltaX: " + deltaX + " - swipingRight: " + swipingRight);
            }
            if (opened.get(downPosition)) {
                swipeListView.onStartClose(downPosition, swipingRight);
                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_CHOICE) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_CHOICE) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
                } else {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
                }
                swipeListView.onStartOpen(downPosition, swipeCurrentAction, swipingRight);
            }
            swipeListView.requestDisallowInterceptTouchEvent(true);
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            swipeListView.onTouchEvent(cancelEvent);
            if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
                backView.setVisibility(View.GONE);
            }
        }

        if (swiping && downPosition != ListView.INVALID_POSITION) {
            if (opened.get(downPosition)) {
                deltaX += openedRight.get(downPosition) ? viewWidth - rightOffset : -viewWidth + leftOffset;
            }
            move(deltaX);
        }

        return velocityY < velocityX;
    }
    }
    return false;
}

From source file:com.hippo.widget.lockpattern.LockPatternView.java

@Override
public boolean onHoverEvent(@NonNull MotionEvent event) {
    AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isTouchExplorationEnabled()) {
        final int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_HOVER_ENTER:
            event.setAction(MotionEvent.ACTION_DOWN);
            break;
        case MotionEvent.ACTION_HOVER_MOVE:
            event.setAction(MotionEvent.ACTION_MOVE);
            break;
        case MotionEvent.ACTION_HOVER_EXIT:
            event.setAction(MotionEvent.ACTION_UP);
            break;
        }//from   w  ww.  jav  a  2  s .co  m
        onTouchEvent(event);
        event.setAction(action);
    }
    return super.onHoverEvent(event);
}

From source file:com.android.nobug.view.pattern.PatternView.java

@Override
public boolean onHoverEvent(MotionEvent event) {
    if (mAccessibilityManager.isTouchExplorationEnabled()) {
        final int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_HOVER_ENTER:
            event.setAction(MotionEvent.ACTION_DOWN);
            break;
        case MotionEvent.ACTION_HOVER_MOVE:
            event.setAction(MotionEvent.ACTION_MOVE);
            break;
        case MotionEvent.ACTION_HOVER_EXIT:
            event.setAction(MotionEvent.ACTION_UP);
            break;
        }/*from w  w  w .  ja  v a 2s .c  o m*/
        onTouchEvent(event);
        event.setAction(action);
    }
    return super.onHoverEvent(event);
}

From source file:com.marshalchen.ultimaterecyclerview.UltimateRecyclerView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    URLogs.d("ev---" + ev);
    if (mCallbacks != null) {

        switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mIntercepted = false;//from   ww  w.j a va 2  s . co m
            mDragging = false;
            mCallbacks.onUpOrCancelMotionEvent(mObservableScrollState);
            break;
        case MotionEvent.ACTION_MOVE:
            if (mPrevMoveEvent == null) {
                mPrevMoveEvent = ev;
            }
            float diffY = ev.getY() - mPrevMoveEvent.getY();
            mPrevMoveEvent = MotionEvent.obtainNoHistory(ev);
            if (getCurrentScrollY() - diffY <= 0) {
                // Can't scroll anymore.

                if (mIntercepted) {
                    // Already dispatched ACTION_DOWN event to parents, so stop here.
                    return false;
                }

                // Apps can set the interception target other than the direct parent.
                final ViewGroup parent;
                if (mTouchInterceptionViewGroup == null) {
                    parent = (ViewGroup) getParent();
                } else {
                    parent = mTouchInterceptionViewGroup;
                }

                // Get offset to parents. If the parent is not the direct parent,
                // we should aggregate offsets from all of the parents.
                float offsetX = 0;
                float offsetY = 0;
                for (View v = this; v != null && v != parent; v = (View) v.getParent()) {
                    offsetX += v.getLeft() - v.getScrollX();
                    offsetY += v.getTop() - v.getScrollY();
                }
                final MotionEvent event = MotionEvent.obtainNoHistory(ev);
                event.offsetLocation(offsetX, offsetY);

                if (parent.onInterceptTouchEvent(event)) {
                    mIntercepted = true;

                    // If the parent wants to intercept ACTION_MOVE events,
                    // we pass ACTION_DOWN event to the parent
                    // as if these touch events just have began now.
                    event.setAction(MotionEvent.ACTION_DOWN);

                    // Return this onTouchEvent() first and set ACTION_DOWN event for parent
                    // to the queue, to keep events sequence.
                    post(new Runnable() {
                        @Override
                        public void run() {
                            parent.dispatchTouchEvent(event);
                        }
                    });
                    return false;
                }
                // Even when this can't be scrolled anymore,
                // simply returning false here may cause subView's click,
                // so delegate it to super.
                return super.onTouchEvent(ev);
            }
            break;
        }
    }
    return super.onTouchEvent(ev);
}

From source file:com.rexmtorres.android.patternlock.PatternLockView.java

@Override
public boolean onHoverEvent(MotionEvent event) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        if (mAccessibilityManager.isTouchExplorationEnabled()) {
            final int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_HOVER_ENTER:
                event.setAction(MotionEvent.ACTION_DOWN);
                break;
            case MotionEvent.ACTION_HOVER_MOVE:
                event.setAction(MotionEvent.ACTION_MOVE);
                break;
            case MotionEvent.ACTION_HOVER_EXIT:
                event.setAction(MotionEvent.ACTION_UP);
                break;
            }//from   w  ww . ja v a 2  s.c  om
            onTouchEvent(event);
            event.setAction(action);
        }
    }
    return super.onHoverEvent(event);
}

From source file:io.authme.sdk.widget.LockPatternView.java

@Override
public boolean onHoverEvent(MotionEvent event) {
    if (((AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE))
            .isTouchExplorationEnabled()) {
        final int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_HOVER_ENTER:
            event.setAction(MotionEvent.ACTION_DOWN);
            break;
        case MotionEvent.ACTION_HOVER_MOVE:
            event.setAction(MotionEvent.ACTION_MOVE);
            break;
        case MotionEvent.ACTION_HOVER_EXIT:
            event.setAction(MotionEvent.ACTION_UP);
            break;
        }/*from  www  .j  ava2  s.co  m*/
        onTouchEvent(event);
        event.setAction(action);
    }
    return super.onHoverEvent(event);
}

From source file:com.android.incallui.widget.multiwaveview.GlowPadView.java

@Override
public boolean onHoverEvent(MotionEvent event) {
    final AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isTouchExplorationEnabled()) {
        final int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_HOVER_ENTER:
            event.setAction(MotionEvent.ACTION_DOWN);
            break;
        case MotionEvent.ACTION_HOVER_MOVE:
            event.setAction(MotionEvent.ACTION_MOVE);
            break;
        case MotionEvent.ACTION_HOVER_EXIT:
            event.setAction(MotionEvent.ACTION_UP);
            break;
        }/*from   ww  w  .j  a  va 2 s  .c o m*/
        onTouchEvent(event);
        event.setAction(action);
    }
    super.onHoverEvent(event);
    return true;
}

From source file:com.king.view.superslidingpanelayout.SuperSlidingPaneLayout.java

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

    // Preserve the open state based on the last view that was touched.
    if (!mCanSlide && action == MotionEvent.ACTION_DOWN && getChildCount() > 1) {
        // After the first things will be slideable.
        final View secondChild = getChildAt(1);
        if (secondChild != null) {
            mPreservedOpenState = !mDragHelper.isViewUnder(secondChild, (int) ev.getX(), (int) ev.getY());
        }/*from  ww w  .j a  va  2  s .  c  om*/
    }

    if (!mCanSlide || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();
        return super.onInterceptTouchEvent(ev);
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mDragHelper.cancel();
        return false;
    }

    boolean interceptTap = false;

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mIsUnableToDrag = false;
        final float x = ev.getX();
        final float y = ev.getY();
        mInitialMotionX = x;
        mInitialMotionY = y;

        if (mDragHelper.isViewUnder(mSlideableView, (int) x, (int) y) && isDimmed(mSlideableView)) {
            interceptTap = true;
        }
        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 (mIsCompatSliding && 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);
        }

        final float adx = Math.abs(x - mInitialMotionX);
        final float ady = Math.abs(y - mInitialMotionY);
        final int slop = mDragHelper.getTouchSlop();
        if (adx > slop && ady > adx) {
            mDragHelper.cancel();
            mIsUnableToDrag = true;
            return false;
        }
    }
    }

    final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev);

    return interceptForDrag || interceptTap;
}

From source file:il.co.globes.android.swipeListView.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(android.view.View,
 *      android.view.MotionEvent)/*from   w w w .j  a  v a  2  s . c  o m*/
 */
// @Override
// public boolean onTouch(View view, MotionEvent motionEvent)
// {
// if (!isSwipeEnabled())
// {
// return false;
// }
//
// if (viewWidth < 2)
// {
// viewWidth = swipeListView.getWidth();
// }
//
// switch (MotionEventCompat.getActionMasked(motionEvent))
// {
// case MotionEvent.ACTION_DOWN :
// {
// if (paused && downPosition != ListView.INVALID_POSITION)
// {
// 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);
//
// int childPosition = swipeListView.getPositionForView(child);
//
// // dont allow swiping if this is on the header or footer or
// // IGNORE_ITEM_VIEW_TYPE or enabled is false on the adapter
// boolean allowSwipe = swipeListView.getAdapter().isEnabled(childPosition)
// && swipeListView.getAdapter().getItemViewType(childPosition) >= 0;
//
// if (allowSwipe && rect.contains(x, y))
// {
// setParentView(child);
// setFrontView(child.findViewById(swipeFrontView));
//
// downX = motionEvent.getRawX();
// downPosition = childPosition;
//
// 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 || downPosition ==
// ListView.INVALID_POSITION)
// {
// 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 * 2 < velocityX)
// {
// swapRight = velocityTracker.getXVelocity() > 0;
// Log.d("SwipeListView", "swapRight: " + swapRight + " - swipingRight: " +
// swipingRight);
// if (swapRight != swipingRight && swipeActionLeft != swipeActionRight)
// {
// swap = false;
// }
// else 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);
// if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE)
// {
// swapChoiceState(downPosition);
// }
//
// velocityTracker.recycle();
// velocityTracker = null;
// downX = 0;
// // change clickable front view
// // if (swap) {
// // frontView.setClickable(opened.get(downPosition));
// // frontView.setLongClickable(opened.get(downPosition));
// // }
// swiping = false;
// break;
// }
//
// case MotionEvent.ACTION_MOVE :
// {
// if (velocityTracker == null || paused || downPosition ==
// ListView.INVALID_POSITION)
// {
// 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);
//
// int swipeMode = this.swipeMode;
// int changeSwipeMode = swipeListView.changeSwipeMode(downPosition);
// if (changeSwipeMode >= 0)
// {
// swipeMode = changeSwipeMode;
// }
//
// 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;
// swipingRight = (deltaX > 0);
// Log.d("SwipeListView", "deltaX: " + deltaX + " - swipingRight: " +
// swipingRight);
// if (opened.get(downPosition))
// {
// swipeListView.onStartClose(downPosition, swipingRight);
// 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_CHOICE)
// {
// swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
// }
// else if (!swipingRight && swipeActionLeft ==
// SwipeListView.SWIPE_ACTION_CHOICE)
// {
// swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
// }
// else
// {
// swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
// }
// swipeListView.onStartOpen(downPosition, swipeCurrentAction,
// swipingRight);
// }
// swipeListView.requestDisallowInterceptTouchEvent(true);
// MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
// cancelEvent.setAction(MotionEvent.ACTION_CANCEL
// | (MotionEventCompat.getActionIndex(motionEvent) <<
// MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
// swipeListView.onTouchEvent(cancelEvent);
// if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE)
// {
// backView.setVisibility(View.GONE);
// }
// }
//
// if (swiping && downPosition != ListView.INVALID_POSITION)
// {
// if (opened.get(downPosition))
// {
// deltaX += openedRight.get(downPosition) ? viewWidth - rightOffset :
// -viewWidth + leftOffset;
// }
// move(deltaX);
// return true;
// }
// break;
// }
// }
// return false;
// }
//

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (!isSwipeEnabled()) {
        return false;
    }

    if (viewWidth < 2) {
        viewWidth = swipeListView.getWidth();
    }

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        if (paused && downPosition != ListView.INVALID_POSITION) {
            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);

            int childPosition = swipeListView.getPositionForView(child);

            // dont allow swiping if this is on the header or footer or
            // IGNORE_ITEM_VIEW_TYPE or enabled is false on the adapter
            boolean allowSwipe = swipeListView.getAdapter().isEnabled(childPosition)
                    && swipeListView.getAdapter().getItemViewType(childPosition) >= 0;

            if (allowSwipe && rect.contains(x, y)) {
                setParentView(child);
                setFrontView(child.findViewById(swipeFrontView));

                downX = motionEvent.getRawX();
                downPosition = childPosition;

                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 || downPosition == ListView.INVALID_POSITION) {
            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 * 2 < velocityX) {
            swapRight = velocityTracker.getXVelocity() > 0;
            Log.d("SwipeListView", "swapRight: " + swapRight + " - swipingRight: " + swipingRight);
            if (swapRight != swipingRight && swipeActionLeft != swipeActionRight) {
                swap = false;
            } else 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);
        if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
            swapChoiceState(downPosition);
        }

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

    case MotionEvent.ACTION_MOVE: {
        if (velocityTracker == null || paused || downPosition == ListView.INVALID_POSITION) {
            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);

        int swipeMode = this.swipeMode;
        int changeSwipeMode = swipeListView.changeSwipeMode(downPosition);
        if (changeSwipeMode >= 0) {
            swipeMode = changeSwipeMode;
        }

        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;
            swipingRight = (deltaX > 0);
            Log.d("SwipeListView", "deltaX: " + deltaX + " - swipingRight: " + swipingRight);
            if (opened.get(downPosition)) {
                swipeListView.onStartClose(downPosition, swipingRight);
                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_CHOICE) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_CHOICE) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
                } else {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
                }
                swipeListView.onStartOpen(downPosition, swipeCurrentAction, swipingRight);
            }
            swipeListView.requestDisallowInterceptTouchEvent(true);
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            swipeListView.onTouchEvent(cancelEvent);
            if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
                backView.setVisibility(View.GONE);
            }
        }

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

    closeOtherOpenedItems();
    view.onTouchEvent(motionEvent);
    return true;

    // return false;
}

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

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

    if (!isEnabled() || !canChildScrollUp() || isLoading || isRefreshing || mNestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }//  w ww .j  av  a 2 s  . com

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

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

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

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

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

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

    return true;
}