Search code examples
androidtouch-eventmotionevent

Android MotionEvent : Transfer between views


I have two custom viewgroups that implements onTouchListener for motionevent.

I am using a framelayout to show them both. Second viewgroup is smaller in size than the first one. So the first one is in background and second one is foreground.

I want to drag item for second/smaller viewgroup to the background viewgroup.

Problem : When dragging the item to the background viewgroup (i.e mAwesomePager), I want ACTION_UP to be triggered on the second viewgroup (smaller one) and ACTION_MOVE to be triggered on the first viewgroup, so basically the touchEvent is transferred from smaller viewgroup to the larger one in the background and the MotionEvent continues without the user has to take up the finger from the screen and then put it back again.

Here is some of the useful code :

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mRectOut == true) {
        ev.setAction(MotionEvent.ACTION_UP);
        mFrame.bringChildToFront(mAwesomePager);
        //mAwesomePager.setClickable(true);

        // Obtain MotionEvent object
        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis() + 100;
        // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
        int metaState = 0;
        MotionEvent motionEvent = MotionEvent.obtain(
                downTime,
                eventTime,
                MotionEvent.ACTION_DOWN,
                ev.getX(),
                ev.getY(),
                metaState
        );
        mAwesomePager.getChildAt(mAwesomePager.mLastDragged).dispatchTouchEvent(motionEvent);
    }
//some more code here

}

I am trying to simulate touch off in the foreground view by this ev.setAction(MotionEvent.ACTION_UP); , now I want the background view to take control over the touch while the touch is holding the dragged image.


Solution

  • Changing this mAwesomePager.getChildAt(mAwesomePager.mLastDragged).dispatchTouchEvent(motionEvent);

    to this

    return mAwesomePager.dispatchTouchEvent(motionEvent);

    eventually did the trick.