WebViewの簡単な使用及びswiperefreshlayoutに対する滑り衝突問題の解決

3464 ワード

private void initVar() {
    webView = (WebView) findViewById(R.id.aw_wb_webview);
    //     javascript
    webView.getSettings().setJavaScriptEnabled(true);
    //        
    webView.getSettings().setSaveFormData(false);
    //    
    webView.getSettings().setDefaultTextEncodingName("UTF-8");
    //    
    webView.getSettings().setSupportZoom(true);
    //     
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    //       
    webView.requestFocus();
    webView.setOnTouchListener(new View.OnTouchListener() {
       @Override
        public boolean onTouch(View v,MotionEvent event) {
            switch(event.getAction()) {
                case MotionEvent.ACITON_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                //       swiperefreshlayout  
                    if(webView.getScrollY() == 0) {
                        swipe.setEnabled(true)} 
                    else{
                        swipe.setEnabled(false)}
                        break;
                case MotionEvent.ACTION_CANCEL:
                    swipe.setEnabled(false):
                    break;
                    }
                    default:
                    break;
            }
            return false;
            }
    });
    webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});
    //     
webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        if (newProgress == 100) {
            //     
            swipe.setRefreshing(false);
        } else {
            if (!swipe.isRefreshing())
                swipe.setRefreshing(true);
        }
        super.onProgressChanged(view, newProgress);
    }
});
    swipe = (SwipeRefreshLayout) findViewById(R.id.aw_swipe);
    swipe.setColorSchemeResources(android.R.color.holo_purple, 
        android.R.color.holo_blue_bright, android.R.color.holo_orange_light,
        android.R.color.holo_red_light);
    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            webView.reload();
        }
    });
    
  //        
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
private WebView webView;
private CityAnBean cityAnBean;
private String htmlStr = null;
private SwipeRefreshLayout swipe;
private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case 0x14:
                //           json          
                CityAnBean cityAnBean = (CityAnBean) msg.obj;
                String htmlStr = cityAnBean.getContent().toString();
                //    images/  
                htmlStr = htmlStr.replaceAll("images/", Constant.URL + "/portals/knowledge/images/");
                //      string        
                webView.loadData(htmlStr, "text/html; charset=UTF-8", null);
                break;
            default:
                break;
        }
    }
};