Example usage for android.support.v4.view MotionEventCompat ACTION_POINTER_INDEX_SHIFT

List of usage examples for android.support.v4.view MotionEventCompat ACTION_POINTER_INDEX_SHIFT

Introduction

In this page you can find the example usage for android.support.v4.view MotionEventCompat ACTION_POINTER_INDEX_SHIFT.

Prototype

int ACTION_POINTER_INDEX_SHIFT

To view the source code for android.support.v4.view MotionEventCompat ACTION_POINTER_INDEX_SHIFT.

Click Source Link

Document

Synonym for MotionEvent#ACTION_POINTER_INDEX_SHIFT .

Usage

From source file:com.xpread.swipelistview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(android.view.View,
 *      android.view.MotionEvent)/*from   w ww . ja  va  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.lgq.rssreader.controls.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;
        }

        closeOpenedItems();

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

        if (swipeAutoClose) {
            float x = ViewHelper.getX(frontView);
            if (x > 0 && Math.abs(x) > viewWidth * getSwipeAutoCloseThreshold())
                swipeListView.onRightAutoClose(downPosition, backView);
            if (x < 0 && Math.abs(x) > viewWidth * getSwipeAutoCloseThreshold())
                swipeListView.onLeftAutoClose(downPosition, backView);
        }

        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;

                //                        if(deltaX > 0)
                //                           deltaX = deltaX > viewWidth ? viewWidth - 280 : deltaX;
                //                           
                //                       if(deltaX < 0)
                //                           deltaX = Math.abs(deltaX) > viewWidth ? -viewWidth +280 : deltaX;

            }

            move(deltaX);

            return true;
        }
        break;
    }
    }
    return false;
}

From source file:com.praisehim.koraildelay.SwipeLibrary.SwipeListViewTouchListener.java

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

                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: {
        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;
            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;
}

From source file:com.peerless2012.twowaynestedscrollview.TwoWayNestedScrollView.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = (ev.getAction()
            & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >> MotionEventCompat.ACTION_POINTER_INDEX_SHIFT;
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionX = (int) MotionEventCompat.getX(ev, newPointerIndex);
        mLastMotionY = (int) MotionEventCompat.getY(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();//from ww w.j a  v  a  2 s  .  c  om
        }
    }
}

From source file:caesar.feng.framework.widget.StaggeredGrid.ExtendableListView.java

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

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

/**
 * @see View.OnTouchListener#onTouch(android.view.View,
 *      android.view.MotionEvent)/*from w ww . j  a v  a2s  .  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));
//
// 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;
}