Swiperefreshlayout + Webview When Scroll Position Is at Top

Android - WebView refreshing when scrolling down

Add SwipeRefreshLayoutListener i.e. setOnRefreshListener like below:

 refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshLayout.setRefreshing(true);
loadWebView();
}
});

Whole Class with SwipeRefreshLayout Layout :

public class WebViewActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();

private WebSettings webSettings;
private View view3;
private ProgressBar loadingBar;
private SwipeRefreshLayout refreshLayout;
private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);

view3 = findViewById(R.id.view3);
loadingBar = findViewById(R.id.loadingBar);
refreshLayout = findViewById(R.id.swipeContainer);
webview = findViewById(R.id.webview);

webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSaveFormData(true);
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
loadWebView();

refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshLayout.setRefreshing(true);
loadWebView();
}
});

}
private void loadWebView(){
String urlLink="https://stackoverflow.com/questions/57478675/android-webview-refreshing-when-scrolling-down";
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
refreshLayout.setRefreshing(false);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
});
webview.setWebChromeClient(new WebChromeClient());
webview.clearCache(true);
webview.loadUrl(urlLink);
}
}

I hope its work for you.

Swipe to refresh is called out too soon in webview

setDistanceToTriggerSync(int distance) 

pretty simple.

WebView getScrollY() always returns 0

The site you linked has a fixed header. My guess is that the page itself doesn't scroll; a container inside it does. The WebView can't inspect every scrollable container on the page, so it sees that the top-level container doesn't scroll and assumes that the entire thing is fixed.

If all you need this for is pull-to-refresh, I'd recommend adding a refresh button in addition to the SwipeRefreshLayout.



Related Topics



Leave a reply



Submit