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

The following are Jave code examples for showing how to use dispatchTouchEvent() of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: GitHub   File: TouchInterceptionLayout.java   Source Code and License Vote up 6 votes
/**
 * 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 2
Project: DailyStudy   File: TouchLinearLayout.java   Source Code and License Vote up 6 votes
@Override
public boolean onTouchEvent(MotionEvent event) {
    int width = getWidth() / getChildCount();
    int height = getHeight();
    int count = getChildCount();
    float eventX = event.getX();
    if (eventX < width) {
        getChildAt(0).dispatchTouchEvent(event);
    } else if (eventX > width && eventX < 2 * width) {
        float eventY = event.getY();
        if (eventY < height / 2) {
            for (int i = 0; i < count; i++) {
                View child = getChildAt(i);
                child.dispatchTouchEvent(event);
            }
        } else {
            getChildAt(1).dispatchTouchEvent(event);
        }
    } else if (eventX > 2 * width) {
        getChildAt(2).dispatchTouchEvent(event);
    }

    return true;
}
 
Example 3
Project: DateTimePicker   File: TimePickerClockDelegate.java   Source Code and License Vote up 6 votes
@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 4
Project: KUtils   File: BGAGuideLinkageLayout.java   Source Code and License Vote up 5 votes
@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 5
Project: JD-Test   File: BGAGuideLinkageLayout.java   Source Code and License Vote up 5 votes
@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 6
Project: chromium-for-android-56-debug-video   File: CompositorViewHolderBehavior.java   Source Code and License Vote up 5 votes
@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 7
Project: chromium-for-android-56-debug-video   File: LayoutManager.java   Source Code and License Vote up 5 votes
@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 8
Project: iosched-reader   File: FractionalTouchDelegate.java   Source Code and License Vote up 5 votes
@Override
public boolean onTouchEvent(MotionEvent event) {
    updateSourcePartial();

    // The logic below is mostly copied from the parent class, since we
    // can't update private mBounds variable.

    // http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;
    // f=core/java/android/view/TouchDelegate.java;hb=eclair#l98

    final Rect sourcePartial = mSourcePartial;
    final View target = mTarget;

    int x = (int)event.getX();
    int y = (int)event.getY();

    boolean sendToDelegate = false;
    boolean hit = true;
    boolean handled = false;

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (sourcePartial.contains(x, y)) {
            mDelegateTargeted = true;
            sendToDelegate = true;
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_MOVE:
        sendToDelegate = mDelegateTargeted;
        if (sendToDelegate) {
            if (!sourcePartial.contains(x, y)) {
                hit = false;
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        sendToDelegate = mDelegateTargeted;
        mDelegateTargeted = false;
        break;
    }

    if (sendToDelegate) {
        if (hit) {
            event.setLocation(target.getWidth() / 2, target.getHeight() / 2);
        } else {
            event.setLocation(-1, -1);
        }
        handled = target.dispatchTouchEvent(event);
    }
    return handled;
}
 
Example 9
Project: NeiHanDuanZiTV   File: BGAGuideLinkageLayout.java   Source Code and License Vote up 5 votes
@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 10
Project: KUtils-master   File: BGAGuideLinkageLayout.java   Source Code and License Vote up 5 votes
@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 11
Project: VerticalViewPager   File: VerticalVPOnTouchListener.java   Source Code and License Vote up 2 votes
/**
 * fake a ACTION_DOWN to adjust child's motion track
 *
 * @param v
 * @param e
 */
private void adjustDownMotion(View v, MotionEvent e) {
    MotionEvent fakeDownEvent = MotionEvent.obtain(e);
    fakeDownEvent.setAction(MotionEvent.ACTION_DOWN);
    v.dispatchTouchEvent(fakeDownEvent);
}