我通过查看从互联网上找到的示例编写了示例webview代码。我想显示进度条,直到webview在我的片段中加载页面。
我的活动包含工具栏,显示片段和导航抽屉的框架。当我从导航抽屉中选择一个条目时,会显示包含进度条和webview的新片段,片段只是尝试加载下面的页面是片段的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_full_page, container, false);
//int view_num = getArguments().getInt(ARG_SECTION_NUMBER);
progressBar = (ProgressBar)rootView.findViewById(R.id.webviewProgress);
progressBar.setVisibility(View.VISIBLE);
webView = (WebView) rootView.findViewById(R.id.webView1);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
//webView.getSettings().setSupportZoom(true);
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
String urlString = getArguments().getString("URLSTRING");
final Boolean clickable = getArguments().getBoolean("CLICKABLE");
Boolean networkAvailable = Utils.isNetworkAvailable(super.getActivity().getApplicationContext());
if(networkAvailable == true) {
webView.loadUrl(urlString);
} else {
webView.loadUrl(Utils.DEFAULT_URL_PAGE);
}
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progressBar.setVisibility(View.VISIBLE);
if (clickable) {
view.loadUrl(url);
return false;
} else {
return true;
}
}
@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);
}
});
webView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
return rootView;
}
下面是fragment_full_page.xml
<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=".FragmentFullPage" >
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center|top"
android:id="@+id/webviewProgress"
/>
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
网页需要时间加载(尝试使用wifi和3g),但进度条没有显示任何时间。
为了测试进度条是否实际显示在fragment_full_page的中间,我删除了webview代码并保持进度条然后显示。
在加载webview时没有显示进度条的代码出错了,我无法弄明白。