Disabling Vertical View Pager when image is zoomed inTouchImageView

886 views Asked by At

I am using VerticalViewPagerand TouchImageView for image zoom in/out. When i zoom in on the image and try to drag the image down (to view the bottom part of image) the view pager swipes to second image. Any help would be appreciate. I need to disable the viewpager i tried to change the canScroll function of vertical view pager but it didnt help.

Here is the code that i modified:

protected boolean canScroll(View v, boolean checkV, int dy, int x, int y) {
   if(v instanceof TouchImageView)
   {
       Log.d("Here","Here");
       if(((TouchImageView) v).getCurrentZoom()!=1)
       {
           Log.d("Here","HereAgain");

           return false;
       }
   }

   else  if (v instanceof ViewGroup) {
        final ViewGroup group = (ViewGroup) v;
        final int scrollX = v.getScrollX();
        final int scrollY = v.getScrollY();
        final int count = group.getChildCount();
        // Count backwards - let topmost views consume scroll distance first.
        for (int i = count - 1; i >= 0; i--) {
            // TODO: Add versioned support here for transformed views.
            // This will not work for transformed views in Honeycomb+
            final View child = group.getChildAt(i);
            if (y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
                    x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
                    canScroll(child, true, dy, x + scrollX - child.getLeft(),
                            y + scrollY - child.getTop())) {
                return true;
            }
        }

       return checkV && ViewCompat.canScrollVertically(v, -dy);
    }

   return false;
}
2

There are 2 answers

6
Muhammed Refaat On BEST ANSWER

To solve that, create a custom VerticalViewPager class which overrides the scroll events (onTouchEvent , onInterceptTouchEvent), then return from it true or false depending on if you want to scroll or not, and that will achieved through a predefined Boolean variable:

import android.content.Context;
import android.view.MotionEvent;

public class CustomizongVVP extends VerticalViewPager {

    public boolean scroll = true;

    public CustomizongVVP(Context context) {
        super(context);
    }

    public CustomizongVVP(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return scroll && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return scroll && super.onInterceptTouchEvent(event);
    }

    public void setScroll(boolean scroll) {
        this.scroll = scroll;
    }

}

and to define :

CustomizongVVP custom_vvp = new CustomizongVVP(this);
custom_vvp.setScroll(true); // to allow scrolling
custom_vvp.setScroll(false); // to disable scrolling

So, you can toggle anytime between true and false to allow and disable scrolling using custom_vvp.setScroll() method.

0
Paul On

Another approach is to subclass TouchImageView and override the canScrollVertically method, like this:

@Override
public boolean canScrollVertically(int direction) {
    matrix.getValues(m);
    float y = m[Matrix.MTRANS_Y];

    if (getImageHeight() < viewHeight) {
        return false;

    } else if (y >= -1 && direction < 0) {
        return false;

    } else if (Math.abs(y) + viewHeight + 1 >= getImageHeight() && direction > 0) {
        return false;
    }

    return true;
}

If the image is zoomed out then swiping changes pages. If the image is zoomed in then they can pan around the image without changing pages until they hit the top or bottom of the zoomed image, at which point the next swipe will change the page.

Hope that's helpful