[Android]带进度条的WebView
@Author GQ 2016年06月22日
最近用到这个就整理了一下,以后直接copy直接使用。
1. WebView加载网页方法
loadUrl("file:///android_asset/example.html");
loadUrl("http://baidu.com");
2. 带进度的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>
3. ProgressWebView类
/**
* 带进度条的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);
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();
}
}
});
setWebChromeClient(new WebChromeClient());
getSettings().setSupportZoom(true);
getSettings().setBuiltInZoomControls(true);
getSettings().setJavaScriptEnabled(true);
getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
getSettings().setUseWideViewPort(true);
getSettings().setLoadWithOverviewMode(true);
getSettings().setSaveFormData(true);
getSettings().setDomStorageEnabled(true);
getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return true;
}
});
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;
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资源失效");
}
});
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();
}
}
return super.onKeyDown(keyCode, event);
}
}
- 其中common_webview就包含一个title一个progressWebView就不贴代码了。