在Android開發(fā)中,經(jīng)常需要加載顯示網(wǎng)頁演闭,一般一個頁面在打開后褒颈,在等待數(shù)據(jù)加載的過程中柒巫,都需要花一點時間,這個時候往往需要顯示一個轉(zhuǎn)動的進度條(ProgressBar)谷丸,接下來封裝了一個自定義控件和加載網(wǎng)頁的公共Activity堡掏,方便使用。此文章已經(jīng)同步到CSDN啦刨疼,歡迎訪問我的博客
一般的做法是在layout.xml中添加ProgressBar泉唁,但我們不這樣做,主要是為了減少layout嵌套揩慕。
按照慣例我們先來看看最終的效果圖:
這里寫圖片描述
在調(diào)用的時候很簡單亭畜,就只需要傳遞一個url(加載網(wǎng)頁的url)和title(顯示標(biāo)題)就可以了,如下所示:
Intent intent = new Intent(MainActivity.this, MainWebViewActivity.class);
intent.putExtra("url", "http://blog.csdn.net/qq_20785431");
intent.putExtra("title", "我的博客");
startActivity(intent);
1.接下來主要還是看看重寫的帶加載條的webview
package com.per.loadingwebviewdome;
import android.content.Context;
import android.os.Environment;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
/**
* @author: xiaolijuan
* @description: 帶加載條的webview
* @date: 2016-06-03
* @time: 23:34
*/
public class LoadingWebView extends WebView {
private ProgressBar mProgressBar;
/**
* 網(wǎng)頁緩存目錄
*/
private static final String cacheDirPath = Environment
.getExternalStorageDirectory() + "/LoadingWebViewDome/webCache/";
public LoadingWebView(Context context) {
super(context, null);
}
public LoadingWebView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public LoadingWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initContext(context);
}
private void initContext(Context context) {
requestFocus();
setInitialScale(39);
getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//支持通過Javascript打開新窗口
getSettings().setJavaScriptEnabled(true);//設(shè)置WebView屬性迎卤,能夠執(zhí)行Javascript腳本
getSettings().setUseWideViewPort(true);//將圖片調(diào)整到適合webview的大小
getSettings().setLoadWithOverviewMode(true);// 縮放至屏幕的大小
getSettings().setDomStorageEnabled(true);//設(shè)置是否啟用了DOM Storage API
getSettings().setDatabaseEnabled(true);//開啟database storage API功能
getSettings().setDatabasePath(cacheDirPath); //設(shè)置數(shù)據(jù)庫緩存路徑
getSettings().setAppCachePath(cacheDirPath);//設(shè)置Application Caches緩存目錄
getSettings().setAppCacheEnabled(true);//開啟Application Caches功能
}
/**
* 加載網(wǎng)頁url
*
* @param url
*/
public void loadMessageUrl(String url) {
super.loadUrl(url);
setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) { // 重寫此方法表明點擊網(wǎng)頁里面的鏈接不調(diào)用系統(tǒng)瀏覽器拴鸵,而是在本W(wǎng)ebView中顯示
loadUrl(url);//加載需要顯示的網(wǎng)頁
return true;
}
});
}
/**
* 添加進度條
*/
public void addProgressBar() {
mProgressBar = new ProgressBar(getContext(), null,
android.R.attr.progressBarStyleHorizontal);
mProgressBar.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, 5, 0, 0));
mProgressBar.setProgressDrawable(getContext().getResources()
.getDrawable(R.drawable.bg_pb_web_loading));
addView(mProgressBar);//添加進度條至LoadingWebView中
setWebChromeClient(new WebChromeClient());//設(shè)置setWebChromeClient對象
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
mProgressBar.setVisibility(GONE);
} else {
if (mProgressBar.getVisibility() == GONE)
mProgressBar.setVisibility(VISIBLE);
mProgressBar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
}
/**
* 回收webview
*/
public void destroyWebView() {
clearCache(true);
clearHistory();
}
}
我們重寫了3個構(gòu)造方法,默認(rèn)的布局文件調(diào)用的是兩個參數(shù)的構(gòu)造方法蜗搔,所以記得讓所有的構(gòu)造調(diào)用我們的三個參數(shù)的構(gòu)造劲藐,我們在三個參數(shù)的構(gòu)造中獲得自定義View的屬性。
然后在布局中聲明我們的自定義View
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/common_top_banner" />
<com.per.loadingwebviewdome.LoadingWebView
android:id="@+id/wv_loading"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
2.下面就是通用的帶進度條的WebView啦
package com.per.loadingwebviewdome;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
/**
* @author: xiaolijuan
* @description: 通用的帶進度條的WebView
* @date: 2016-06-03
* @time: 23:32
*/
public class MainWebViewActivity extends Activity implements View.OnClickListener {
private ImageView mIvBack;
private TextView mTvTitle;
private LoadingWebView mLoadingWebView;
private String mTitle = "";
private String mUrl = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
initView();
initData();
}
private void initView() {
mIvBack = (ImageView) findViewById(R.id.iv_back);
mLoadingWebView = (LoadingWebView) findViewById(R.id.wv_loading);
mTvTitle = (TextView) findViewById(R.id.tv_title);
mLoadingWebView.addProgressBar();
mIvBack.setOnClickListener(this);
}
private void initData() {
mTitle = getIntent().getStringExtra("title");
mUrl = getIntent().getStringExtra("url");
mLoadingWebView.loadMessageUrl(mUrl);
mTvTitle.setText(mTitle);
}
@Override
public void onDestroy() {
super.onDestroy();
mLoadingWebView.destroyWebView();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_back:
if (mLoadingWebView.canGoBack())
mLoadingWebView.goBack();
else {
finish();
}
break;
}
}
/**
* 按返回鍵時樟凄, 不退出程序而是返回WebView的上一頁面
*/
@Override
public void onBackPressed() {
if (mLoadingWebView.canGoBack())
mLoadingWebView.goBack();
else {
super.onBackPressed();
}
}
}