您好,过去 2 天一直在调查这个问题。我在 SwipeRefreshLayout 中有一个 Web View ,它几乎适用于每个站点,但是当尝试使用 maps.google.com 时,我遇到了这种奇怪的情况,滚动 map 会触发 SwipeRefresh,并且页面会继续重新加载 ()。
非常感谢在这方面的任何帮助。示例代码如下。
class MainActivity : AppCompatActivity() {
lateinit var browser: WebView
lateinit var swipeLayout : SwipeRefreshLayout
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
browser = findViewById<View>(R.id.webview) as WebView
swipeLayout = findViewById<View>(R.id.swipeRefreshLayout) as SwipeRefreshLayout
browser.settings.loadsImagesAutomatically = true;
browser.settings.javaScriptEnabled = true;
browser.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY;
browser.loadUrl("https://www.google.com/maps");
swipeLayout.setOnRefreshListener {
browser.reload()
swipeLayout.isRefreshing=false
}
browser.webViewClient = (object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
Log.d("Browser", browser.scrollY.toString())
}
})
}
布局文件是这样的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
问题的gif附在下面
预期结果:向下滚动应该只会移动 map
实际结果:触发 SwipeToRefresh,导致网页刷新。
最佳答案
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
Log.d(TAG, "onProgressChanged: " + view.getUrl());
//here check the condition
// if url contains "map" then set swipe layout to setEnabled(false)
// else setEnabled(true)
swipelayout.setEnabled(!BASE_URL.concat("my-location").equals(view.getUrl()) && !view.getUrl().contains("running-order"));
}
关于Android - 滚动或平移谷歌地图等网站时刷新 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67175033/