Android WebView Touch event and troubleshooting

Source: Internet
Author: User

 

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.

 

 

 

 

 

 

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.