从未使用过 OnProgressChanged
OnProgressChanged is never used
我正在尝试设置一个主要显示 progress bar
和
webview
,一旦网站完全加载,progress bar
应该会消失。我正在遵循此代码:
问题是从未使用过 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>
通过添加以下行将 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
供将来参考,WebViewClient doesn't have a onProgressChanged()
method, 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;
}
我正在尝试设置一个主要显示 progress bar
和
webview
,一旦网站完全加载,progress bar
应该会消失。我正在遵循此代码:
问题是从未使用过 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>
通过添加以下行将 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
供将来参考,WebViewClient doesn't have a onProgressChanged()
method, 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;
}