Following the previous Android WebView FAQ and solution summary, we summarized some common problems for webview processing. This time we will talk about the touch event in webview:
Sometimes in development, we need to add the webview touch event processing, such as adding the sliding effect or similar to the page flip effect in reading. At this time, we need to rewrite the onTouch method in webview:
public class MyWebView extends WebView{public MyWebView(Context context) { super(context); }@Overridepublic boolean onTouchEvent(MotionEvent evt) { switch (evt.getAction()){ case MotionEvent.ACTION_DOWN: //do something...... break; case MotionEvent.ACTION_MOVE: //do something...... break; case MotionEvent.ACTION_UP: //do something...... break; } return false;}}
It should be noted that the returned value must be false and the event will be passed down. Otherwise, the hyperlink will not work.
After changing the return value in the onTouch () event to false, the onTouch () event is still not triggered. Why? After analyzing the code line by line, the root cause of the problem is finally found:
This is mainly because the following settings are made:
webView.getSettings().setBuiltInZoomControls(true);
This setting allows the webview control to support scaling. This scaling setting will also respond to the onTouch event, so it will overwrite our own onTouhc listener, causing an event conflict.
To set the webview size, callWebView. setInitialScale () functionTo set the webview size.
The specific value can be processed as follows:
1. If the webview size needs to be fixed, you can calculate the initial proportion of the page size based on the resolution and webview location. Of course, if the page size is fixed, it is best to perform adaptation on the H5 page.
2. If you want to scale the webview, you need to provide a scaling button for the user, and then control the value of each scaling, and then callWebView. setInitialScale (), The value size depends on the demand. If there is no requirement, you can refer to the scaling value supported by webview.