Example usage for android.view MotionEvent getActionMasked

List of usage examples for android.view MotionEvent getActionMasked

Introduction

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

Prototype

public final int getActionMasked() 

Source Link

Document

Return the masked action being performed, without pointer index information.

Usage

From source file:org.chromium.chrome.browser.ntp.cards.NewTabPageRecyclerView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!mTouchEnabled)
        return false;

    // Action down would already have been handled in onInterceptTouchEvent
    if (ev.getActionMasked() != MotionEvent.ACTION_DOWN) {
        mGestureDetector.onTouchEvent(ev);
    }/*from www .j ava  2  s.  c om*/
    return super.onTouchEvent(ev);
}

From source file:org.telegram.ui.ActionBar.ActionBarMenuItem.java

public TextView addSubItem(int id, String text, int icon) {
    if (popupLayout == null) {
        rect = new Rect();
        location = new int[2];
        popupLayout = new ActionBarPopupWindow.ActionBarPopupWindowLayout(getContext());
        popupLayout.setOnTouchListener(new OnTouchListener() {
            @Override//from ww  w.ja  v a  2  s.  c  o m
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                    if (popupWindow != null && popupWindow.isShowing()) {
                        v.getHitRect(rect);
                        if (!rect.contains((int) event.getX(), (int) event.getY())) {
                            popupWindow.dismiss();
                        }
                    }
                }
                return false;
            }
        });
        popupLayout.setDispatchKeyEventListener(new ActionBarPopupWindow.OnDispatchKeyEventListener() {
            @Override
            public void onDispatchKeyEvent(KeyEvent keyEvent) {
                if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0
                        && popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
    }
    TextView textView = new TextView(getContext());
    textView.setTextColor(ContextCompat.getColor(getContext(), R.color.primary_text));
    textView.setBackgroundResource(R.drawable.list_selector);
    if (!LocaleController.isRTL) {
        textView.setGravity(Gravity.CENTER_VERTICAL);
    } else {
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
    }
    textView.setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    textView.setMinWidth(AndroidUtilities.dp(196));
    textView.setTag(id);
    textView.setText(text);
    if (icon != 0) {
        textView.setCompoundDrawablePadding(AndroidUtilities.dp(12));
        if (!LocaleController.isRTL) {
            textView.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(icon), null, null,
                    null);
        } else {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(icon),
                    null);
        }
    }
    popupLayout.setShowedFromBotton(showFromBottom);
    popupLayout.addView(textView);
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
    if (LocaleController.isRTL) {
        layoutParams.gravity = Gravity.RIGHT;
    }
    layoutParams.width = LayoutHelper.MATCH_PARENT;
    layoutParams.height = AndroidUtilities.dp(48);
    textView.setLayoutParams(layoutParams);
    textView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (popupWindow != null && popupWindow.isShowing()) {
                if (processedPopupClick) {
                    return;
                }
                processedPopupClick = true;
                popupWindow.dismiss(allowCloseAnimation);
            }
            if (parentMenu != null) {
                parentMenu.onItemClick((Integer) view.getTag());
            } else if (delegate != null) {
                delegate.onItemClick((Integer) view.getTag());
            }
        }
    });
    menuHeight += layoutParams.height;
    return textView;
}

From source file:com.commonsware.cwac.crossport.design.widget.HeaderBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {
    if (mTouchSlop < 0) {
        mTouchSlop = ViewConfiguration.get(parent.getContext()).getScaledTouchSlop();
    }/* ww w .ja v  a  2  s  .  c  om*/

    final int action = ev.getAction();

    // Shortcut since we're being dragged
    if (action == MotionEvent.ACTION_MOVE && mIsBeingDragged) {
        return true;
    }

    switch (ev.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        mIsBeingDragged = false;
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        if (canDragView(child) && parent.isPointInChildBounds(child, x, y)) {
            mLastMotionY = y;
            mActivePointerId = ev.getPointerId(0);
            ensureVelocityTracker();
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }
        final int pointerIndex = ev.findPointerIndex(activePointerId);
        if (pointerIndex == -1) {
            break;
        }

        final int y = (int) ev.getY(pointerIndex);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (yDiff > mTouchSlop) {
            mIsBeingDragged = true;
            mLastMotionY = y;
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(ev);
    }

    return mIsBeingDragged;
}

From source file:com.dpg.kodimote.view.SwipeableRecyclerViewTouchListener.java

private boolean handleTouchEvent(MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }/*from   w w w  . j  av a2s . com*/

    Log.d("Swipe", motionEvent.getActionMasked() + "");
    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            break;
        }

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mRecyclerView.getChildCount();
        int[] listViewCoords = new int[2];
        mRecyclerView.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 = mRecyclerView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                mDownView = child;
                break;
            }
        }

        if (mDownView != null && mAnimatingPosition != mRecyclerView.getChildLayoutPosition(mDownView)) {
            mAlpha = ViewCompat.getAlpha(mDownView);
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mRecyclerView.getChildLayoutPosition(mDownView);
            mSwipingLeft = mSwipeListener.canSwipeLeft(mDownPosition);
            mSwipingRight = mSwipeListener.canSwipeRight(mDownPosition);
            if (mSwipingLeft || mSwipingRight) {
                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(motionEvent);
            } else {
                mDownView = null;
            }
        }
        break;
    }

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

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

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

        mFinalDelta = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(mFinalDelta) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = mFinalDelta > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (mFinalDelta < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss && mDownPosition != mAnimatingPosition && mDownPosition != ListView.INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            mAnimatingPosition = mDownPosition;
            ViewCompat.animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new ViewPropertyAnimatorListener() {
                        @Override
                        public void onAnimationStart(View view) {
                            // Do nothing.
                        }

                        @Override
                        public void onAnimationEnd(View view) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                                performDismiss(downView, downPosition);
                            }
                        }

                        @Override
                        public void onAnimationCancel(View view) {
                            // Do nothing.
                        }
                    });
        } else {
            // cancel
            ViewCompat.animate(mDownView).translationX(0).alpha(mAlpha).setDuration(mAnimationTime)
                    .setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = ListView.INVALID_POSITION;
        mSwiping = false;
        break;
    }

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

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

        if (deltaX < 0 && !mSwipingLeft)
            mSwiping = false;
        if (deltaX > 0 && !mSwipingRight)
            mSwiping = false;

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

    return false;
}

From source file:org.creativecommons.thelist.swipedismiss.SwipeDismissRecyclerViewTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mRecyclerView.getWidth();
    }/*from w w  w. ja  v  a 2s. com*/

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            return false;
        }

        // TODO: ensure this is a finger, and set a flag

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mRecyclerView.getChildCount();
        int[] listViewCoords = new int[2];
        mRecyclerView.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 = mRecyclerView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                mDownView = child;
                break;
            }
        }

        if (mDownView != null) {
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mRecyclerView.getChildPosition(mDownView);
            if (mCallbacks.canDismiss(mDownPosition)) {
                mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(motionEvent);
            } else {
                mDownView = null;
            }
        }
        return false;
    } //OnTouch

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

        if (mDownView != null && mSwiping) {
            // cancel
            //TODO: DOES THIS WORK
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = ListView.INVALID_POSITION;
        mSwiping = false;
        mRefreshLayout.setEnabled(true);
        break;
    } //ACTION_CANCEL

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

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        //                float velocityX = Math.abs(mVelocityTracker.getXVelocity());
        //                float velocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss && mDownPosition != ListView.INVALID_POSITION) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            //TODO: add animation lock
            //                    synchronized (mAnimationLock){
            //                        if(mAnimatedViews.contains(downView)){
            //                            break;
            //                        }
            //                        ++mDismissAnimationRefCount;
            //                        mAnimatedViews.add(downView);
            //                    }

            animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime)
                    .setListener(new com.nineoldandroids.animation.AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(com.nineoldandroids.animation.Animator animation) {
                            performDismiss(downView, downPosition);
                        }
                    });
        } else {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDownY = 0;
        mDownView = null;
        mDownPosition = ListView.INVALID_POSITION;
        mSwiping = false;
        mRefreshLayout.setEnabled(true);
        break;
    } //ACTION_UP

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

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

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

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

From source file:com.lightstreamer.demo.android.DetailsFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    pnEnabled = false;//from  w  w w  . j  ava2s .c  om

    // If activity recreated (such as from screen rotate), restore
    // the previous article selection set by onSaveInstanceState().
    // This is primarily necessary when in the two-pane layout.
    if (savedInstanceState != null) {
        currentItem = savedInstanceState.getInt(ARG_ITEM);
        pnEnabled = savedInstanceState.getBoolean(ARG_PN_CONTROLS);
    }

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.details_view, container, false);

    toggle = (ToggleButton) view.findViewById(R.id.pn_switch);
    toggleContainer = view.findViewById(R.id.toggle_container);

    this.enablePN(pnEnabled);

    holder.put("stock_name", (TextView) view.findViewById(R.id.d_stock_name));
    holder.put("last_price", (TextView) view.findViewById(R.id.d_last_price));
    holder.put("time", (TextView) view.findViewById(R.id.d_time));
    holder.put("pct_change", (TextView) view.findViewById(R.id.d_pct_change));
    holder.put("bid_quantity", (TextView) view.findViewById(R.id.d_bid_quantity));
    holder.put("bid", (TextView) view.findViewById(R.id.d_bid));
    holder.put("ask", (TextView) view.findViewById(R.id.d_ask));
    holder.put("ask_quantity", (TextView) view.findViewById(R.id.d_ask_quantity));
    holder.put("min", (TextView) view.findViewById(R.id.d_min));
    holder.put("max", (TextView) view.findViewById(R.id.d_max));
    holder.put("open_price", (TextView) view.findViewById(R.id.d_open_price));

    final XYPlot plot = (XYPlot) view.findViewById(R.id.mySimpleXYPlot);
    chart.setPlot(plot);

    plot.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (!pnEnabled) {
                //push notifications not enabled, ignore the touch
                return false;
            }

            v.performClick();

            int action = event.getActionMasked();

            float touchY = event.getY();
            float touchX = event.getX();

            XYGraphWidget widget = plot.getGraphWidget();
            RectF gridRect = widget.getGridRect();
            if (gridRect.contains(touchX, touchY)) {

                if (currentSubscription != null) {
                    double triggerVal = widget.getYVal(touchY);

                    chart.setMovingTriggerLine(triggerVal);
                    if (action == MotionEvent.ACTION_UP) {
                        triggerVal = Math.round(triggerVal * 100.0) / 100.0;
                        chart.endMovingTriggerLine(triggerVal);

                        Log.d(TAG, "Touch released @ " + triggerVal);
                        //go on the network only after the touch has been released
                        subscriptionHandling.activateMPN(getMpnInfo(triggerVal));
                    }
                } else {
                    Log.v(TAG, "touch ignored");
                }
            }

            return true;
        }
    });

    return view;
}

From source file:danonino.danonino_the_game.Core.Labels.PowerUp.java

public int onTouch(MotionEvent event) {
    int index = MotionEventCompat.getActionIndex(event);
    int x;/*from  w w w.  j a  va  2 s.c  om*/
    int y;

    if (event.getPointerCount() > 1) {
        // The coordinates of the current screen contact, relative to
        // the responding View or Activity.
        x = (int) MotionEventCompat.getX(event, index);
        y = (int) MotionEventCompat.getY(event, index);

    } else {
        // Single touch event
        x = (int) MotionEventCompat.getX(event, index);
        y = (int) MotionEventCompat.getY(event, index);
    }

    if (this.usePowerUpBtn.btn_rect.contains(x, y) || ((event.getActionMasked() == MotionEvent.ACTION_UP
            || event.getActionMasked() == MotionEvent.ACTION_POINTER_UP) && usePowerUpBtnPressed)) {
        this.usePowerUpBtnPressed = !this.usePowerUpBtnPressed;
        this.usePowerUpBtn.onTouch(event);
        return 1;
    }
    return 0;
}

From source file:org.protocoderrunner.apprunner.AppRunnerActivity.java

@Override
public boolean onGenericMotionEvent(MotionEvent event) {

    int action = event.getAction();
    int actionCode = event.getActionMasked();

    ArrayList<PPadView.TouchEvent> t = new ArrayList<PPadView.TouchEvent>();

    // check finger if down or up
    if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) {

    }/*from  w  ww  .  ja v a2s  .  c o  m*/

    if (event.getSource() == MotionEvent.TOOL_TYPE_MOUSE) {
        // if (event.getButtonState() == ac) {

        // }
    }

    // get positions per finger
    for (int i = 0; i < event.getPointerCount(); i++) {
        // TouchEvent o = new TouchEvent("finger", event.getPointerId(i),
        // action, (int) event.getX(),
        // (int) event.getY());
        // t.add(o);
    }

    //
    // FINGER 1 UP x y
    // FINGER 2 MOVE x y
    // MOUSE 3 MOVE x y

    // return touching;
    return super.onGenericMotionEvent(event);
}

From source file:com.android.messaging.ui.conversationlist.ConversationListSwipeHelper.java

@Override
public void onTouchEvent(final RecyclerView recyclerView, final MotionEvent event) {
    // We should only be here if we intercepted the touch due to swipe.
    Assert.isTrue(mIsSwiping);/* w  w  w  . j  a v  a2s .  c o  m*/

    // We are now tracking a swipe gesture.
    mVelocityTracker.addMovement(event);

    final int action = event.getActionMasked();
    switch (action) {
    case MotionEvent.ACTION_OUTSIDE:
    case MotionEvent.ACTION_MOVE:
        if (hasValidGestureSwipeTarget()) {
            mListItemView.setSwipeTranslationX(event.getX() - mInitialX);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (hasValidGestureSwipeTarget()) {
            final float maxVelocity = mMaximumFlingVelocity;
            mVelocityTracker.computeCurrentVelocity(UNIT_SECONDS, maxVelocity);
            final float velocityX = getLastComputedXVelocity();

            final float translationX = mListItemView.getSwipeTranslationX();

            int swipeDirection = SWIPE_DIRECTION_NONE;
            if (translationX != 0) {
                swipeDirection = translationX > 0 ? SWIPE_DIRECTION_RIGHT : SWIPE_DIRECTION_LEFT;
            } else if (velocityX != 0) {
                swipeDirection = velocityX > 0 ? SWIPE_DIRECTION_RIGHT : SWIPE_DIRECTION_LEFT;
            }

            final boolean fastEnough = isTargetSwipedFastEnough();
            final boolean farEnough = isTargetSwipedFarEnough();

            final boolean shouldDismiss = (fastEnough || farEnough);

            if (shouldDismiss) {
                if (fastEnough) {
                    animateDismiss(mListItemView, velocityX);
                } else {
                    animateDismiss(mListItemView, swipeDirection);
                }
            } else {
                animateRestore(mListItemView, velocityX);
            }

            onSwipeGestureEnd(mListItemView, shouldDismiss ? swipeDirection : SWIPE_DIRECTION_NONE);
        } else {
            onGestureEnd();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (hasValidGestureSwipeTarget()) {
            animateRestore(mListItemView, 0f);
            onSwipeGestureEnd(mListItemView, SWIPE_DIRECTION_NONE);
        } else {
            onGestureEnd();
        }
        break;
    }
}

From source file:net.simonvt.cathode.ui.HomeActivity.java

@Override
protected void onCreate(Bundle inState) {
    super.onCreate(inState);
    Timber.d("onCreate");
    CathodeApp.inject(this);

    setContentView(R.layout.activity_home);

    ButterKnife.inject(this);

    isTablet = getResources().getBoolean(R.bool.isTablet);

    navigation = (NavigationFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_NAVIGATION);

    watchingParent.setOnTouchListener(new View.OnTouchListener() {
        @Override/*from w  w  w. ja v a 2 s  . com*/
        public boolean onTouch(View v, MotionEvent event) {
            if (watchingView.isExpanded()) {
                final int action = event.getActionMasked();
                if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
                    watchingView.collapse();
                }

                return true;
            }

            return false;
        }
    });
    watchingView.setWatchingViewListener(watchingListener);

    stack = FragmentStack.forContainer(this, R.id.content, new FragmentStack.Callback() {
        @Override
        public void onStackChanged(int stackSize, Fragment topFragment) {
            Timber.d("onStackChanged: %s", topFragment.getTag());
        }
    });
    stack.setDefaultAnimation(R.anim.fade_in_front, R.anim.fade_out_back, R.anim.fade_in_back,
            R.anim.fade_out_front);
    if (inState != null) {
        stack.restoreState(inState.getBundle(STATE_STACK));
    }
    if (stack.size() == 0) {
        stack.replace(UpcomingShowsFragment.class, FRAGMENT_SHOWS_UPCOMING);
        stack.commit();
    }

    getSupportLoaderManager().initLoader(Loaders.LOADER_SHOW_WATCHING, null, watchingShowCallback);
    getSupportLoaderManager().initLoader(Loaders.LOADER_MOVIE_WATCHING, null, watchingMovieCallback);

    messageBar = new MessageBar(this);

    drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
        }

        @Override
        public void onDrawerOpened(View drawerView) {
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            stack.commit();
        }

        @Override
        public void onDrawerStateChanged(int newState) {
            drawerState = newState;
        }
    });

    if (!Settings.isLoggedIn(this) || isLoginAction(getIntent())) {
        startLoginActivity();
    }
}