Check if Android WebView is consuming touch events

后端 未结 4 845
臣服心动
臣服心动 2021-01-31 13:59

Short version

How can I detect whether Android WebView consumed a touch event? onTouchEvent always returns true and WebViewClient\'s on

4条回答
  •  不知归路
    2021-01-31 14:11

    This code will handle your scrolling events in a webview. This catch the click down and the click up events, and compares the positions of each one. It never minds that the content within the webview is scrollable, just compare the coordinates in the area of webview.

    public class MainActivity extends Activity implements View.OnTouchListener, Handler.Callback {
    
        private float x1,x2,y1,y2; //x1, y1 is the start of the event, x2, y2 is the end.
        static final int MIN_DISTANCE = 150; //min distance for a scroll event
    
        private static final int CLICK_ON_WEBVIEW = 1;
        private static final int CLICK_ON_URL = 2;
        private static final int UP_ON_WEBVIEW = 3;
    
    
        private final Handler handler = new Handler(this);
    
        public WebView webView;
        private WebViewClient client;
        private WebAppInterface webAppInt = new WebAppInterface(this);
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            webView = (WebView)findViewById(R.id.myWebView);
            webView.setOnTouchListener(this);
    
            client = new WebViewClient();
            webView.setWebViewClient(client);        
            webView.loadDataWithBaseURL("file:///android_asset/", "myweb.html", "text/html", "UTF-8", "");
    
        }
    
    //HERE START THE IMPORTANT PART
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (v.getId() == R.id.myWebView && event.getAction() == MotionEvent.ACTION_DOWN){
                x1 = event.getX();
                y1 = event.getY();
                handler.sendEmptyMessageDelayed(CLICK_ON_WEBVIEW, 200);
            } else if (v.getId() == R.id.myWebView && event.getAction() == MotionEvent.ACTION_UP){
                x2 = event.getX();
                y2 = event.getY();
    
                handler.sendEmptyMessageDelayed(UP_ON_WEBVIEW, 200);
            }
    
            return false;
        }
    
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == CLICK_ON_URL){ //if you clic a link in the webview, thats not a scroll
                handler.removeMessages(CLICK_ON_WEBVIEW);
                handler.removeMessages(UP_ON_WEBVIEW);
                return true;
            }
            if (msg.what == CLICK_ON_WEBVIEW){
                //Handle the click in the webview
                Toast.makeText(this, "WebView clicked", Toast.LENGTH_SHORT).show();
                return true;
            }
            if (msg.what == UP_ON_WEBVIEW){
                float deltaX = x2 - x1; //horizontal move distance
                float deltaY = y2 - y1; //vertical move distance
                if ((Math.abs(deltaX) > MIN_DISTANCE) && (Math.abs(deltaX) > Math.abs(deltaY)))
                {
                    // Left to Right swipe action
                    if (x2 > x1)
                    {
                        //Handle the left to right swipe
                        Toast.makeText(this, "Left to Right swipe", Toast.LENGTH_SHORT).show ();
                    }
    
                    // Right to left swipe action
                    else
                    {
                        //Handle the right to left swipe
                        Toast.makeText(this, "Right to Left swipe", Toast.LENGTH_SHORT).show ();
                    }
    
                }
                else if ((Math.abs(deltaY) > MIN_DISTANCE) && (Math.abs(deltaY) > Math.abs(deltaX)))
                {
                    // Top to Bottom swipe action
                    if (y2 > y1)
                    {
                        //Handle the top to bottom swipe
                        Toast.makeText(this, "Top to Bottom swipe", Toast.LENGTH_SHORT).show ();
                    }
    
                    // Bottom to top swipe action -- I HIDE MY ACTIONBAR ON SCROLLUP
                    else
                    {
                        getActionBar().hide();
                        Toast.makeText(this, "Bottom to Top swipe [Hide Bar]", Toast.LENGTH_SHORT).show ();
                    }
                }
                return true;
            }
            return false;
        }
    }
    

    You can also try to control the speed of the swipe, to detect it as a real swipe or scrolling.

    I hope that helps you.

提交回复
热议问题