Android webview上面的进度条不停
The progress bar above the Android webview does not stop
进度条不会停止,会无限期加载。
Referred Page
:
片段代码
public class testFragment extends Fragment {
private WebView mWebView;
private WebSettings mWebSettings;
private ProgressBar progressBar;
public testFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@SuppressLint("SetJavaScriptEnabled")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.testfragment, container, false);
mWebView = (WebView) v.findViewById(R.id.webView);
progressBar = (ProgressBar) v.findViewById(R.id.pro);
//progressBar.setVisibility(View.GONE);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.setWebViewClient(new WebViewClient());
mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setSupportMultipleWindows(false);
mWebSettings.setJavaScriptCanOpenWindowsAutomatically(false);
mWebSettings.setLoadWithOverviewMode(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setSupportZoom(false);
mWebSettings.setBuiltInZoomControls(true);
mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebSettings.setDomStorageEnabled(true);
mWebView.loadUrl("https://whosebug.com/");
return v;
}
}
去掉注释中的注释,进度条就会完全消失
我也想在引用的页面上使用@Override public void onProgressChanged (WebView view, int newProgress)
,但它没有出现在可用的方法中。
问题是,
您已设置 2 web clients
并且最后一条语句覆盖了第一条语句。所以 visible & hide 从未被执行过。
由于在 UI 中,您已将进度条的可见性设置为可见。因此它始终可见。
删除 mWebView.setWebViewClient(new WebViewClient());
行。
对于onProgressChanged()
你必须使用 WebChromeClient
。由于在 Chrome 客户端中可用,在 WebViewClient
中不可用
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
});
进度条不会停止,会无限期加载。
Referred Page
:
片段代码
public class testFragment extends Fragment {
private WebView mWebView;
private WebSettings mWebSettings;
private ProgressBar progressBar;
public testFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@SuppressLint("SetJavaScriptEnabled")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.testfragment, container, false);
mWebView = (WebView) v.findViewById(R.id.webView);
progressBar = (ProgressBar) v.findViewById(R.id.pro);
//progressBar.setVisibility(View.GONE);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.setWebViewClient(new WebViewClient());
mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setSupportMultipleWindows(false);
mWebSettings.setJavaScriptCanOpenWindowsAutomatically(false);
mWebSettings.setLoadWithOverviewMode(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setSupportZoom(false);
mWebSettings.setBuiltInZoomControls(true);
mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebSettings.setDomStorageEnabled(true);
mWebView.loadUrl("https://whosebug.com/");
return v;
}
}
去掉注释中的注释,进度条就会完全消失
我也想在引用的页面上使用@Override public void onProgressChanged (WebView view, int newProgress)
,但它没有出现在可用的方法中。
问题是,
您已设置 2 web clients
并且最后一条语句覆盖了第一条语句。所以 visible & hide 从未被执行过。
由于在 UI 中,您已将进度条的可见性设置为可见。因此它始终可见。
删除 mWebView.setWebViewClient(new WebViewClient());
行。
对于onProgressChanged()
你必须使用 WebChromeClient
。由于在 Chrome 客户端中可用,在 WebViewClient
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
});