2
votes

I am working with the ViewPager trying to accomplish some animations. One of them is trying to slide from left to right (default transition of view pager is from right to left). I have done that.

My problem is that I want to "hack" the touch event so I don't need to modify the view pager. For example, for the left to right transition, I will want to make some kind of mirroring with the X in the touch event passed to the view pager.

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    event.setX(Math.abs(event.getX() - getWidth());
    return super.onInterceptTouchEvent(event);
}
2
look at pagetransformer.bogdan
Yes, I have done the transition well. Now I want it to make sense with the touch move. I don't want a transition left to right when I make a touch move from right to left.Juangcg

2 Answers

5
votes

I finally did it.

public boolean dispatchTouchEvent(MotionEvent event) {
    MotionEvent hackedEvent = MotionEvent.obtain(event.getDownTime(),
            event.getEventTime(), event.getAction(), (event.getX() - getWidth()) * -1,
            event.getY(), event.getMetaState());
    boolean result = super.dispatchTouchEvent(hackedEvent);
    hackedEvent.recycle();
    return result;
};
0
votes

For people with the same issue and using Kotlin:

I created an extension function to copy the whole MotionEvent in data class style, which allows you to modify certain attributes of it.

E.g.

val modifiedEvent = event.copy(downTime = System.currentTimeMillis())
event.recycle()

Extension function:

/**
 * Copies a whole MotionEvent. Use the named parameters to modify certain 
values.
 * Don't forget to recycle the original event (if it is not used anymore :) 
)!
 */
fun MotionEvent.copy(
    downTime: Long = getDownTime(),
    eventTime: Long = getEventTime(),
    action: Int = getAction(),
    pointerCount: Int = getPointerCount(),
    pointerProperties: Array<MotionEvent.PointerProperties>? =
        (0 until getPointerCount())
            .map { index ->
                MotionEvent.PointerProperties().also { pointerProperties ->
                    getPointerProperties(index, pointerProperties)
                }
            }
            .toTypedArray(),
    pointerCoords: Array<MotionEvent.PointerCoords>? =
        (0 until getPointerCount())
            .map { index ->
                MotionEvent.PointerCoords().also { pointerCoords ->
                    getPointerCoords(index, pointerCoords)
                }
            }
            .toTypedArray(),
    metaState: Int = getMetaState(),
    buttonState: Int = getButtonState(),
    xPrecision: Float = getXPrecision(),
    yPrecision: Float = getYPrecision(),
    deviceId: Int = getDeviceId(),
    edgeFlags: Int = getEdgeFlags(),
    source: Int = getSource(),
    flags: Int = getFlags()
): MotionEvent =
    MotionEvent.obtain(
        downTime,
        eventTime,
        action,
        pointerCount,
        pointerProperties,
        pointerCoords,
        metaState,
        buttonState,
        xPrecision,
        yPrecision,
        deviceId,
        edgeFlags,
        source,
        flags
    )

Source: https://gist.github.com/sebschaef/b803da53217c88e8c691aeed08602193