当前位置: 移动技术网 > 移动技术>移动开发>Android > Adnroid打造通用的带进度条的WebView

Adnroid打造通用的带进度条的WebView

2019年07月24日  | 移动技术网移动技术  | 我要评论
在android开发中,经常需要加载显示网页,一般一个页面在打开后,在等待数据加载的过程中,都需要花一点时间,这个时候往往需要显示一个转动的进度条(progressbar)

在android开发中,经常需要加载显示网页,一般一个页面在打开后,在等待数据加载的过程中,都需要花一点时间,这个时候往往需要显示一个转动的进度条(progressbar),接下来封装了一个自定义控件和加载网页的公共activity,方便使用。
一般的做法是在layout.xml中添加progressbar,但我们不这样做,主要是为了减少layout嵌套。
按照惯例我们先来看看最终的效果图:

在调用的时候很简单,就只需要传递一个url(加载网页的url)和title(显示标题)就可以了,如下所示:

intent intent = new intent(mainactivity.this, mainwebviewactivity.class);
  intent.putextra("url", "http://blog.csdn.net/qq_20785431");
  intent.putextra("title", "我的博客");
  startactivity(intent);

1.接下来主要还是看看重写的带加载条的webview

package com.per.loadingwebviewdome;

import android.content.context;
import android.os.environment;
import android.util.attributeset;
import android.webkit.webview;
import android.webkit.webviewclient;
import android.widget.progressbar;

/**
 * @author: xiaolijuan
 * @description: 带加载条的webview
 * @date: 2016-06-03
 * @time: 23:34
 */
public class loadingwebview extends webview {

 private progressbar mprogressbar;
 /**
  * 网页缓存目录
  */
 private static final string cachedirpath = environment
   .getexternalstoragedirectory() + "/loadingwebviewdome/webcache/";

 public loadingwebview(context context) {
  super(context, null);
 }

 public loadingwebview(context context, attributeset attrs) {
  super(context, attrs, 0);
 }

 public loadingwebview(context context, attributeset attrs, int defstyle) {
  super(context, attrs, defstyle);
  initcontext(context);
 }

 private void initcontext(context context) {
  requestfocus();
  setinitialscale(39);
  getsettings().setjavascriptcanopenwindowsautomatically(true);//支持通过javascript打开新窗口
  getsettings().setjavascriptenabled(true);//设置webview属性,能够执行javascript脚本
  getsettings().setusewideviewport(true);//将图片调整到适合webview的大小
  getsettings().setloadwithoverviewmode(true);// 缩放至屏幕的大小
  getsettings().setdomstorageenabled(true);//设置是否启用了dom storage api
  getsettings().setdatabaseenabled(true);//开启database storage api功能
  getsettings().setdatabasepath(cachedirpath); //设置数据库缓存路径
  getsettings().setappcachepath(cachedirpath);//设置application caches缓存目录
  getsettings().setappcacheenabled(true);//开启application caches功能
 }

 /**
  * 加载网页url
  *
  * @param url
  */
 public void loadmessageurl(string url) {
  super.loadurl(url);
  setwebviewclient(new webviewclient() {
   public boolean shouldoverrideurlloading(webview view, string url) { // 重写此方法表明点击网页里面的链接不调用系统浏览器,而是在本webview中显示
    loadurl(url);//加载需要显示的网页
    return true;
   }
  });
 }

 /**
  * 添加进度条
  */
 public void addprogressbar() {
  mprogressbar = new progressbar(getcontext(), null,
    android.r.attr.progressbarstylehorizontal);
  mprogressbar.setlayoutparams(new layoutparams(
    layoutparams.match_parent, 5, 0, 0));
  mprogressbar.setprogressdrawable(getcontext().getresources()
    .getdrawable(r.drawable.bg_pb_web_loading));
  addview(mprogressbar);//添加进度条至loadingwebview中

  setwebchromeclient(new webchromeclient());//设置setwebchromeclient对象
 }

 public class webchromeclient extends android.webkit.webchromeclient {
  @override
  public void onprogresschanged(webview view, int newprogress) {
   if (newprogress == 100) {
    mprogressbar.setvisibility(gone);
   } else {
    if (mprogressbar.getvisibility() == gone)
     mprogressbar.setvisibility(visible);
    mprogressbar.setprogress(newprogress);
   }
   super.onprogresschanged(view, newprogress);
  }
 }

 /**
  * 回收webview
  */
 public void destroywebview() {
  clearcache(true);
  clearhistory();
 }
}

我们重写了3个构造方法,默认的布局文件调用的是两个参数的构造方法,所以记得让所有的构造调用我们的三个参数的构造,我们在三个参数的构造中获得自定义view的属性。

然后在布局中声明我们的自定义view

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <include layout="@layout/common_top_banner" />

 <com.per.loadingwebviewdome.loadingwebview
  android:id="@+id/wv_loading"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

</linearlayout>

2.下面就是通用的带进度条的webview啦

package com.per.loadingwebviewdome;

import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.widget.imageview;
import android.widget.textview;

/**
 * @author: xiaolijuan
 * @description: 通用的带进度条的webview
 * @date: 2016-06-03
 * @time: 23:32
 */
public class mainwebviewactivity extends activity implements view.onclicklistener {

 private imageview mivback;
 private textview mtvtitle;
 private loadingwebview mloadingwebview;

 private string mtitle = "";
 private string murl = "";

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_webview);
  initview();
  initdata();
 }

 private void initview() {
  mivback = (imageview) findviewbyid(r.id.iv_back);
  mloadingwebview = (loadingwebview) findviewbyid(r.id.wv_loading);
  mtvtitle = (textview) findviewbyid(r.id.tv_title);

  mloadingwebview.addprogressbar();
  mivback.setonclicklistener(this);
 }

 private void initdata() {
  mtitle = getintent().getstringextra("title");
  murl = getintent().getstringextra("url");

  mloadingwebview.loadmessageurl(murl);
  mtvtitle.settext(mtitle);
 }

 @override
 public void ondestroy() {
  super.ondestroy();
  mloadingwebview.destroywebview();
 }

 @override
 public void onclick(view v) {
  switch (v.getid()) {
   case r.id.iv_back:
    if (mloadingwebview.cangoback())
     mloadingwebview.goback();
    else {
     finish();
    }
    break;
  }
 }

 /**
  * 按返回键时, 不退出程序而是返回webview的上一页面
  */
 @override
 public void onbackpressed() {
  if (mloadingwebview.cangoback())
   mloadingwebview.goback();
  else {
   super.onbackpressed();
  }
 }
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网