0

I was opening a solution of Stack Overflow in my webview so mobile version of that page opens in the webview. Now there is one code which requires horizontal scrolling to view it completely. As I start scrolling, my webview start scrolling too. As a result, scrolling on webpage doesn't occur and viewPager scrolling occurs.

I tried this but when i implemented this my swiping was completely disabled. How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically? I have no idea how can I implement this.

<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <LinearLayout
        android:id="@+id/urlBoxId"
        android:background="@color/colorPrimaryDark"
        android:elevation="4dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/editTextId"
            android:layout_marginHorizontal="3dp"
            android:background="@drawable/rectangle"
            android:paddingHorizontal="10dp"
            android:paddingVertical="8.5dp"
            android:textColor="#fff"
            android:hint="EnterUrl"
            android:inputType="textUri"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="6"/>
    </LinearLayout>
    <WebView
        android:id="@+id/webViewId"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

This is my fragment Layout. I have a linearlayout above webview. I want to disable viewPager swiping when swiping on webview but able to swipe fragment when swiping done on linearLayout.

I am quite new to Android.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mayank Doda
  • 117
  • 2
  • 11
  • please see this : https://stackoverflow.com/questions/7774642/scroll-webview-horizontally-inside-a-viewpager – Tariqul Islam Jun 25 '19 at 12:39
  • Thanks @TariqulIslam . but do I have to extend my webview or not. Because as of now we have canScrollHorizontally(). However when I tried this method, It didn't work. Also will this method works for scrollView which are inside the webpages? – Mayank Doda Jun 25 '19 at 13:41
  • That solution @TariqulIslam is not working – Mayank Doda Jun 25 '19 at 14:54

1 Answers1

0

So I override onTouch method in setOnTouchListener of the webView.

// to access the parent activity
ViewPager vpager = getActivity().findViewById(R.id.myViewPager); 

webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

              if(event.getAction()==MotionEvent.ACTION_DOWN || 
                        event.getAction()==MotionEvent.ACTION_POINTER_DOWN){
                    vpager.requestDisallowInterceptTouchEvent(true);

              }else if(event.getAction()==MotionEvent.ACTION_UP ||
                        event.getAction()==MotionEvent.ACTION_POINTER_UP){
                    vpager.requestDisallowInterceptTouchEvent(false);

                }

                return false; // set it false for your webview to scroll correctly.
            }
        });
Mayank Doda
  • 117
  • 2
  • 11