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
Android:Textallcaps="False" Not Working for Tablayout Design Support
Error When Using Any Android Design Support Library Elements
Add Dynamic Text Over Android Seekbar Thumb
Changing Gradient Background Colors on Android at Runtime
Highlighting the Selected Item in the Listview in Android
How to Customize the Contextual Action Bar Using Appcompat in Material Design
How to Download a Video File to Sd Card
Android Emulator Screen Fills Up Only Some Fraction Part of Actual Android Emulator Screen
Tapping Form Field in Webview Does Not Show Soft Keyboard
Creating a Product Sdk: How to Add a Native Lib (.So) and a Jar with the Sdk I am Creating
Savedinstancestate Is Always Null in Fragment
Textbox Hidden Below Keyboard in Android Webview
How to Fill Color in Image in Particular Area
Mediametadataretriever Setdatasource Throws Illegalargumentexception
Start Google Hangout from Intent in New Hangouts Android App