【WebView】帶載入進度條的WebView及Chrome聯調

來源:互聯網
上載者:User

標籤:android   移動   webview   chrome   progressbar   

先看:


看到頂部藍色的進度條了。


原理:用到了 android.webkit.WebChromeClient中的onProgressChanged,而android.webkit.WebViewClient是沒有這個方法的。所以普通的WebView是無法實現進度條的。


下面直接上乾貨:

/** * ProgressWebView *  * @author lif *  *  */@SuppressWarnings("deprecation")public class ProgressWebView extends WebView {private ProgressBar progressbar;@SuppressLint("NewApi")public ProgressWebView(Context context, AttributeSet attrs) {super(context, attrs);progressbar = new ProgressBar(context, null,android.R.attr.progressBarStyleHorizontal);progressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,16, 0, 0));addView(progressbar);setWebViewClient(new WebCustomClient());setWebChromeClient(new WebChromeClient());// 發布取消//<a target=_blank href="http://www.itbookstudy.com/forum.php?mod=viewthread&tid=108500" style="word-wrap: break-word; color: rgb(51, 51, 51); text-decoration: none; font-family: 'Microsoft Yahei', Hei, Tahoma, SimHei, sans-serif; font-size: 20px; font-weight: bold; line-height: 30px; background-color: rgb(241, 242, 246);" se_prerender_url="complete">Web開發調試之Chrome遠端偵錯(Remote Debugging)</a>會用到// setWebContentsDebuggingEnabled(true);getSettings().setDomStorageEnabled(true);}public class WebCustomClient extends android.webkit.WebViewClient {@Overridepublic void onPageFinished(WebView view, String url) {// TODO Auto-generated method stubsuper.onPageFinished(view, url);// 頁面下載完畢,卻不代表頁面渲染完畢顯示出來// WebChromeClient中progress==100時也是一樣if (view.getContentHeight() != 0) {// 這個時候網頁才顯示}}@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {// TODO Auto-generated method stub// 自身載入新連結,不做外部跳轉view.loadUrl(url);return true;}}public class WebChromeClient extends android.webkit.WebChromeClient {@Overridepublic void onProgressChanged(WebView view, int newProgress) {if (newProgress == 100) {progressbar.setVisibility(GONE);} else {if (progressbar.getVisibility() == GONE)progressbar.setVisibility(VISIBLE);progressbar.setProgress(newProgress);}super.onProgressChanged(view, newProgress);}}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();lp.x = l;lp.y = t;progressbar.setLayoutParams(lp);super.onScrollChanged(l, t, oldl, oldt);}}

調用代碼:

// ~~~ 設定資料webview.setVerticalScrollbarOverlay(true);webview.getSettings().setJavaScriptEnabled(true);webview.getSettings().setAllowFileAccess(true);webview.getSettings().setSavePassword(false);webview.setDownloadListener(new DownloadListener() {@Overridepublic void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) {if (url != null&& (url.startsWith("http://") || url.startsWith("file:///")))startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));}});webview.loadUrl(url);
有興趣的朋友可以嘗試一下


注意:關於Chrome遠程開發調試可以參考移動端Web開發調試之Chrome遠端偵錯(Remote Debugging)

【WebView】帶載入進度條的WebView及Chrome聯調

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.