編輯:關於 Android 程式設計
在 Android 專案中用 WebView 嵌入活動網頁是很常見的做法。這種模式最大的好處是:改需求只要換網頁,不必重新打包上架。不過一旦頁面載入較慢而畫面上又沒有任何提示,使用者就會以為 App 當掉了——這正是本文要解決的問題。
做法是讓 WebView 內部額外放一個水平的 ProgressBar,並透過 WebChromeClient.onProgressChanged() 回呼同步進度,載入完成(100)後就把進度條藏起來。
/**
* 內建的 WebView,頂部帶一條載入進度條
*/
@SuppressWarnings("deprecation")
public class ProgressWebView extends WebView {
private final static String TAG = ProgressWebView.class.getSimpleName();
private ProgressBar progressBar;
private Context context;
public ProgressWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
progressBar = new ProgressBar(context, null,
android.R.attr.progressBarStyleHorizontal);
progressBar.setLayoutParams(new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.MATCH_PARENT, 3, 0, 0));
progressBar.setProgressDrawable(
getResources().getDrawable(R.drawable.wevbview_progressbar));
addView(progressBar);
setWebChromeClient(new WebChromeClient());
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
Log.d(TAG, "newProgress" + newProgress);
if (newProgress == 100) {
progressBar.setVisibility(GONE);
} else {
if (progressBar.getVisibility() == GONE) {
progressBar.setVisibility(VISIBLE);
}
progressBar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
// 攔截網頁中的 console.log,方便除錯
@Override
public boolean onConsoleMessage(ConsoleMessage cm) {
android.util.Log.d(TAG, "webview console " + cm.lineNumber()
+ " of " + cm.sourceId() + " : " + cm.message());
return true;
}
// 攔截網頁中的 alert(),改用自家的 Toast
@Override
public boolean onJsAlert(WebView view, String url,
String message, JsResult result) {
ToastUtil.showMessage(context, message,
Toast.LENGTH_SHORT, Gravity.CENTER);
result.cancel();
return true;
}
}
// 讓進度條隨內容一起捲動
@Override
protected 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);
}
}
<cn.net.huami.ui.view.ProgressWebView
android:id="@+id/them_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
新增一個 drawable 檔案,用 layer-list 分別定義背景與進度色:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 背景 -->
<item android:id="@android:id/background">
<shape>
<solid android:color="@color/default_bg" />
</shape>
</item>
<!-- 進度 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#EEAAEE" />
</shape>
</clip>
</item>
</layer-list>
ProgressWebView webView = (ProgressWebView) findViewById(R.id.them_webview);
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
// 非網頁內容交給外部瀏覽器處理
if (url != null && url.startsWith("https://")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}
});
webView.loadUrl("網頁 url");
除了進度條,正式專案通常還會處理兩件事:一是設定 WebSettings 開啟 JavaScript 與 DOM storage,並視需要處理返回鍵的 canGoBack();二是若使用 https 頁面卻載入 http 資源,需留意混合內容政策造成的封鎖。另外别忘了在 AndroidManifest 加上 INTERNET 權限。
自製 Android PullToRefresh:下拉刷新、上拉加載,並可擴充 ScrollView、ListView、GridView
找了一圈開源方案,包含 GitHub 上頗有名氣的 Android-PullToRefresh,總覺得不太合手,最後決定自己寫一個,過程中自然也借鑑了一些前人的思路。核心
Android 效能優化:如何避免 ANR 並保持介面流暢
ANR 是什麼 ANR 是 Application Not Responding 的縮寫。當應用無法在時限內回應輸入或廣播時,系統會顯示對話框,讓使用者選擇等待或關閉。
深入解析 Android Fragment(下篇)
在上篇《深入淺析 Android Fragment(上篇)》中,我們介紹了 Fragment 的基本用法與各類 API。如果想更深入地掌握 Fragment,就接著往下看
android:descendantFocusability 用法解析:解決 ListView item 點擊沒反應
這是一個開發中很常遇到的怪現象:自訂 ListView 的 item 裡放了 Button、ImageButton 或 CheckBox 這類子控件,結果點擊整列完全沒反