Android, FragmentActivity and prevent Swipe

Posted by FIG-GHD742 on Stack Overflow See other posts from Stack Overflow or by FIG-GHD742
Published on 2012-10-16T15:50:08Z Indexed on 2012/10/16 17:01 UTC
Read the original article Hit count: 412

I use android.support.v4.app.FragmentActivity for create a app with multi fragment/panels that can be access by drag/swipe between different part of the app. In one of my fragment I has a zoomable view and my problem is in case I is on the zoomable view I will prevent the use for drag/swipe to a other fragment.

I has try to hack into android.support.v4.view.ViewPager for get the action from on Touch event but not work.

I has try all of this case but not work: (All code is a a part of subclass to android.support.v4.view.ViewPager)

Case 1:

// Not working
@Override
protected void onPageScrolled(int position, float offset, int offsetPixels) {
    if (isPreventDrag()) {
        super.onPageScrolled(position, 1, 0);
    } else {
        super.onPageScrolled(position, offset, offsetPixels);
    }
}

Case 2:

// Work but stop all event include the event to the target image view.
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            lastX = ev.getX(); // float 
            lockScroll = false;
            return super.onInterceptTouchEvent(ev);
        case MotionEvent.ACTION_MOVE:
            this.lockScroll = this.isPreventDrag();
            break;
    }
    if (lockScroll) {
        ev.setLocation(lastX, ev.getY());
        return super.onInterceptTouchEvent(ev);
    } else {
        return super.onInterceptTouchEvent(ev);
    }
}

Case 3:

// Work good, but by some unknown error I can drag the screen
// some pixels before this stop the event.
@Override
public boolean onTouchEvent(MotionEvent ev) {

    if (this.isPreventDrag()) {
        return true;
    } else {
        return super.onTouchEvent(ev);
    }
}

I want a easy way to deactivate stop or deactivate if the use is allow to switch to a other Fragment.

Here is a working code for me, I don't know what error I do before.

// This work for me,
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (this.isPreventDrag()) {
        return false;
    } else {
        return super.onInterceptTouchEvent(ev);
    }
}

© Stack Overflow or respective owner

Related posts about android

Related posts about android-fragments