10

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.

Sanjeet A
  • 5,171
  • 3
  • 23
  • 40
  • Can content displayed inside `WebView` be zoomed and scrolled ? Or scrolling behavior should be applicable only to the `ViewPager`? – j2ko Mar 02 '17 at 00:19
  • Scrolling should be enabled with 'ViewPager' only. However zooming and other behaviour like - long click for text selection should be enabled with 'WebView'. – Sanjeet A Mar 02 '17 at 16:58
  • @Sanjeet Ajnabee Please share the project with demo data. Atleast with viewpager content and webview? – Anurag Singh Mar 06 '17 at 07:57
  • add proper screenshots so that we can understand your problem easily and help. – Maveňツ Mar 10 '17 at 09:55

3 Answers3

6

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

Take a look here Scroll webview horizontally inside a ViewPager

Community
  • 1
  • 1
MarkySmarky
  • 1,609
  • 14
  • 17
  • Finally worked. Its was damn easy . Thank you so much! – Sanjeet A Mar 11 '17 at 06:43
  • Please add the answer part from the referred link so other can get answers, Basically the section where allowing /disallowing the scroll to the child views solves the problem! – Sanjeet A Mar 11 '17 at 06:46
1

However MarkySmarky is extremely helpful to find out the solution but still I will like to write the the answer part so other people can get help.

I solved the problem by overriding the method

protected boolean canScroll(View v, boolean checkV, int dx, int x, int y)

of ViewPager and returning false if v is the instance of the WebView. This is not letting the WebView scroll itself and hence ViewPager treat it as a nono scrolling child and hence swiping works from the anywhere of the page.

Finally my ViewPager is looking like-

public class BigBeeViewPager extends ViewPager {

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

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

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        return !(v instanceof ObservableWebView) && super.canScroll(v, checkV, dx, x, y);
    }
}
Community
  • 1
  • 1
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40
0

You have create a GestureDetector and implement that on a Webviewlike this:

GestureDetector gestureDetector=new GestureDetector(new GestureDetector.OnGestureListener() {
        @Override
        public boolean onDown(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent motionEvent) {
        }

        @Override
        public boolean onSingleTapUp(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent motionEvent) {
        }

        @Override
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            return false;
        }
    });

          webView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                        case MotionEvent.ACTION_UP:
                            if (!v.hasFocus()) {
                                v.requestFocus();
                            }
                            break;
                    }
                    gestureDetector.onTouchEvent(event);
                    return false;
                }
            });

And remove the Interceptor from Viewpager and use the default one without any changes

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51