I am using a webview wrapped in SwipeRefreshLayout. If the site is long enough, when scrolling up, SwipeRefreshLayout is triggered and the page is updated. Simply put - I can not scroll the page up.
The webView.getScrollY () method always returns 0, most likely because the site Header is fixed.
The same site in Google Chrome works great. How to make it possible to refresh the page only if the user is located at the beginning of the site?
activity_main
<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"> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipeLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent"> </WebView> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout> MainActivity
public class MainActivity extends AppCompatActivity { private WebView webView; private SwipeRefreshLayout swipeLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initResources(); initWebSettings(); swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { if (webView.getUrl() != null) { webView.reload(); } swipeLayout.setRefreshing(false); } }); webView.getViewTreeObserver(). addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { if (webView.getScrollY() == 0) { swipeLayout.setEnabled(true); } else { swipeLayout.setEnabled(false); } } }); } private void initResources() { webView = findViewById(R.id.webView); swipeLayout = findViewById(R.id.swipeLayout); } private void initWebSettings() { WebSettings webSettings = webView.getSettings(); webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); webSettings.setBuiltInZoomControls(true); webSettings.setDisplayZoomControls(false); webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING); webSettings.setMinimumFontSize(1); webSettings.setJavaScriptEnabled(true); // File Access webSettings.setAllowFileAccess(true); // Storage webSettings.setDomStorageEnabled(true); webSettings.setSupportMultipleWindows(true); webSettings.setUserAgentString(BuildConfig.USER_AGENT); webView.setWebChromeClient(new WebChromeClient()); webView.setWebViewClient(new WebViewClient()); webView.loadUrl(BuildConfig.SITE_ADDRESS); } }