Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager with WebView allowing swiping and disallowing scrolling of WebView

I am using ViewPager and WebViewto present HTML contents as an eBook. On some pages of the ViewPager I am allowing the TouchEvent to be handled by the WebView and on some pages I am letting the ViewPager handle the TouchEvent by itself by overriding the onInterceptTouchEvent method in the ViewPager(some pages are having TextViews and Buttons without WebView 1st and last page here). Here is code for the same-

@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (getCurrentItem() == 0 || getCurrentItem() == getAdapter().getCount() - 1)
            return false;
        else {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                return false;
            }
            return true;
        }
    }

I am again disallowing the scrolling by setting OnTouchListener on the WebView using the following code-

contentView.setOnTouchListener((v, event) -> {
            if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_MOVE) {
                ((BigBeeViewPager) v.getParent().getParent()).onInterceptTouchEvent(event);
            }
            return true;
        });

Problem I am facing is - When I am trying to swipe the ViewPager from the middle of the pages it doesn't work. I have to start swiping from the end of the pages. However it works fine for the pages which are not having WebView How to allow it to start swiping from the anywhere in the pages.

This behaviour is similar to Gmail Android App where ViewPager is containing the WebView and Swiping is available from any where. I am not able to figure out how they have done it.

Note - When I am intercepting all TouchEvent to the WebView it disables the long click and text selection too, which is not desirable.

Thanks in advance for the help.

like image 406
Sanjeet A Avatar asked Mar 01 '17 08:03

Sanjeet A


1 Answers

Here you go man - an old problem with a nice solution.

Take a look here Scroll webview horizontally inside a ViewPager

like image 81
MarkySmarky Avatar answered Nov 03 '22 15:11

MarkySmarky