Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dragging (when zoomed) in ViewPager not working as expected #25

Open
peshkira opened this issue Jul 5, 2016 · 11 comments
Open

Dragging (when zoomed) in ViewPager not working as expected #25

peshkira opened this issue Jul 5, 2016 · 11 comments

Comments

@peshkira
Copy link

peshkira commented Jul 5, 2016

I have a few pdfs displayed in a view pager. When I zoom one and try to drag to the left/right, the pdf does not pan, but the view pager immediately takes hold of the scrolling and switches to the next page.

If I first drag up/down and then (without lifting my finger) drag left/right, it seems to work as expected.

Any ideas, how I can pass the touch events first to the PDFView and then to the view pager?

@dabele91
Copy link

dabele91 commented Jul 8, 2016

Same problem here. Would be nice if someone has an idea how to resolve this issue.

@MoQ93
Copy link

MoQ93 commented Jul 15, 2016

That's because canScrollHorizontally(int direction) is not implemented, which is already requested in issue #19

@paperrose
Copy link

Had the same problem, but fixed it with overriding VP canScroll method

@dabele91
Copy link

dabele91 commented Oct 4, 2016

@paperrose can you write an example how you've implemented the canScroll functionality?

@paperrose
Copy link

@dabele91

public class PDFViewPager extends ViewPager {

    public PDFViewPager(Context context) {
        super(context);

    }

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

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        if (v instanceof ViewGroup) {
            final ViewGroup group = (ViewGroup) v;
            final int scrollX = v.getScrollX();
            final int scrollY = v.getScrollY();
            final int count = group.getChildCount();
            for (int i = count - 1; i >= 0; i--) {
                final View child = group.getChildAt(i);
                if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
                        y + scrollY >= child.getTop() && y + scrollY < child.getBottom()) {
                    if ((child instanceof PDFView && isNotHorizontalBounds((PDFView)child, dx)) || canScroll(child, true, dx, x + scrollX - child.getLeft(),
                            y + scrollY - child.getTop()))
                        return true;
                }
            }
        }

        return checkV && false;
    }

    private boolean isNotHorizontalBounds(PDFView view, int dx) {
        final float zoom = view.getZoom();
        final float pageWidth = view.getWidth()*zoom;
        final float offset = view.getCurrentXOffset();
        if (offset + pageWidth <= getWidthScreen() + 1 && dx < 0) return false;
        if (offset >= -1 && dx > 0) return false;
        return true;
    }

    private int getWidthScreen(){
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        return size.x;
    }
} 

@dabele91
Copy link

@paperrose works lke a charm. thanks ;-)

@hustyichi
Copy link

Thank you , it works

@paperrose
Copy link

Just find a small bug in my code above. In landscape orientation right-to-left swipe doesn't change page
Solved by replacing
final float pageWidth = view.getWidth()*zoom;
to
final float pageWidth = view.getOptimalPageWidth()*zoom;

@MonaHammad
Copy link

How to use this approach, I can't find ViewPager class?

@peshkira
Copy link
Author

peshkira commented Dec 5, 2016

Hi @MonaHammad!

The ViewPager is part of the android support library: https://developer.android.com/reference/android/support/v4/view/ViewPager.html

It is not related to this PDF library. This issue here is relevant if you show multiple PDFs inside a view pager. Hope this helps.

best

@MonaHammad
Copy link

MonaHammad commented Dec 5, 2016

Hi @peshkira
Ok, thanks.
But how to use it here to improve the paging while zooming in this library?
I couldn't figure out how to use PDFViewPager?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants