Header Ads Widget

Ticker

6/recent/ticker-posts

SwipeRefreshLayout to refresh WebView

Suppose you have an app which contains a WebView webview1 inside a LinearV linear1. In onCreate it loads a website and onBackPressed it goes to previous page.

To implement SwipeRefreshLayout in this app to reload the WebView, follow the steps given below.

1. Swith on AppCompat and design.

2. Create a more block extra and put codes to declare a SwipeRefreshLayout srl.
Copy code
}
androidx.swiperefreshlayout.widget.SwipeRefreshLayout srl;
{


3. In onCreate event, before loading url in WebView;
* define a new SwipeRefreshLayout srl,
* remove all views from linear1,
* add SwipeRefreshLayout to linear1,
* add WebView to SwipeRefreshLayout,
* setOnRefreshListener for the SwipeRefreshLayout,
* In onRefresh event of OnRefreshListener, load WebView getUrl in WebView using blocks.

Copy code
srl = new androidx.swiperefreshlayout.widget.SwipeRefreshLayout(this);

linear1.removeAllViews();
linear1.addView(srl);
srl.addView(webview1);

srl.setOnRefreshListener( new androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener() {
@Override public void onRefresh() {

// PUT BLOCK TO REFRESH WEBVIEW HERE

}});

4. Add WebView onPageFinished event. Here, to stop the SwipeRefreshLayout, use setRefreshing(false).


Copy code
srl.setRefreshing(false);

5. Save and run the project.

Yorum Gönder

0 Yorumlar