Android 實現(xiàn)帶進(jìn)度條的WebView的實例
Android 實現(xiàn)帶進(jìn)度條的WebView的實例
1. WebView加載網(wǎng)頁方法
//加載本地資源
loadUrl("file:///android_asset/example.html");
//加載網(wǎng)絡(luò)資源
loadUrl("http://baidu.com");
2. 帶進(jìn)度的Drawable文件view_progress_webview
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#31CE15"/>
<corners android:radius="2dp"/>
</shape>
</clip>
</item>
</layer-list>
顏色值為進(jìn)度顏色,根據(jù)需要更換
3. ProgressWebView類
/**
* 帶進(jìn)度條的WebView
* @Author GQ
*/
public class ProgressWebView extends WebView {
private ProgressBar progressbar;
private Context mContext;
public ProgressWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 5, 0, 0));
Drawable drawable = context.getResources().getDrawable(R.drawable.view_progress_webview);
progressbar.setProgressDrawable(drawable);
addView(progressbar);
//主要處理解析,渲染網(wǎng)頁等瀏覽器做的事情
setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
//加載失敗時候,顯示自定義的頁面
if (errorListener != null) {
errorListener.onError();
}
}
});
//輔助WebView處理Javascript的對話框,網(wǎng)站圖標(biāo),網(wǎng)站title,加載進(jìn)度等
setWebChromeClient(new WebChromeClient());
getSettings().setSupportZoom(true);//是否可以縮放
getSettings().setBuiltInZoomControls(true);
getSettings().setJavaScriptEnabled(true);//支持JS
getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
getSettings().setUseWideViewPort(true);
getSettings().setLoadWithOverviewMode(true);
getSettings().setSaveFormData(true);
getSettings().setDomStorageEnabled(true);
//優(yōu)先使用緩存
getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//禁用長按
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return true;
}
});
//如果在瀏覽器下載,調(diào)用瀏覽器默認(rèn)下載+通知欄
setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
mContext.startActivity(intent);
}
});
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public 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);
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
if (titleListener != null)
titleListener.getTitle(title);
}
}
@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);
}
private TitleListener titleListener;
public interface TitleListener {
void getTitle(String title);
}
public void setOnTitleListener(TitleListener titleListener) {
this.titleListener = titleListener;
}
private ErrorListener errorListener;
public interface ErrorListener {
void onError();
}
public void setOnErrorListener(ErrorListener errorListener) {
this.errorListener = errorListener;
}
}
4. 使用
/**
* 公共WebView
*/
public class BasicWebActivity extends Activity {
protected ProgressWebView progressWebView;
private TextView title;//標(biāo)題欄
private TextView tv_none;//加載失敗顯示文字
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.common_webview);
title = (TextView) findViewById(R.id.title);
tv_none = (TextView) findViewById(R.id.tv_none);
progressWebView = (ProgressWebView) findViewById(R.id.progressWebView);
String url = getIntent().getStringExtra("url");
progressWebView.setOnTitleListener(new ProgressWebView.TitleListener() {
@Override
public void getTitle(String title) {
title.setText(title);
}
});
progressWebView.setOnErrorListener(new ProgressWebView.ErrorListener() {
@Override
public void onError() {
tv_none.setText("url資源失效");
}
});
//加載網(wǎng)頁
progressWebView.loadUrl(url);
}
//重寫返回鍵
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (progressWebView.canGoBack()) {
progressWebView.goBack();//返回上一層頁面
return true;
} else {
finish();//關(guān)閉頁面
}
}
return super.onKeyDown(keyCode, event);
}
}
其中common_webview就包含一個title一個progressWebView就不貼代碼了。
如有疑問,請留言或者到本站社區(qū)交流討論,本站關(guān)于Android開發(fā)的文章還有很多,還希望大家搜索參閱,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
- Android自定義帶進(jìn)度條WebView仿微信加載過程
- Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
- android實現(xiàn)用戶體驗超棒的微信WebView進(jìn)度條
- Android編程實現(xiàn)WebView添加進(jìn)度條的方法
- Android 帶進(jìn)度條的WebView 示例代碼
- Android Webview添加網(wǎng)頁加載進(jìn)度條實例詳解
- Android WebView線性進(jìn)度條實例詳解
- Android中實現(xiàn)Webview頂部帶進(jìn)度條的方法
- Android WebView實現(xiàn)頂部進(jìn)度條
相關(guān)文章
淺談Android Studio 4.1 更新內(nèi)容
這篇文章主要介紹了淺談Android Studio 4.1 更新內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Android 關(guān)于“NetworkOnMainThreadException”問題的原因分析及解決辦法
這篇文章主要介紹了Android 關(guān)于“NetworkOnMainThreadException”的相關(guān)知識,本文介紹的非常詳細(xì),具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
Android畫板開發(fā)之添加背景和保存畫板內(nèi)容為圖片
這篇文章主要為大家詳細(xì)介紹了Android畫板開發(fā)之添加背景和保存畫板內(nèi)容為圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
解決android關(guān)于打開虛擬機(jī)時右側(cè)工具欄不顯示的問題
下面小編就為大家分享一篇解決android關(guān)于打開虛擬機(jī)時右側(cè)工具欄不顯示的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android SwipeRefreshLayout下拉刷新組件示例
SwipeRefrshLayout是Google官方更新的一個Widget,可以實現(xiàn)下拉刷新的效果。本文主要介紹了Android之SwipeRefreshLayout下拉刷新組件示例,有興趣的可以了解一下。2017-02-02
DialogFragment運(yùn)行原理及使用方法詳解
這篇文章主要介紹了DialogFragment運(yùn)行原理及使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
Kotlin 擴(kuò)展函數(shù)和擴(kuò)展屬性的使用方法
這篇文章主要介紹了Kotlin 擴(kuò)展函數(shù)和擴(kuò)展屬性的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Android中創(chuàng)建快捷方式及刪除快捷方式實現(xiàn)方法
這篇文章主要介紹了Android中創(chuàng)建快捷方式及刪除快捷方式實現(xiàn)方法,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-06-06

