androidandroid-viewpagertouchimageview

Disabling Vertical View Pager when image is zoomed inTouchImageView


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;
}

Solution

  • 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.