我试图设置主要显示progress bar
和a的片段
webview
,一旦网站完全加载,progress bar
就会消失。我关注此代码:
https://stackoverflow.com/a/8467430/7803533
问题是从不使用OnProgressChanged方法:
public class HUB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_hub, container, false);
final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.pBar);
WebView mWebView = (WebView) rootView.findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient(){
public void onProgressChanged(WebView view, int progress){
if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE){
progressBar.setVisibility(ProgressBar.VISIBLE);
}
progressBar.setProgress(progress);
if(progress == 100){
progressBar.setVisibility(ProgressBar.GONE);
}
}
});
mWebView.clearCache(true);
mWebView.clearHistory();
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadUrl("https:10.10.1.40/index.html");
return rootView;
}
}
布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sykes.moodleapp.HUB">
<!-- TODO: Update blank fragment layout -->
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pBar"
style="?android:attr/progressBarStyleHorizontal"
android:padding="2dip"/>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"
></WebView>
</FrameLayout>
答案 0 :(得分:0)
通过添加以下行,将您的进度条可见性更改为xml中的GONE
:
android:visibility="gone"
因为如果进度小于100且可见性为“View.GONE:
,那么此条件if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE)
的代码只会导致true
,否则强>
在您获得进度条的引用之后,在其旁边添加一行,View
为GONE
这样:
final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.pBar);
progressBar.setVisibility(View.GONE); //Add this line
答案 1 :(得分:0)
为了将来参考,WebViewClient没有onProgressChanged()
方法,WebChromeClient会这样做。
我最终都使用它们,因为我希望能够使用onReceivedSslError()
显示SSL错误。
public class HUB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_hub, container, false);
final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.pBar);
WebView mWebView = (WebView) rootView.findViewById(R.id.webview);
mWebView.setWebChromeClient(new WebChromeClient(){
// SSL error handler
});
mWebView.setWebViewClient(new WebViewClient(){
public void onProgressChanged(WebView view, int progress){
if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE){
progressBar.setVisibility(ProgressBar.VISIBLE);
}
progressBar.setProgress(progress);
if(progress == 100){
progressBar.setVisibility(ProgressBar.GONE);
}
}
});
mWebView.clearCache(true);
mWebView.clearHistory();
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadUrl("https:10.10.1.40/index.html");
return rootView;
}