这是我的Webview
片段。无论如何都要添加加载圈直到加载HTML
页面。
public class WebViewFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Retrieving the currently selected item number
int position = getArguments().getInt("position");
String url = getArguments().getString("url");
// List of rivers
String[] menus = getResources().getStringArray(R.array.menus);
// Creating view corresponding to the fragment
View v = inflater.inflate(R.layout.fragment_layout, container, false);
// Updating the action bar title
getActivity().getActionBar().setTitle(menus[position]);
//Initializing and loading url in webview
final WebView webView = (WebView)v.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/myerrorpage.html"); }
});
return v;
}
}
答案 0 :(得分:2)
您可以使用WebViewClient回调。有OnPageFinished()回调和许多其他有用的东西。
webview.setWebViewClient(new WebViewClient() {
public void public void onPageFinished (WebView view, String url) {
......some code here..
}
});
所以你需要做的就是在webview.loadUrl()方法之后立即显示进度,并在onPageFinished()回调方法中将其关闭。
答案 1 :(得分:0)
**main.xml**
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/framelayout"
android:orientation="vertical"
android:foregroundGravity="center"
android:fitsSystemWindows="true"
>
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:layout_gravity="center"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="none" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loading_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/bg"
android:drawingCacheQuality="high"
android:fitsSystemWindows="true"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/loading_bar"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:padding="10dp"
android:text="Loading. Please Wait"
android:textStyle="bold"
android:typeface="normal" />
<ProgressBar
android:id="@+id/loading_bar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
WebView webview;
boolean first_time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first_time = true;
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setAppCacheEnabled(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
// setting the required pluggins
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.setWebViewClient( new MyWebViewClient());
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//Zoom Controls
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setLightTouchEnabled(true);
webview.getSettings().setSupportZoom(true);
// below set html url
webview.loadUrl("set_html_url");
}
private class MyWebViewClient extends WebViewClient
{
ProgressBar bar;
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
bar = (ProgressBar) findViewById(R.id.loading_bar);
bar.setVisibility(View.VISIBLE);
bar.bringToFront();
if(!first_time)
findViewById(R.id.loading_ll).setBackgroundColor(color.transparent);
findViewById(R.id.loading_ll).setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
first_time = false;
bar.clearFocus();
bar.setVisibility(View.INVISIBLE);
findViewById(R.id.loading_ll).setVisibility(View.INVISIBLE);
super.onPageFinished(view, url);
}
}
@Override
public void onBackPressed()
{
// TODO Auto-generated method stub
if(webview.getUrl().contains(getString(R.string.START_PAGE_PATH)))
moveTaskToBack(true);
else
webview.loadUrl(getString(R.string.START_PAGE_PATH));
}
}