Handle Touch Events inside WebView in android

99封情书 提交于 2021-02-08 08:16:57

问题


I had created WebView that loads html files from the asset folder. Now when i am going to put GestureDetecter in that WebView it stops working ScrollView & links inside html page. but when i removes GestureDetecter both ScrollView & links works fine.

Following is my complete code,

(I have used two html files 1.html & 2.html, you can use any html files instead of it which can allows scrolling and put both files into asset folder)

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

public class DemoWebViewActivity extends Activity {

    private MyWebView mWebView;
    private WebViewClient client;

    private LinearLayout lymain;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lymain = new LinearLayout(getApplicationContext());
        lymain.setOrientation(1);

        mWebView = new MyWebView(getApplicationContext());
        mWebView.setWebViewClient(client);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.loadUrl("file:///android_asset/1.html");   

        lymain.addView(mWebView);
        setContentView(lymain);
    }

    class MyWebView extends WebView
    {
        Context context;
        GestureDetector gd;

        public MyWebView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub

            this.context = context;
            gd = new GestureDetector(context, sogl);
        }

        public boolean onTouchEvent(MotionEvent event) {

            return (gd.onTouchEvent(event) 
                    || super.onTouchEvent(event));
        };

        GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() {

            public boolean onDown(MotionEvent event) {

                  return true;
            }
            public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {

                   if (event1.getRawX() > event2.getRawX() && StrictMath.abs(event1.getRawY()-event2.getRawY())<100) {
                      mWebView.loadUrl("file:///android_asset/2.html");
                   } else if(event1.getRawX() < event2.getRawX() && StrictMath.abs(event1.getRawY()-event2.getRawY())<100){
                       mWebView.loadUrl("file:///android_asset/1.html");
                   } else {

                   }
                   return true;
            }
        };
    }
}

Any Help would be greatly appreciated. Thanks!


回答1:


try use a two arguments constructor like this:

public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    gd = new GestureDetector(context, sogl);
}


来源:https://stackoverflow.com/questions/9519559/handle-touch-events-inside-webview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!