how to put a progress bar in an application using

2019-09-06 07:40发布

问题:

I have a main java file that sets up a series of tabs where each one calls a seperate activity in this case each activity is linked to a webpage. I am trying to implemente a progress bar in the tab activity that will show a progress bar for when a link is clicked on regardless of what tab is currently selected. I have a progress bar that i can use for a single activity but i cant get it to work for the tabs

i have the progress bar defined as

    getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
   setContentView(R.layout.main );
   webview = (WebView) findViewById(R.id.webview);
   webview.setWebChromeClient(new WebChromeClient() {
       public void onProgressChanged(WebView view, int progress)   
       {
        MyActivity.setProgress(progress * 100);
           if(progress == 100)

              MyActivity.setTitle(R.string.app_name);
         }
       });

Which goes after oncreate()

Here is the main Java File

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.TabHost;
import android.widget.TextView;

public class UniversityofColorado extends TabActivity{

    WebView webview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        TabHost host=getTabHost();
        Resources res = getResources();

        host.addTab(host.newTabSpec("one")
                .setIndicator("",res.getDrawable(R.drawable.ic_tab_google))
                .setContent(new Intent(this, Hello.class)));

        host.addTab(host.newTabSpec("two")
                        .setIndicator("Colorado Main Site")
                        .setContent(new Intent(this, ColoradoMainSiteBrowser.class)));

        host.addTab(host.newTabSpec("three")
                        .setIndicator("CULearn")
                        .setContent(new Intent(this, CULearnBrowser.class)));

        host.addTab(host.newTabSpec("four")
                .setIndicator("CULink")
                .setContent(new Intent(this, CULinkBrowser.class)));

        host.addTab(host.newTabSpec("five")
                .setIndicator("MyCUInfo")
                .setContent(new Intent(this, MyCUInfoBrowser.class)));

        host.addTab(host.newTabSpec("six")
                .setIndicator("", res.getDrawable(R.drawable.ic_tab_map))
                .setContent(new Intent(this, CampusBrowser.class)));

        host.addTab(host.newTabSpec("Seven")
                .setIndicator("",res.getDrawable(R.drawable.ic_tab_notes))
                .setContent(new Intent(this, NotesList.class)));

        host.addTab(host.newTabSpec("eight")
                .setIndicator("", res.getDrawable(R.drawable.ic_tab_help))
                .setContent(new Intent(this, CampusBrowser.class)));
    }   

Here is one of the activities that the main java file calls

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class CampusBrowser extends Activity {

    WebView webview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webview = (WebView) findViewById(R.id.webview);
    webview.setWebViewClient(new HelloWebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://amath.colorado.edu/department/Maps/bldgs.html");
    }
    private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
    view.loadUrl(url);
    return true;
    }
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
    webview.goBack();
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }
    }

XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

If you could help me figure this out it would be appreciated

回答1:

Hey this is exactly what I do in my webviews

In the onCreate method of the webview[in your case it may be your tabActivity] do this

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.webview_simple);
}

then in the webview client do this

private class MyWebViewClient extends WebViewClient {

public synchronized boolean shouldOverrideUrlLoading(WebView view, String url){
}// and all ur implementation

    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        setProgressBarIndeterminateVisibility(false);
    }

    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        setProgressBarIndeterminateVisibility(true);
        super.onPageStarted(view, url, favicon);            
    }

}

Hope it helps. I am overriding the onPageFinished and onPageStarted in the webview client. Its difficult to do it from onProgressChanged. I tried it but I found this to be simple.