0

When I scroll up in my webview app, it triggers a Swipe-to-Refresh refresh- How can I make the refresh to trigger only when reaching the top of the page? I saw this post about this exact problem, but I couldn't understand where to insert the code If you answer please be specific about how I implement the code and where Thank you

Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29
Shay S
  • 41
  • 3
  • Add `addOnScrollChangedListener` in `onStart` then `removeOnScrollChangedListener` in `onStop`. If you want more specific answer post your code where you used WebView – Varad Mondkar Oct 18 '19 at 19:13
  • Possible duplicate of [SwipeRefreshLayout Tab layout. Webview can't scroll up](https://stackoverflow.com/questions/24923021/swiperefreshlayout-tab-layout-webview-cant-scroll-up) – Mohamed AbdelraZek Oct 18 '19 at 19:16
  • @MohamedAbdelraZek Yes that's correct, I mentioned this post in the description – Shay S Oct 18 '19 at 19:30

1 Answers1

0

If you're using WebView only,

you may have to use these layout in the provided order,

  1. SwipeRefreshLayout 1.1 NestedScrollView 1.1.1 WebView
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

   <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <WebView
        android:id="@+id/webview1"
        android:layout_width="598dp"
        android:layout_height="1022dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    </androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

And in your Activity

public class WebView1 extends AppCompatActivity {

    SwipeRefreshLayout swipeRefreshLayout;
    WebView browser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        browser = (WebView) findViewById(R.id.webview1);
        swipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
        browser.setWebViewClient(new WebViewClient());browser.getSettings().setJavaScriptEnabled(true);


        browser.loadUrl("http://www.google.com");

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                browser.reload();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }
}

If you're copying this code, please note that I'm importing androidx libraries

To make it to support libraries, consider the following

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout/>
<androidx.nesv4.widget.NestedScrollView/>

to

<android.support.v4.widget.SwipeRefreshLayout/>
<android.support.v4.widget.NestedScrollView/>
Community
  • 1
  • 1