/** * Description: init the webview object * We add webView object into view with codes but not in XML, the benefits is that we can release * the memory more deeply and clearly when we destroy webview's instance. * Created by Michael Lee on 9/21/16 10:47 * @param aContext current context */ privatevoidinitWebView(Context aContext){ RelativeLayout webview_container = (RelativeLayout) findViewById(R.id.base_web_view_container); web_view_ = new MyWebView(aContext,null); web_view_.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); web_view_.setOnTouchListener(this);
/** * Description: see {@link LinearLayout#onLayout(boolean, int, int, int, int)} * After views are initialized, get web_view's location in screen to check if able to pull_to_ * refresh. And these codes only be execute once. * Created by Michael Lee on 9/21/16 15:46 */ @Override publicvoidonLayout(boolean changed, int l, int t, int r, int b){ super.onLayout(changed,l,t,r,b); if (changed && !load_once_) { // confirm the WebView's original location web_view_.getLocationOnScreen(location_array_); original_location_y_ = location_array_[1];
/**--------------------------------------------------------------------------------------------- * Description: mywebview extends {@link WebView} * override onScrollChanged method to set if we should allow pull-to-refresh, cause when user * is navigating the web page, we should NOT show the pull-to-refresh header. *--------------------------------------------------------------------------------------------*/ publicclassMyWebViewextendsWebView{ /** * Description: Default Constructor * Created by Michael Lee on 9/21/16 17:13 * @param context context */ publicMyWebView(Context context){ this(context,null); }
/** * Description: copy constructor. * Do NOT invoke this(context, attrs,0), cause if style type was 0, would not response click * event, and if type is 'com.android.internal.R.attr.webViewStyle', there would be on compile * error: * You cannot access id's of com.android.internal.R at compile time, but you can access the * defined internal resources at runtime and get the resource by name. * You should be aware that this is slower than direct access and there is no guarantee. * Created by Michael Lee on 9/21/16 17:13 * @param context context * @param attrs attributes Set */ publicMyWebView(Context context, AttributeSet attrs){ this(context, attrs, Resources.getSystem().getIdentifier("webViewStyle", "attr", "android")); }
/** * Description: Copy Constructor * Created by Michael Lee on 9/21/16 17:13 * @param context context * @param attrs attributes set * @param defStyle sytle type */ @SuppressLint({"SetJavaScriptEnabled"}) publicMyWebView(Context context, AttributeSet attrs, int defStyle){ super(context, attrs, defStyle); // Set WebView client setWebViewClient(new MyWebViewClient()); // set JS getSettings().setJavaScriptEnabled(true); // Block press and hold event this.setOnLongClickListener(new OnLongClickListener() { @Override publicbooleanonLongClick(View v){ returntrue; } }); }
/** * Description: see {@link WebView#onScrollChanged} * When webview scrolling, keep the progress bar on the top. * Created by Michael Lee on 9/21/16 17:13 * @param l Current horizontal scroll origin * @param t current vertical scroll origin * @param oldl Previous horizontal scroll origin * @param oldt Previous vertical scroll origin */ @Override protectedvoidonScrollChanged(int l, int t, int oldl, int oldt){ // if current vertical scroll origin is not 0, CAN NOT pull to refresh can_pull_to_refresh_ = (t == 0); super.onScrollChanged(l, t, oldl, oldt); } }
/** * Description: View.OnTouchListener * Created by Michael Lee on 9/21/16 16:23 * @param v touched view * @param event motion event * @return 'false' means do nothing, 'true' means do custom actions. */ @Override publicbooleanonTouch(View v, MotionEvent event){ web_view_.getLocationOnScreen(location_array_); // we should reduce the original coordinate by 1, cause when convert float to int, there will // be 1 pix error. if ((original_location_y_ - 1) <= location_array_[1] && can_pull_to_refresh_) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: pressed_coords_y = event.getRawY(); break;
case MotionEvent.ACTION_MOVE: float y_move = event.getRawY(); int distance = (int) (y_move - pressed_coords_y);
/** * Description: this method is 'public' for outer class to load url into progressWebView * manually. * In some case, outer class may want to load url into webView. * For example, when login success, the inside activity should refresh the url before goto login. * Created by Michael Lee on 9/21/16 10:41 * @param url url for navigating */ publicvoidloadUrl(String url){ if (web_view_ == null && current_context_ != null) { Log.d(TAG,"current web view has not be init, do it now"); initWebView(current_context_); } if (url != null && !url.equals("")) { web_view_.loadUrl(url); } else { Log.e(TAG,"Url navigating is NULL or empty"); } }
/** * Description: webView's canGoBack * Created by Michael Lee on 9/21/16 15:26 * @return can go back? true = yes, false = no. */ publicbooleancanGoBack(){ return web_view_.canGoBack(); }
/** * Description: webView's goBack * Created by Michael Lee on 9/21/16 15:30 */ publicvoidgoBack(){ if (canGoBack()) { web_view_.goBack(); } else { Log.d(TAG,"Can NOT go back anymore"); } }