带进度条的WebView

自己封装的一个带进度条的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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值