【问题标题】:how to show and hide the progress bar in webview - android如何在 webview 中显示和隐藏进度条 - android
【发布时间】:2014-03-31 13:13:16
【问题描述】:

我在网络应用程序中工作并尝试显示和隐藏进度条,但它不起作用。当用户单击手柄按钮时,我也使用滑动抽屉显示此网页。所以这是我使用的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>

请问我怎样才能让这个进度条在页面加载完成后显示和隐藏??

【问题讨论】:

    标签: android webview progress-bar


    【解决方案1】:

    这是我的修复方法,

    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;
            }
        };
    };
    

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多