Android上用于Webview的“内置”progressBar技术

时间:2016-05-27 10:37:57

标签: android webview

如何在Android上为Webview添加加载栏?

我有以下工作Webview代码:

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

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        // Force links and redirects to open in the WebView instead of in a browser (commented out, we'll allow fice URLs only)
        //mWebView.setWebViewClient(new WebViewClient());

        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new MyAppWebViewClient());

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.loadUrl("http://google.com");

    }

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

我的目标是添加一个简单的程序栏/加载栏,但似乎无法找到如何做到这一点。

3 个答案:

答案 0 :(得分:1)

  

试试这个。

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        // Force links and redirects to open in the WebView instead of in a browser (commented out, we'll allow fice URLs only)
        //mWebView.setWebViewClient(new WebViewClient());

        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new myWebClient());

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.loadUrl("http://google.com");

    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

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

不要忘记在Web View之前在XML中添加它。

<ProgressBar
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:layout_gravity="center"
       android:id="@+id/progressBar1"/>      

答案 1 :(得分:0)

您应该实现自定义WebViewClient

mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new CustomWebViewClient());
mProgressBar.setVisibility(View.VISIBLE); // here show your progress view
mWebView.loadUrl("http://google.com");

和自定义WebViewClient:

private class CustomWebViewClient extends WebViewClient
    {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mProgressBar.setVisibility(View.GONE);
                }
            }); // here hide progress view
        }

    }

答案 2 :(得分:0)

这是你的代码。试试吧!!!

创建类MyWebChromeClient.class

public class MyWebChromeClient extends WebChromeClient {
private ProgressListener mListener;

public MyWebChromeClient(ProgressListener listener) {
    mListener = listener;
}

@Override
public void onProgressChanged(WebView view, int newProgress) {
    mListener.onUpdateProgress(newProgress);
    super.onProgressChanged(view, newProgress);
}

public interface ProgressListener {
    public void onUpdateProgress(int progressValue);
}
}
WebviewActivity.class中的

public class WebviewActivity extends BaseActivity implements MyWebChromeClient.ProgressListener {

  private ProgressBar mProgressBar;
  private WebView mWebView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview_layout);
    mWebView = (WebView) findViewById(R.id.web_view);
    mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    mWebView.setWebChromeClient(new MyWebChromeClient(this));
    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mProgressBar.setVisibility(View.VISIBLE);

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mProgressBar.setVisibility(View.GONE);

        }
    });

}
@Override
public void onUpdateProgress(int progressValue) {
    mProgressBar.setProgress(progressValue);
    if (progressValue == 100) {
        mProgressBar.setVisibility(View.GONE);
    }
}

}

这是webview_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/progress_bar_height"
    android:progressDrawable="@drawable/bg_progress_bar_history" />

<WebView
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/progressBar" />

 </RelativeLayout>

在drawable中创建bg_progress_bar_history.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@android:id/background"
    android:drawable="@android:color/transparent" />
<item android:id="@android:id/secondaryProgress">
    <scale
        android:drawable="@drawable/progress_bar_second"
        android:scaleWidth="100%" />
</item>
<item android:id="@android:id/progress">
    <scale
        android:drawable="@color/color_bg_splash"
        android:scaleWidth="100%" />
</item>

</layer-list>