List of usage examples for android.view ViewParent requestDisallowInterceptTouchEvent
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept);
From source file:com.albedinsky.android.support.ui.widget.ViewPagerWidget.java
/** * Requests the current parent to disallow intercepting of touch event by {@link ViewParent#requestDisallowInterceptTouchEvent(boolean)}. * * @param disallow {@code True} to disallow, {@code false} otherwise. *//*from w w w .j ava 2 s. c o m*/ private void requestParentDisallowInterceptTouchEvent(boolean disallow) { final ViewParent parent = getParent(); if (parent != null) { parent.requestDisallowInterceptTouchEvent(disallow); } }
From source file:com.github.chrisbanes.photoview.PhotoViewAttacher.java
@Override public boolean onTouch(View v, MotionEvent ev) { boolean handled = false; if (mZoomEnabled && Util.hasDrawable((ImageView) v)) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: ViewParent parent = v.getParent(); // First, disable the Parent from intercepting the touch // event if (parent != null) { parent.requestDisallowInterceptTouchEvent(true); }/*from www .ja v a2 s .c o m*/ // If we're flinging, and the user presses down, cancel // fling cancelFling(); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: // If the user has zoomed less than min scale, zoom back // to min scale if (getScale() < mMinScale) { RectF rect = getDisplayRect(); if (rect != null) { v.post(new AnimatedZoomRunnable(getScale(), mMinScale, rect.centerX(), rect.centerY())); handled = true; } } break; } // Try the Scale/Drag detector if (mScaleDragDetector != null) { boolean wasScaling = mScaleDragDetector.isScaling(); boolean wasDragging = mScaleDragDetector.isDragging(); handled = mScaleDragDetector.onTouchEvent(ev); boolean didntScale = !wasScaling && !mScaleDragDetector.isScaling(); boolean didntDrag = !wasDragging && !mScaleDragDetector.isDragging(); mBlockParentIntercept = didntScale && didntDrag; } // Check to see if the user double tapped if (mGestureDetector != null && mGestureDetector.onTouchEvent(ev)) { handled = true; } } return handled; }
From source file:com.antew.redditinpictures.library.widget.SwipeListView.java
@Override public boolean onTouchEvent(MotionEvent event) { // Store width of this list for usage of swipe distance detection if (mViewWidth < 2) { mViewWidth = getWidth();//from w w w.j av a 2s . c om } switch (MotionEventCompat.getActionMasked(event)) { case MotionEvent.ACTION_DOWN: int[] viewCoords = new int[2]; // Figure out where the touch occurred. getLocationOnScreen(viewCoords); int touchX = (int) event.getRawX() - viewCoords[0]; int touchY = (int) event.getRawY() - viewCoords[1]; Rect rect = new Rect(); View child; int childCount = getChildCount(); for (int i = getHeaderViewsCount(); i <= childCount; i++) { // Go through each child view (excluding headers) and see if our touch pressed it. child = getChildAt(i); if (child != null) { //Get the child hit rectangle. child.getHitRect(rect); //If the child would be hit by this press. if (rect.contains(touchX, touchY)) { // DIRECT HIT! You sunk my battleship. Now that we know which view was touched, store it off for use if a move occurs. View frontView = child.findViewById(mFrontViewId); View backView = child.findViewById(mBackViewId); // Create our view pair. mViewPair = new SwipeableViewPair(frontView, backView); break; } } } if (mViewPair != null) { // If we have a view pair, record details about the inital touch for use later. mDownX = event.getRawX(); mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(event); } break; case MotionEvent.ACTION_UP: if (mVelocityTracker != null) { // Add the movement so we can calculate velocity. mVelocityTracker.addMovement(event); mVelocityTracker.computeCurrentVelocity(1000); float deltaX = event.getRawX() - mDownX; float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); if (mViewPair != null) { boolean shouldSwipe = false; // If the view has been moved a significant enough distance or if the view was flung, check to see if we should swipe it. if ((Math.abs(deltaX) > mViewWidth / 2 && mState == State.SWIPING) || (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityX > velocityY)) { if (mSwipeDirection == SWIPE_DIRECTION_BOTH) { // If the list is setup to swipe in either direction, just let it go. shouldSwipe = true; } else if (mSwipeDirection == SWIPE_DIRECTION_LEFT && deltaX < 0) { // If the list is only setup to swipe left, then only allow swiping to the left. shouldSwipe = true; } else if (mSwipeDirection == SWIPE_DIRECTION_RIGHT && deltaX > 0) { // If the list is only setup to swipe right, then only allow swiping to the right. shouldSwipe = true; } } if (shouldSwipe) { // If a swipe should occur meaning someone has let go of a view they were moving and it was far/fast enough for us to consider it a swipe start the animations. mViewPair.mFrontView.animate().translationX(deltaX >= 0 ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime); mViewPair.mBackView.animate().alpha(1).setDuration(mAnimationTime); // Now that the item is open, store it off so we can close it when we scroll if needed. mSwipedViews.put(mViewPair.hashCode(), mViewPair); // Clear out current variables as they are no longer needed and recycle the velocity tracker. resetState(); } else { // If the user let go of the view and we don't think the swipe was intended to occur (it was cancelled basically) reset the views. // Make sure the back disappears, since if it has buttons these can intercept touches from the front view. mViewPair.mBackView.setVisibility(View.GONE); mViewPair.mFrontView.animate().translationX(0).alpha(1).setDuration(mAnimationTime); // Clear out current variables as they are no longer needed and recycle the velocity tracker. resetState(); } } } break; case MotionEvent.ACTION_MOVE: if (mVelocityTracker != null && mState != State.SCROLLING) { // If this is an initial movement and we aren't already swiping. // Add the movement so we can calculate velocity. mVelocityTracker.addMovement(event); mVelocityTracker.computeCurrentVelocity(1000); float deltaX = event.getRawX() - mDownX; float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); // If the movement has been more than what is considered slop, and they are clearing moving horizontal not vertical. if (Math.abs(deltaX) > mTouchSlop && velocityX > velocityY) { boolean initiateSwiping = false; if (mSwipeDirection == SWIPE_DIRECTION_BOTH) { // If the list is setup to swipe in either direction, just let it go. initiateSwiping = true; } else if (mSwipeDirection == SWIPE_DIRECTION_LEFT && deltaX < 0) { // If the list is only setup to swipe left, then only allow swiping to the left. initiateSwiping = true; } else if (mSwipeDirection == SWIPE_DIRECTION_RIGHT && deltaX > 0) { // If the list is only setup to swipe right, then only allow swiping to the right. initiateSwiping = true; } if (initiateSwiping) { ViewParent parent = getParent(); if (parent != null) { // Don't allow parent to intercept touch (prevents NavigationDrawers from intercepting when near the bezel). parent.requestDisallowInterceptTouchEvent(true); } // Change our state to swiping to start tranforming the item. changeState(State.SWIPING); // Make sure that touches aren't intercepted. requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch to prevent it from being focused. MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); super.onTouchEvent(cancelEvent); } else { // Otherwise we need to cancel the touch event to prevent accidentally selecting the item and also preventing the swipe in the wrong direction or an incomplete touch from moving the view. MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); super.onTouchEvent(cancelEvent); } } if (mState == State.SWIPING && mViewPair != null) { // Make sure the back is visible. mViewPair.mBackView.setVisibility(View.VISIBLE); //Fade the back in and front out as they move. mViewPair.mBackView.setAlpha(Math.min(1f, 2f * Math.abs(deltaX) / mViewWidth)); mViewPair.mFrontView.setTranslationX(deltaX); mViewPair.mFrontView .setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } } break; } return super.onTouchEvent(event); }
From source file:com.davidtpate.swipelistview.SwipeListView.java
@Override public boolean onTouchEvent(MotionEvent event) { // Store width of this list for usage of swipe distance detection if (mViewWidth < 2) { mViewWidth = getWidth();/*from w w w . j a v a 2s. c om*/ } switch (MotionEventCompat.getActionMasked(event)) { case MotionEvent.ACTION_DOWN: int[] viewCoords = new int[2]; // Figure out where the touch occurred. getLocationOnScreen(viewCoords); int touchX = (int) event.getRawX() - viewCoords[0]; int touchY = (int) event.getRawY() - viewCoords[1]; Rect rect = new Rect(); View child; int childCount = getChildCount(); for (int i = getHeaderViewsCount(); i <= childCount; i++) { // Go through each child view (excluding headers) and see if our touch pressed it. child = getChildAt(i); if (child != null) { //Get the child hit rectangle. child.getHitRect(rect); //If the child would be hit by this press. if (rect.contains(touchX, touchY)) { // DIRECT HIT! You sunk my battleship. Now that we know which view was touched, store it off for use if a move occurs. View frontView = child.findViewById(mFrontViewId); View backView = child.findViewById(mBackViewId); // Create our view pair. mViewPair = new SwipeableViewPair(frontView, backView); break; } } } if (mViewPair != null) { // If we have a view pair, record details about the inital touch for use later. mDownX = event.getRawX(); mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(event); } break; case MotionEvent.ACTION_UP: if (mVelocityTracker != null) { // Add the movement so we can calculate velocity. mVelocityTracker.addMovement(event); mVelocityTracker.computeCurrentVelocity(1000); float deltaX = event.getRawX() - mDownX; float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); if (mViewPair != null) { boolean shouldSwipe = false; // If the view has been moved a significant enough distance or if the view was flung, check to see if we should swipe it. if ((Math.abs(deltaX) > mViewWidth / 2 && mState == State.SWIPING) || (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityX > velocityY)) { if (mSwipeDirection == SWIPE_DIRECTION_BOTH) { // If the list is setup to swipe in either direction, just let it go. shouldSwipe = true; } else if (mSwipeDirection == SWIPE_DIRECTION_LEFT && deltaX < 0) { // If the list is only setup to swipe left, then only allow swiping to the left. shouldSwipe = true; } else if (mSwipeDirection == SWIPE_DIRECTION_RIGHT && deltaX > 0) { // If the list is only setup to swipe right, then only allow swiping to the right. shouldSwipe = true; } } if (shouldSwipe) { // If a swipe should occur meaning someone has let go of a view they were moving and it was far/fast enough for us to consider it a swipe start the animations. ViewPropertyAnimator.animate(mViewPair.mFrontView) .translationX(deltaX >= 0 ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime); ViewPropertyAnimator.animate(mViewPair.mBackView).alpha(1).setDuration(mAnimationTime); // Now that the item is open, store it off so we can close it when we scroll if needed. mSwipedViews.put(mViewPair.hashCode(), mViewPair); // Clear out current variables as they are no longer needed and recycle the velocity tracker. resetState(); } else { // If the user let go of the view and we don't think the swipe was intended to occur (it was cancelled basically) reset the views. // Make sure the back disappears, since if it has buttons these can intercept touches from the front view. mViewPair.mBackView.setVisibility(View.GONE); ViewPropertyAnimator.animate(mViewPair.mFrontView).translationX(0).alpha(1) .setDuration(mAnimationTime); // Clear out current variables as they are no longer needed and recycle the velocity tracker. resetState(); } } } break; case MotionEvent.ACTION_MOVE: if (mVelocityTracker != null && mState != State.SCROLLING) { // If this is an initial movement and we aren't already swiping. // Add the movement so we can calculate velocity. mVelocityTracker.addMovement(event); mVelocityTracker.computeCurrentVelocity(1000); float deltaX = event.getRawX() - mDownX; float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); // If the movement has been more than what is considered slop, and they are clearing moving horizontal not vertical. if (Math.abs(deltaX) > mTouchSlop && velocityX > velocityY) { boolean initiateSwiping = false; if (mSwipeDirection == SWIPE_DIRECTION_BOTH) { // If the list is setup to swipe in either direction, just let it go. initiateSwiping = true; } else if (mSwipeDirection == SWIPE_DIRECTION_LEFT && deltaX < 0) { // If the list is only setup to swipe left, then only allow swiping to the left. initiateSwiping = true; } else if (mSwipeDirection == SWIPE_DIRECTION_RIGHT && deltaX > 0) { // If the list is only setup to swipe right, then only allow swiping to the right. initiateSwiping = true; } if (initiateSwiping) { ViewParent parent = getParent(); if (parent != null) { // Don't allow parent to intercept touch (prevents NavigationDrawers from intercepting when near the bezel). parent.requestDisallowInterceptTouchEvent(true); } // Change our state to swiping to start tranforming the item. changeState(State.SWIPING); // Make sure that touches aren't intercepted. requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch to prevent it from being focused. MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); super.onTouchEvent(cancelEvent); } else { // Otherwise we need to cancel the touch event to prevent accidentally selecting the item and also preventing the swipe in the wrong direction or an incomplete touch from moving the view. MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); super.onTouchEvent(cancelEvent); } } if (mState == State.SWIPING && mViewPair != null) { // Make sure the back is visible. mViewPair.mBackView.setVisibility(View.VISIBLE); //Fade the back in and front out as they move. ViewHelper.setAlpha(mViewPair.mBackView, Math.min(1f, 2f * Math.abs(deltaX) / mViewWidth)); ViewHelper.setTranslationX(mViewPair.mFrontView, deltaX); ViewHelper.setAlpha(mViewPair.mFrontView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } } break; } return super.onTouchEvent(event); }
From source file:com.frank.protean.photoview.PhotoViewAttacher.java
@Override public boolean onTouch(View v, MotionEvent ev) { boolean handled = false; if (mZoomEnabled && Util.hasDrawable((ImageView) v)) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: ViewParent parent = v.getParent(); // First, disable the Parent from intercepting the touch // event if (parent != null) { parent.requestDisallowInterceptTouchEvent(true); }//from w w w . j a v a 2 s. c o m // If we're flinging, and the user presses down, cancel // fling cancelFling(); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: // If the user has zoomed less than min scale, zoom back // to min scale if (getScale() < mMinScale) { RectF rect = getDisplayRect(); if (rect != null) { v.post(new AnimatedZoomRunnable(getScale(), mMinScale, rect.centerX(), rect.centerY())); handled = true; } } else if (getScale() > mMaxScale) { RectF rect = getDisplayRect(); if (rect != null) { v.post(new AnimatedZoomRunnable(getScale(), mMaxScale, rect.centerX(), rect.centerY())); handled = true; } } break; } // Try the Scale/Drag detector if (mScaleDragDetector != null) { boolean wasScaling = mScaleDragDetector.isScaling(); boolean wasDragging = mScaleDragDetector.isDragging(); handled = mScaleDragDetector.onTouchEvent(ev); boolean didntScale = !wasScaling && !mScaleDragDetector.isScaling(); boolean didntDrag = !wasDragging && !mScaleDragDetector.isDragging(); mBlockParentIntercept = didntScale && didntDrag; } // Check to see if the user double tapped if (mGestureDetector != null && mGestureDetector.onTouchEvent(ev)) { handled = true; } } return handled; }
From source file:com.huyn.demogroup.leaveblank.PhotoViewAttacher.java
@Override public void onDrag(float dx, float dy) { if (mScaleDragDetector.isScaling()) { return; // Do not drag if we are already scaling }//ww w.ja va2 s. com System.out.println("++++ondrag:" + dx + "/" + dy); mSuppMatrix.postTranslate(dx, dy); checkAndDisplayMatrix(); /* * Here we decide whether to let the ImageView's parent to start taking * over the touch event. * * First we check whether this function is enabled. We never want the * parent to take over if we're scaling. We then check the edge we're * on, and the direction of the scroll (i.e. if we're pulling against * the edge, aka 'overscrolling', let the parent take over). */ ViewParent parent = mImageView.getParent(); if (mAllowParentInterceptOnEdge && !mScaleDragDetector.isScaling() && !mBlockParentIntercept) { if (mScrollEdge == EDGE_BOTH || (mScrollEdge == EDGE_LEFT && dx >= 1f) || (mScrollEdge == EDGE_RIGHT && dx <= -1f)) { if (parent != null) { parent.requestDisallowInterceptTouchEvent(false); } } } else { if (parent != null) { parent.requestDisallowInterceptTouchEvent(true); } } }
From source file:com.hippo.largeimageview.LargeImageView.java
@Override public boolean onTouchEvent(MotionEvent event) { // Always call parent.requestDisallowInterceptTouchEvent(true) // When get edge, translate() will call parent.requestDisallowInterceptTouchEvent(false) final ViewParent parent = getParent(); if (parent != null) { parent.requestDisallowInterceptTouchEvent(true); }/* ww w . j a v a 2 s . co m*/ mGestureRecognizer.onTouchEvent(event); return true; }
From source file:com.github.chrisbanes.photoview.PhotoViewAttacher.java
@Override public void onDrag(float dx, float dy) { if (mScaleDragDetector.isScaling()) { return; // Do not drag if we are already scaling }/*from w w w. j a v a2 s . co m*/ mSuppMatrix.postTranslate(dx, dy); checkAndDisplayMatrix(); /* * Here we decide whether to let the ImageView's parent to start taking * over the touch event. * * First we check whether this function is enabled. We never want the * parent to take over if we're scaling. We then check the edge we're * on, and the direction of the scroll (i.e. if we're pulling against * the edge, aka 'overscrolling', let the parent take over). */ ViewParent parent = mImageView.getParent(); if (mAllowParentInterceptOnEdge && !mScaleDragDetector.isScaling() && !mBlockParentIntercept) { if (mScrollEdge == EDGE_BOTH || (mScrollEdge == EDGE_LEFT && dx >= 1f) || (mScrollEdge == EDGE_RIGHT && dx <= -1f)) { if (parent != null) { parent.requestDisallowInterceptTouchEvent(false); } } } else { if (parent != null) { parent.requestDisallowInterceptTouchEvent(true); } } }
From source file:test.hugo.photoView.PhotoViewAttacher.java
@Override public void onDrag(float dx, float dy) { if (mScaleDragDetector.isScaling()) { return; // Do not drag if we are already scaling }//from w ww .j a v a 2 s. co m if (mOnViewDragListener != null) { mOnViewDragListener.onDrag(dx, dy); } mSuppMatrix.postTranslate(dx, dy); checkAndDisplayMatrix(); /* * Here we decide whether to let the ImageView's parent to start taking * over the touch event. * * First we check whether this function is enabled. We never want the * parent to take over if we're scaling. We then check the edge we're * on, and the direction of the scroll (i.e. if we're pulling against * the edge, aka 'overscrolling', let the parent take over). */ ViewParent parent = mImageView.getParent(); if (mAllowParentInterceptOnEdge && !mScaleDragDetector.isScaling() && !mBlockParentIntercept) { if (mScrollEdge == EDGE_BOTH || (mScrollEdge == EDGE_LEFT && dx >= 1f) || (mScrollEdge == EDGE_RIGHT && dx <= -1f)) { if (parent != null) { parent.requestDisallowInterceptTouchEvent(false); } } } else { if (parent != null) { parent.requestDisallowInterceptTouchEvent(true); } } }
From source file:com.hippo.largeimageview.LargeImageView.java
private void translate(float dx, float dy) { final int wWidth = mWindowWidth; final int wHeight = mWindowHeight; if (wWidth <= 0 || wHeight <= 0) { return;/*from www .ja v a2s . c o m*/ } final RectF dst = mDst; final float dWidth = dst.width(); final float dHeight = dst.height(); if (dWidth <= 0 || dHeight <= 0) { return; } final float remainX; final float remainY; if (dWidth > wWidth) { dst.left -= dx; dst.right -= dx; float fixXOffset = dst.left; if (fixXOffset > 0) { dst.left -= fixXOffset; dst.right -= fixXOffset; remainX = -fixXOffset; } else if ((fixXOffset = wWidth - dst.right) > 0) { dst.left += fixXOffset; dst.right += fixXOffset; remainX = fixXOffset; } else { remainX = 0; } } else { remainX = dx; } if (dHeight > wHeight) { dst.top -= dy; dst.bottom -= dy; float fixYOffset = dst.top; if (fixYOffset > 0) { dst.top -= fixYOffset; dst.bottom -= fixYOffset; remainY = -fixYOffset; } else if ((fixYOffset = wHeight - dst.bottom) > 0) { dst.top += fixYOffset; dst.bottom += fixYOffset; remainY = fixYOffset; } else { remainY = 0; } } else { remainY = dy; } // Check requestDisallowInterceptTouchEvent // Don't call requestDisallowInterceptTouchEvent when animated // Only call requestDisallowInterceptTouchEvent when on room for scroll left or right if (mAnimating == 0 && dx == remainX) { final ViewParent parent = getParent(); if (parent != null) { parent.requestDisallowInterceptTouchEvent(false); } } if (dx != remainX || dy != remainY) { mRectDirty = true; invalidate(); } }