android SwipeRefreshLayout doesn't allow WebView scrolling

1.1k views Asked by At

Am trying to implement android SwipeRefreshLayout in my WebView app. When I swipe down it refreshes the page but it doesn't allow to scroll up and down anymore except the swipe.

I have tried some basic fix I found online but it still not working.

private WebView mywebview, handshakemywebview;
private SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mywebview = findViewById(R.id.MainWebInterface);
    handshakemywebview = findViewById(R.id.HandShakeWebInterface);
    mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);

    mywebview.loadUrl("https://example.com");
    mySwipeRefreshLayout.setOnRefreshListener(
       new SwipeRefreshLayout.OnRefreshListener() {
          @Override
          public void onRefresh() {
             shopwebview.reload();
          }
       }
    );
}



@Override
protected  void onRestart(){
    super.onRestart();
    mySwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
       @Override
       public void onScrollChanged() {
          if (mywebview.getScrollY() == 0) {
             mySwipeRefreshLayout.setEnabled(true);
          } else {
            mySwipeRefreshLayout.setEnabled(false);
          }
       }
    });
}

@Override
protected void onPause(){
   super.onPause();
  mySwipeRefreshLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
}

Layout XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@color/colorPrimary"
            android:contentDescription="@string/activityLabel"
            android:visibility="visible"
            android:id="@+id/MainWebInterface"/>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <WebView
        android:id="@+id/HandShakeWebInterface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="@color/colorPrimary"
        android:visibility="gone" />

</RelativeLayout>
2

There are 2 answers

0
Chintan On

Add NestedScrollView in swiperefreshlayout. You could wrap webview in Swipe refesh layout like this

  <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipeContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <WebView
                    android:id="@+id/MainWebInterface"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:background="@color/colorPrimary"
                    android:contentDescription="@string/activityLabel"
                    android:visibility="visible" />

            </androidx.core.widget.NestedScrollView>
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
1
Akash Suresh Dandwate On

Write OnRestart code in onResume. Its because you are Adding addOnScrollChangedListener on Restart and onRestart is not called Always