Pavithra
Pavithra

Reputation: 43

Disable paging in ViewPager when a ImageView is zoomed

I have used ViewPager for image sliding and I need to view the images with zoom option ,the image gets zoomed but the paging interrupts while zooming the image.Have tried onTouchEvent() and OnInterceptTouchEvent().Disabling and Enabling ViewPager results in Force close.

Upvotes: 3

Views: 2394

Answers (4)

Jorge Alfaro
Jorge Alfaro

Reputation: 924

I know this questions is old but for somebody else looking for an answer Mike Ortiz TouchImageView works out of the box, (you may need to switch to the dev branch)

Upvotes: 2

Damir Halilović
Damir Halilović

Reputation: 172

I did this with the following way, it will disable paging when the user zooms, but when he hits the edge of the zoomed imageview it will properly page onto the next / previous image.

 @Override 
        public boolean onTouchEvent(MotionEvent ev) {
            if(handleTouch(ev)) {
                return super.onTouchEvent(ev);
            }
            else {
                return false;
            }

        }

        @Override 
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if(handleTouch(ev) && !areCommentsOpen()) {
                return super.onInterceptTouchEvent(ev);
            }
            else {
                return false;
            }
        }

        private boolean handleTouch(MotionEvent ev) {
            if (prevX != -666 && ev.getAction() == MotionEvent.ACTION_MOVE) {
                if(ev.getX() <= prevX) {
                    isGoingLeft=false;
                }
                else {
                    isGoingLeft=true;
                }
            }
            else if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
                return true;
            }

            ZoomableImageView imageView = getImageView();
            if(imageView == null) {
                return true;
            }
            prevX=ev.getX();
            if(imageView.getZoomLevel() <= 1f) {
                return true;
            }

            else if(imageView.isBoundedLeft()) {
                if(isTransitioning) {
                    return true;
                }
                else if (isGoingLeft) {
                    isTransitioning = true;
                    return true;
                }
                else {
                    return false;
                }
            }

            else if (imageView.isBoundedRight()) {
                if(isTransitioning) {
                    return true;
                }
                else if (!isGoingLeft) {
                    isTransitioning = true;
                    return true;
                }
                else {
                    return false;
                }
            }

            else {
                isTransitioning = false;
                return false;
            }
        }

private ZoomableImageView getImageView() {
        for (int i = 0; i < getChildCount(); i++) {
            ZoomableImageView imageView = (ZoomableImageView) (getChildAt(i).findViewById(R.id.single_image_view));
            if(imageView.getPosition() == getCurrentItem()) {
                return imageView;
            }
        }
        return null;
    }

    public void setTransitioning(boolean trans) {
        this.isTransitioning = trans;
    }

    public void setGoingLeft(boolean left) {
        this.isGoingLeft = left;
    }

Upvotes: 0

Spring Breaker
Spring Breaker

Reputation: 8251

I was also facing the same problem once and I did the following to solve this.I used a custom ViewPager class.

CustomViewPager .java

public class CustomViewPager extends ViewPager {

    private boolean isPagingEnabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.isPagingEnabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.isPagingEnabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.isPagingEnabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }
}

and to disable scrolling use the following snippet,

CustomViewPager pager = (CustomViewPager) findViewById(R.id.pager);
pager.setPagingEnabled(false);

If you need more info then you can ask. Hope it helps.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006759

Subclass ViewPager and override canScroll(). You will be passed the widget over which the touch events were detected, and you can return true if you want the scroll event to passed to that widget or false if you want the ViewPager to use that scroll amount. In your case, you would employ your own custom logic when you are zoomed and simply chain to the superclasss when you are not zoomed.

Upvotes: 2

Related Questions