现在的实际项目中一般是少不了混合开发的,通常加载页面我们都需要一个WebView,为了美观我们还需要一个用于显示当前加载进度的进度条。接下来就看看我们自定义的WebView:
**
* Created by SerryWang
* on 2018/8/23
*/
public class ProgressWebView extends WebView {
private ProgressBar progressBar;
private Context context;
public ProgressWebView(Context context, AttributeSet attrs) {
super(context, attrs);
progressBar = new ProgressBar(context,null,android.R.attr.progressBarStyleHorizontal);
progressBar.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,3));//设置宽高属性
addView(progressBar);
//设置内部加载器
setWebChromeClient(new MyWebChromeClient(context,progressBar));
setWebViewClient(new MyWebViewClient());
}
}
代码说明:
我们将一个progressBar添加到我们的WebView中去。可是哪里可以监听加载进度的变化了,这个时候我们就要重写WebChromeClient;
WebChromeClient:
/**
* Created by SerryWang
* on 2018/8/23
*/
public class MyWebChromeClient extends WebChromeClient {
private Context context;
private ProgressBar progressBar;
public MyWebChromeClient(Context context, ProgressBar progressBar){
this.context = context;
this.progressBar = progressBar;
}
//监听进度的回调
@Override
public void onProgressChanged(WebView view, int newProgress) {
if(newProgress == 100){
progressBar.setVisibility(View.GONE);
}else{
if(progressBar.getVisibility() == View.GONE){
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(newProgress);
}
}
super.onProgressChanged(view, newProgress);
}
}
可以看到构造方法中接收progressBar,在onProgressChanged中实现对进度的监听。
于此同事为了不使用外部浏览器,我们还要重写WebViewClient.
WebViewClient:
/**
* Created by SerryWang
* on 2018/8/23
*/
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
}
最后记得防止WebView的内存泄漏,所以应该伴随Activity或者Fragment的生命周期。
public class WebActivity extends AppCompatActivity {
private LinearLayout mLinearLayout;
private AgentWeb mAgentWeb;
private ToolBarView toolBarView;
private WebView webView;
private ProgressWebView progressWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
initView();
LoadUrl();
}
private void LoadUrl() {
progressWebView.getSettings().setJavaScriptEnabled(true);
progressWebView.loadUrl("file:///android_asset/detaillss/index.html");
}
private void initView() {
mLinearLayout = findViewById(R.id.mLinearLayout);
toolBarView = findViewById(R.id.WebTool);
progressWebView = findViewById(R.id.Web);
toolBarView.setmNavigationTip(Constant.Toolbar_Title);
}
@Override
protected void onPause() {
if(progressWebView!=null){
progressWebView.pauseTimers();
progressWebView.onPause();
}
super.onPause();
}
@Override
protected void onResume() {
if(progressWebView != null){
progressWebView.onResume();
}
super.onResume();
}
@Override
protected void onDestroy() {
if(progressWebView != null){
progressWebView.stopLoading();
progressWebView.removeAllViews();
progressWebView.setWebViewClient(null);
progressWebView.setWebChromeClient(null);
unregisterForContextMenu(progressWebView);
progressWebView.destroy();
}
super.onDestroy();
}
}