Java Code Examples for android.view.View#dispatchTouchEvent()

The following examples show how to use android.view.View#dispatchTouchEvent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GestureControllerForPager.java    From GestureViews with Apache License 2.0 7 votes vote down vote up
@SuppressLint("ClickableViewAccessibility") // Not needed for ViewPager
@Override
public boolean onTouch(View view, @NonNull MotionEvent event) {
    // ViewPager will steal touch events during settling regardless of
    // requestDisallowInterceptTouchEvent. We will prevent it here.
    if (!isTouchInProgress && event.getActionMasked() == MotionEvent.ACTION_DOWN) {
        isTouchInProgress = true;
        // Now ViewPager is in drag mode, so it should not intercept DOWN event
        view.dispatchTouchEvent(event);
        isTouchInProgress = false;
        return true;
    }

    // User can touch outside of child view, so we will not have a chance to settle
    // ViewPager. If so, this listener should be called and we will be able to settle
    // ViewPager manually.
    settleViewPagerIfFinished((ViewPager) view, event);

    return true; // We should skip view pager touches to prevent some subtle bugs
}
 
Example 2
Source File: FormEntryActivity.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
@SuppressLint("NewApi")
@Override
public boolean dispatchTouchEvent(MotionEvent mv) {
    //We need to ignore this even if it's processed by the action
    //bar (if one exists)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ActionBar bar = getActionBar();
        if (bar != null) {
            View customView = bar.getCustomView();
            if (customView != null && customView.dispatchTouchEvent(mv)) {
                return true;
            }
        }
    }

    boolean handled = mGestureDetector.onTouchEvent(mv);
    return handled || super.dispatchTouchEvent(mv);
}
 
Example 3
Source File: RecyclerViewUtils.java    From PowerfulRecyclerView with Apache License 2.0 6 votes vote down vote up
private void checkHitArea(View recyclerViewHeader, MotionEvent motionEvent, int pointerIndex) {
    final float x = MotionEventCompat.getX(motionEvent, pointerIndex);
    final float y = MotionEventCompat.getY(motionEvent, pointerIndex);

    Rect rect = new Rect();
    recyclerViewHeader.getHitRect(rect);
    if((rect.contains((int)x,(int)y))){
        recyclerViewHeader.dispatchTouchEvent(motionEvent);

        shouldIntercept = true;

        return;
    }

    shouldIntercept = false;
}
 
Example 4
Source File: TouchDispatchView.java    From WayHoo with Apache License 2.0 6 votes vote down vote up
public boolean onTouchEvent(MotionEvent event) {
	if (!isInterceptTouches)
		return super.onTouchEvent(event);
	int count = getChildCount();
	if (count < 0)
		return isInterceptTouches;
	
	for (int i = 0; i < count; ++i) {
		View childView = getChildAt(i);
		float oldX = event.getX();
		float oldY = event.getY();
		float x = event.getX() - childView.getLeft();
		float y = event.getY() - childView.getTop();
		if (((y >= 0.0F) && (x >= 0.0F))
				|| ((MotionEvent.ACTION_MASK & event.getAction()) != MotionEvent.ACTION_DOWN)){
			//L.i("liweiping", "new touch --> x = " + x +", y = " + y);
			event.setLocation(x, y);
		}else{
			//L.i("liweiping", "old touch --> oldX = " + oldX +", oldY = " + oldY);
			event.setLocation(oldX, oldY);
		}
		childView.dispatchTouchEvent(event);
	}
	return isInterceptTouches;
}
 
Example 5
Source File: SnackbarView.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {
    if (!isInBounds(ev, child)) return false;
    ev.offsetLocation(-child.getX(), -child.getY());
    child.dispatchTouchEvent(ev);
    return true;
}
 
Example 6
Source File: TouchInterceptionFrameLayout.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
/**
 * Duplicate touch events to child views.
 * We want to dispatch a down motion event and the move events to
 * child views, but calling dispatchTouchEvent() causes StackOverflowError.
 * Therefore we do it manually.
 *
 * @param ev            motion event to be passed to children
 * @param pendingEvents pending events like ACTION_DOWN. This will be passed to the children before ev
 */
private void duplicateTouchEventForChildren(MotionEvent ev, MotionEvent... pendingEvents) {
    if (ev == null) {
        return;
    }
    for (int i = getChildCount() - 1; 0 <= i; i--) {
        View childView = getChildAt(i);
        if (childView != null) {
            Rect childRect = new Rect();
            childView.getHitRect(childRect);
            MotionEvent event = MotionEvent.obtainNoHistory(ev);
            if (!childRect.contains((int) event.getX(), (int) event.getY())) {
                continue;
            }
            float offsetX = -childView.getLeft();
            float offsetY = -childView.getTop();
            boolean consumed = false;
            if (pendingEvents != null) {
                for (MotionEvent pe : pendingEvents) {
                    if (pe != null) {
                        MotionEvent peAdjusted = MotionEvent.obtainNoHistory(pe);
                        peAdjusted.offsetLocation(offsetX, offsetY);
                        consumed |= childView.dispatchTouchEvent(peAdjusted);
                    }
                }
            }
            event.offsetLocation(offsetX, offsetY);
            consumed |= childView.dispatchTouchEvent(event);
            if (consumed) {
                break;
            }
        }
    }
}
 
Example 7
Source File: SlideLayout.java    From SlideLayout with GNU General Public License v2.0 5 votes vote down vote up
private boolean cancelMotionEvent(MotionEvent event, View mDispatchView){
  	MotionEvent cancelEvent = MotionEvent.obtain(event);
      cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
      
      boolean result;
      if(mDispatchView != null)
      	result = mDispatchView.dispatchTouchEvent(cancelEvent) || super.dispatchTouchEvent(cancelEvent);
      else
      	result = super.dispatchTouchEvent(cancelEvent);
      
cancelEvent.recycle();

return result;
  }
 
Example 8
Source File: ObservableWebViewWithHeader.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent me){

   boolean wasInTitle = false;
   switch(me.getActionMasked()){
   case MotionEvent.ACTION_DOWN:
      touchInHeader = (me.getY() <= visibleHeaderHeight());
      break;

   case MotionEvent.ACTION_UP:
   case MotionEvent.ACTION_CANCEL:
      wasInTitle = touchInHeader;
      touchInHeader = false;
      break;
   }
   if (touchInHeader || wasInTitle) {
      View title = getChildAt(0);
      if(title!=null) {
         // this touch belongs to title bar, dispatch it here
         me.offsetLocation(0, getScrollY());
         return title.dispatchTouchEvent(me);
      }
   }
   // this is our touch, offset and process
   me.offsetLocation(0, -headerHeight);
   return super.dispatchTouchEvent(me);
}
 
Example 9
Source File: ClickSliderView.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public boolean dispatchTouchEventToView(View view, MotionEvent ev) {
    try {
        return view.dispatchTouchEvent(ev);
    } catch (Exception e) {
        // 部分机型会抛异常
        e.printStackTrace();
    }
    return false;
}
 
Example 10
Source File: LayoutManager.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public boolean propagateEvent(MotionEvent e) {
    if (e == null) return false;

    View view = getActiveLayout().getViewForInteraction();
    if (view == null) return false;

    e.offsetLocation(-view.getLeft(), -view.getTop());
    return view.dispatchTouchEvent(e);
}
 
Example 11
Source File: CompositorViewHolderBehavior.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {
    if (!mShouldIntercept) return false;
    ev.offsetLocation(-child.getX(), -child.getY());
    child.dispatchTouchEvent(ev);
    return true;
}
 
Example 12
Source File: PasswordEditTextTest.java    From materialandroid with Apache License 2.0 5 votes vote down vote up
private void fireTouchEvent(View view, float xPosition, float yPosition, int actionDown) {
  MotionEvent motionEvent = MotionEvent.obtain(
      SystemClock.uptimeMillis(),
      SystemClock.uptimeMillis(),
      actionDown,
      xPosition,
      yPosition,
      0
  );
  view.dispatchTouchEvent(motionEvent);
}
 
Example 13
Source File: TouchInterceptionLayout.java    From UltimateRecyclerView with Apache License 2.0 5 votes vote down vote up
/**
 * Duplicate touch events to child views.
 * We want to dispatch a down motion event and the move events to
 * child views, but calling dispatchTouchEvent() causes StackOverflowError.
 * Therefore we do it manually.
 *
 * @param ev            motion event to be passed to children
 * @param pendingEvents pending events like ACTION_DOWN. This will be passed to the children before ev
 */
private void duplicateTouchEventForChildren(MotionEvent ev, MotionEvent... pendingEvents) {
    if (ev == null) {
        return;
    }
    for (int i = getChildCount() - 1; 0 <= i; i--) {
        View childView = getChildAt(i);
        if (childView != null) {
            Rect childRect = new Rect();
            childView.getHitRect(childRect);
            MotionEvent event = MotionEvent.obtainNoHistory(ev);
            if (!childRect.contains((int) event.getX(), (int) event.getY())) {
                continue;
            }
            float offsetX = -childView.getLeft();
            float offsetY = -childView.getTop();
            boolean consumed = false;
            if (pendingEvents != null) {
                for (MotionEvent pe : pendingEvents) {
                    if (pe != null) {
                        MotionEvent peAdjusted = MotionEvent.obtainNoHistory(pe);
                        peAdjusted.offsetLocation(offsetX, offsetY);
                        consumed |= childView.dispatchTouchEvent(peAdjusted);
                    }
                }
            }
            event.offsetLocation(offsetX, offsetY);
            consumed |= childView.dispatchTouchEvent(event);
            if (consumed) {
                break;
            }
        }
    }
}
 
Example 14
Source File: LayoutManager.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public boolean propagateEvent(MotionEvent e) {
    if (e == null) return false;

    View view = getActiveLayout().getViewForInteraction();
    if (view == null) return false;

    e.offsetLocation(-view.getLeft(), -view.getTop());
    return view.dispatchTouchEvent(e);
}
 
Example 15
Source File: TimePickerClockDelegate.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    final int actionMasked = motionEvent.getActionMasked();
    if (actionMasked == MotionEvent.ACTION_DOWN) {
        if (view instanceof ViewGroup) {
            mInitialTouchTarget = findNearestChild((ViewGroup) view,
                    (int) motionEvent.getX(), (int) motionEvent.getY());
        } else {
            mInitialTouchTarget = null;
        }
    }

    final View child = mInitialTouchTarget;
    if (child == null) {
        return false;
    }

    final float offsetX = view.getScrollX() - child.getLeft();
    final float offsetY = view.getScrollY() - child.getTop();
    motionEvent.offsetLocation(offsetX, offsetY);
    final boolean handled = child.dispatchTouchEvent(motionEvent);
    motionEvent.offsetLocation(-offsetX, -offsetY);

    if (actionMasked == MotionEvent.ACTION_UP
            || actionMasked == MotionEvent.ACTION_CANCEL) {
        mInitialTouchTarget = null;
    }

    return handled;
}
 
Example 16
Source File: BGAGuideLinkageLayout.java    From KUtils-master with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        try {
            child.dispatchTouchEvent(ev);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}
 
Example 17
Source File: BGAGuideLinkageLayout.java    From KUtils with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        try {
            child.dispatchTouchEvent(ev);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}
 
Example 18
Source File: PhotoAttachPhotoCell.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean performAccessibilityAction(int action, Bundle arguments) {
    if (action == R.id.acc_action_open_photo) {
        View parent = (View) getParent();
        parent.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, getLeft(), getTop() + getHeight() - 1, 0));
        parent.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, getLeft(), getTop() + getHeight() - 1, 0));
    }
    return super.performAccessibilityAction(action, arguments);
}
 
Example 19
Source File: TouchExpansionDelegateTest.java    From litho with Apache License 2.0 4 votes vote down vote up
public static void emulateClickEvent(View view, int x, int y) {
  MotionEvent down = obtain(uptimeMillis(), uptimeMillis(), ACTION_DOWN, x, y, 0);
  MotionEvent up = obtain(uptimeMillis() + 10, uptimeMillis() + 10, ACTION_UP, x, y, 0);
  view.dispatchTouchEvent(down);
  view.dispatchTouchEvent(up);
}
 
Example 20
Source File: ZoomButtonsController.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * @hide The ZoomButtonsController implements the OnTouchListener, but this
 *       does not need to be shown in its public API.
 */
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();

    if (event.getPointerCount() > 1) {
        // ZoomButtonsController doesn't handle mutitouch. Give up control.
        return false;
    }

    if (mReleaseTouchListenerOnUp) {
        // The controls were dismissed but we need to throw away all events until the up
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            mOwnerView.setOnTouchListener(null);
            setTouchTargetView(null);
            mReleaseTouchListenerOnUp = false;
        }

        // Eat this event
        return true;
    }

    dismissControlsDelayed(ZOOM_CONTROLS_TIMEOUT);

    View targetView = mTouchTargetView;

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            targetView = findViewForTouch((int) event.getRawX(), (int) event.getRawY());
            setTouchTargetView(targetView);
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            setTouchTargetView(null);
            break;
    }

    if (targetView != null) {
        // The upperleft corner of the target view in raw coordinates
        int targetViewRawX = mContainerRawLocation[0] + mTouchTargetWindowLocation[0];
        int targetViewRawY = mContainerRawLocation[1] + mTouchTargetWindowLocation[1];

        MotionEvent containerEvent = MotionEvent.obtain(event);
        // Convert the motion event into the target view's coordinates (from
        // owner view's coordinates)
        containerEvent.offsetLocation(mOwnerViewRawLocation[0] - targetViewRawX,
                mOwnerViewRawLocation[1] - targetViewRawY);
        /* Disallow negative coordinates (which can occur due to
         * ZOOM_CONTROLS_TOUCH_PADDING) */
        // These are floats because we need to potentially offset away this exact amount
        float containerX = containerEvent.getX();
        float containerY = containerEvent.getY();
        if (containerX < 0 && containerX > -ZOOM_CONTROLS_TOUCH_PADDING) {
            containerEvent.offsetLocation(-containerX, 0);
        }
        if (containerY < 0 && containerY > -ZOOM_CONTROLS_TOUCH_PADDING) {
            containerEvent.offsetLocation(0, -containerY);
        }
        boolean retValue = targetView.dispatchTouchEvent(containerEvent);
        containerEvent.recycle();
        return retValue;

    } else {
        return false;
    }
}