互联网技术

带进度条的WebView

2017-07-29  本文已影响57人  一点愁

自己封装的一个带进度条的WebView,在此分享一下

WebView添加自定义进度条;实现缓存,有网取网络数据,无网取缓存数据

1、自定义一个ProgressWebView继承webview

public class ProgressWebView extends WebView{

private final String TAG="ProgressWebView";

Context context;

ProgressBar progressbar;

public ProgressWebView(Contextcontext) {

super(context);

this.context= context;

init();

setWebChromeClient(newWebChromeClient());

setWebViewClient(newMyWebViewClient());

settings();

}

publicProgressWebView(Contextcontext,AttributeSetattrs) {

super(context,attrs);

}

public ProgressWebView(Contextcontext,AttributeSetattrs,intdefStyleAttr) {

super(context,attrs,defStyleAttr);

}

/**

* 设置ProgressBar

*/

voidinit(){

progressbar=new ProgressBar(context,null,android.R.attr.progressBarStyleHorizontal);

//progressbar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.progress_horizontal_color));

progressbar.setProgressDrawable(getResources().getDrawable(R.drawable.progress_horizontal_color));

progressbar.setMax(100);

//progressbar.setIndeterminate(false);

progressbar.setLayoutParams(newLayoutParams(LayoutParams.MATCH_PARENT,5,0,0));

addView(progressbar);

}

public class WebChromeClient extends android.webkit.WebChromeClient{

@Override

public void onProgressChanged(WebViewview,intnewProgress) {

progressbar.setProgress(newProgress);

LogUtil.log(TAG,Log.ERROR,progressbar.getProgress() +"========newProgress========"+ newProgress);

if(newProgress ==100) {

progressbar.setVisibility(GONE);

}else{

progressbar.setVisibility(VISIBLE);

}

super.onProgressChanged(view,newProgress);

}

@Override

public void onReceivedTitle(WebViewview,Stringtitle) {

super.onReceivedTitle(view,title);

if(onWebCallBack!=null){//获取标题

onWebCallBack.getTitle( title );

}

LogUtil.log(TAG,Log.ERROR,"==========title========"+ title);

}

}

/**

* 不重写的话,会跳到手机浏览器中

*@authoradmin

*/

public class MyWebViewClient extends WebViewClient{

@Override

public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) {

//            if(canGoBack())

//                goBack();

}

@Override

public void onPageFinished(WebView view,String url) {

super.onPageFinished(view,url);

}

@Override

public void onPageStarted(WebView view,String url,Bitmap favicon) {

}

@Override

public boolean shouldOverrideUrlLoading(WebView view,WebResourceRequest request) {

if(NetStateUtils.isConnect(context))

return super.shouldOverrideUrlLoading(view,request);

else{

ToastUtil.showToast("暂无网络啊亲");

return false;

}

}

@Override

public void onLoadResource(WebView view,String url) {

super.onLoadResource(view,url);

}

}

@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);

}

/**

* 设置WebView的回掉器

*@paramonWebCallBack

*/

public void setOnWebCallBack(OnWebCallBack onWebCallBack ){

this.onWebCallBack= onWebCallBack;

}

OnWebCallBack onWebCallBack;

public interface OnWebCallBack{

/**

* 获取标题

*@paramtitle

*/

void getTitle(String title);

/**

* 获得WebView的地址

*@paramurl

*/

void getUrl(String url);

}

private void settings(){

WebSettings webSettings= getSettings();

webSettings.setJavaScriptEnabled(true);//启用js

webSettings.setBlockNetworkImage(false);//解决图片不显示

webSettings.setAppCacheEnabled(true);

if(NetStateUtils.isConnect(context)) {

webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

}else{

webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

}

}

@Override

public boolean onKeyDown(int keyCode,KeyEven tevent) {

if(canGoBack() && event.getKeyCode() ==KeyEvent.KEYCODE_BACK&& event.getRepeatCount() ==0) {

goBack();

return true;

}

return super.onKeyDown(keyCode,event);

}

}

2、自定义进度条颜色progress_horizontal_color文件放在drawable文件夹下

<layer-listxmlns:android="http://schemas.android.com/apk/res/android">

<item

android:id="@android:id/background"

>

<clip>

<shape>

<solid android:color="@color/white"/>

</shape>

</clip>

</item>

<item

android:id="@android:id/secondaryProgress"

>

<clip>

<shape>

<solid android:color="@color/white"/>

</shape>

</clip>

</item>

<item

android:id="@android:id/progress"

>

<clip>

<shape>

<solid android:color="@color/cadmium_green_pale"/>

</shape>

</clip>

</item>

</layer-list>

上一篇下一篇

猜你喜欢

热点阅读