Example usage for android.view MotionEvent obtain

List of usage examples for android.view MotionEvent obtain

Introduction

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

Prototype

static public MotionEvent obtain(MotionEvent other) 

Source Link

Document

Create a new MotionEvent, copying from an existing one.

Usage

From source file:com.young.icontacts.view.SwipeListViewTouchListenerTwo.java

/**
 * @see View.OnTouchListener#(android.view.View, android.view.MotionEvent)
 *///from  w w  w  .j  a  v a2 s . c o m
@SuppressLint({ "ClickableViewAccessibility", "Recycle" })
@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;
            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 {
                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);
            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 true;
        }
        break;
    }
    }
    return false;
}

From source file:com.example.swipelistviewexample.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
 *///from   w w w  . j a v  a2  s. com
@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 - swipeListView.getHeaderViewsCount();

                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;
            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 {
                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);
            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 true;
        }
        break;
    }
    }
    return false;
}

From source file:cn.fantasee.swipwmenulistview.swipelistview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(View, MotionEvent)
 *//*from   ww  w. j a  v  a 2s. c om*/
@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);

                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;
            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 {
                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);
            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 true;
        }
        break;
    }
    }
    return false;
}

From source file:com.happen.app.util.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 if (swapRight != swipingRight) {
                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;
}

From source file:com.mjw.android.swipe.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(android.view.View,
 *      android.view.MotionEvent)//from   w  w  w.  j  ava  2s  .  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));

                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;
            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 {
                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);
            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 true;
        }
        break;
    }
    }
    return false;
}

From source file:com.gcloud.gaadi.ui.swipelistview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
 *//*from   w ww .j  ava2 s . c om*/
@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);

                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;
            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
                swap = !(opened.get(downPosition) && !openedRight.get(downPosition) && !swapRight);
        } 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);
            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 true;
        }
        break;
    }
    }
    return false;
}

From source file:com.proyecto.alberto.monedero.Swipe.SwipeListViewTouchListener.java

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

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

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

                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;
            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 {
                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);
            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 true;
        }
        break;
    }
    }
    return false;
}

From source file:com.peng.saishi.widget.swipelistview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
 *///from  www  . j  a  va2 s .c o  m
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (!isSwipeEnabled()) {
        closeOpenedItems();
        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);

                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;
            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 {
                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);
            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 true;
        }
        break;
    }
    }
    return false;
}

From source file:com.example.zhangyipeng.swipelibrary.SwipeListViewTouchListener2.java

/**
 * @see View.OnTouchListener#onTouch(View, MotionEvent)
 *//* www.  j  ava 2  s.  c om*/
@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;
            boolean allowSwipe = true;

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

                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;
            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 {
                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);
            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 true;
        }
        break;
    }
    }
    if (onlyOneOpenedWhenSwipe) {
        closeOtherOpenedItems();
        view.onTouchEvent(motionEvent);
        return true;
    }
    return false;
}

From source file:com.stackbase.mobapp.view.swipelistview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
 *//*from   w  w  w  .ja  v  a  2  s  . c om*/
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (!isSwipeEnabled()) {
        return false;
    }
    closeOtherOpenedItems();
    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);

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

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

                velocityTracker = VelocityTracker.obtain();
                velocityTracker.addMovement(motionEvent);
                if (swipeBackView > 0) {
                    setBackView(child.findViewById(swipeBackView));
                }
                break;
            }
        }
        view.onTouchEvent(motionEvent);
        if (swipeDrawableChecked > 0 && !opened.get(downPosition))
            frontView.setBackgroundResource(swipeDrawableChecked);

        return true;
    }

    case MotionEvent.ACTION_UP: {
        if (swipeDrawableUnchecked > 0)
            frontView.setBackgroundResource(swipeDrawableUnchecked);
        view.onTouchEvent(motionEvent);

        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 {
                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 (!swipeListView.isMovable(downPosition)) {
            return false;
        }

        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);
            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 true;
        }
        break;
    }
    }
    return false;
}