向android webview java添加进度条

时间:2016-12-02 17:00:51

标签: java android xml webview

请我在Android开发中非常新,我有我的网站,我在webview加载,现在正在工作,我想添加一个进度条,以显示链接处理后,在页面加载后呈现和隐藏​​进度条。 请问我看到了很多例子,但是在尝试实现它的过程中我一直遇到错误,请有人帮助我使用我自己的webview。

package com.mypage.mypage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;


public class SourceProjectActivity extends AppCompatActivity {

    private WebView projectLoaderView;
    private ProgressBar progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_source_project);
        projectLoaderView = (WebView)findViewById(R.id.sourcejailview);
        WebSettings webSettings = projectLoaderView.getSettings();

        progress = (ProgressBar) findViewById(R.id.progressBar);
        progress.setMax(100);

        webSettings.setJavaScriptEnabled(true);
        webSettings.setSaveFormData(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setDomStorageEnabled(true);
        //local offline loader
        webSettings.setCacheMode( WebSettings.LOAD_DEFAULT );

        projectLoaderView.loadUrl("http://google.com");
        projectLoaderView.setWebViewClient(new WebViewClient());
        SourceProjectActivity.this.progress.setProgress(0);
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            //super.onProgressChanged(view, newProgress);
        }
    }

    public void setValue(int progress) {
        this.progress.setProgress(progress);
    }

    @Override
    public void onBackPressed(){
        if(projectLoaderView.canGoBack()){
            projectLoaderView.goBack();
        }else {
            super.onBackPressed();
        }
    }

}

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app=""
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_source_project"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mypage.mypage.SourceProjectActivity">

    <WebView
        android:id="@+id/sourcejailview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true" >

        <ProgressBar
            style="@android:style/Widget.Material.Light.ProgressBar.Small"
            android:layout_width="match_parent"
            android:layout_x="124dp"
            android:layout_y="43dp"
            android:id="@+id/progressBar"
            android:layout_height="wrap_content" />
    </WebView>
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

WebViewClient没有onProgressChanged()方法。取而代之的是,您可以使用WebChromeClient并设置进度条,如下所示:

private class MyWebViewClient extends WebViewClient {
        public void onProgressChanged(WebView view, int newProgress) {
                progress.setProgress(newProgress);
                //Maybe you want to hide it once it reaches 100
                if (newProgress == 100){
                       progress.setVisibility(ProgressBar.GONE);
                }
        }
}