如何在webview中显示和隐藏进度条 - android

时间:2014-03-31 13:13:17

标签: android webview progress-bar

我在网络应用程序中工作并尝试显示和隐藏进度条,但它无法正常工作。当用户点击手柄按钮时,我也会使用幻灯片抽屉显示此网页。所以这是我使用的java代码

        tweb = (WebView) findViewById(R.id.twitter_web);
    WebSettings webSettings1 = tweb.getSettings();
    webSettings1.setJavaScriptEnabled(true);    
    tweb.loadUrl("https://www.twitter.com");    
    tweb.setWebViewClient(new WebViewClient());
    class viewclient_twitter extends WebViewClient {

             @Override
             public void onPageStarted(WebView view, String url, Bitmap favicon) {

              super.onPageStarted(view, url, favicon);
              findViewById(R.id.progressBar2).setVisibility(View.VISIBLE);
              findViewById(R.id.textView2).setVisibility(View.VISIBLE);

             }
            @Override
            public void onPageFinished(WebView view, String url) {

            findViewById(R.id.progressBar2).setVisibility(View.GONE);
                findViewById(R.id.textView2).setVisibility(View.GONE);
            }
        } 

这是xml代码

    <SlidingDrawer
    android:id="@+id/twitter_slide"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:content="@+id/content"
    android:handle="@+id/handle" >

    <Button
        android:id="@+id/handle"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:background="@drawable/handle"
        android:nextFocusRight="@+id/handlee"

         />

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ProgressBar
            android:id="@+id/progressBar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/progressBar2"
            android:layout_below="@+id/progressBar2"
            android:text="Loading. . ."
            android:textSize="10sp" />

        <WebView
            android:id="@+id/twitter_web"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>
</SlidingDrawer>

所以请问如何让这个进度条显示并在页面加载完成后隐藏?

1 个答案:

答案 0 :(得分:0)

以下是我的修复方法,

protected ProgressDialog progressDialog;
private static final int SHOW_PROGRESS = 0x01;
private static final int STOP_PROGRESS = 0x02;

创建webview客户端将为您提供帮助,

WebSettings settings = myWebView.getSettings();
settings.setLoadWithOverviewMode(true);
settings.setJavaScriptEnabled(true);

//在此处显示进度。     mHandler.sendEmptyMessage(SHOW_PROGRESS);

myWebView.setWebViewClient(new WebViewClient() {

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    public void onPageFinished(WebView view, String url) {
        mHandler.sendEmptyMessage(STOP_PROGRESS);
        // animate(view);
    }

    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        mHandler.sendEmptyMessage(STOP_PROGRESS);
        Utils.displayToast("We are sorry! " + description,
                CommonWebView.this);
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

});

Handler mHandler = new Handler() {

    public void handleMessage(android.os.Message msg) {
        switch (msg.what) {

        case SHOW_PROGRESS:
            if (progressDialog == null) {
                progressDialog = Utils
                        .createProgressDialog(CommonWebView.this);
// Create your own progress and show here.

                progressDialog.show();

            } else {
                progressDialog.show();

            }
            mHandler.removeMessages(SHOW_PROGRESS);
            break;

        case STOP_PROGRESS:
            progressDialog.dismiss();
            mHandler.removeMessages(STOP_PROGRESS);
            break;
        }
    };
};