現(xiàn)在的實際項目中一般是少不了混合開發(fā)的,通常加載頁面我們都需要一個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);
//設置內(nèi)部加載器
setWebChromeClient(new MyWebChromeClient(context,progressBar));
setWebViewClient(new MyWebViewClient());
}
}
代碼說明:
我們將一個progressBar添加到我們的WebView中去井厌∶氤啵可是哪里可以監(jiān)聽加載進度的變化了,這個時候我們就要重寫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;
}
//監(jiān)聽進度的回調(diào)
@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中實現(xiàn)對進度的監(jiān)聽痹愚。
于此同事為了不使用外部瀏覽器翔始,我們還要重寫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的內(nèi)存泄漏,所以應該伴隨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();
}
}