Check if Android WebView is consuming touch events

后端 未结 4 844
臣服心动
臣服心动 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:33

    You can pass all touch events to GestureDetector by overriding onTouchEvent of WebView, so you can know when Android WebView is consuming touch events anywhere, anytime by listening to GestureDetector.

    Try like this:

    public class MyWebView extends WebView {
        private Context context;
        private GestureDetector gestDetector;
    
        public MyWebView(Context context) {
            super(context);
    
            this.context = context;
            gestDetector = new GestureDetector(context, gestListener);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return gd.onTouchEvent(event);
        }
    
        GestureDetector.SimpleOnGestureListener gestListener= 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()) {
                //    show_toast("swipe left");
                //} else {
                //    show_toast("swipe right");
                //}
                //you can trace any touch events here
                return true;
            }
        };
    
        void show_toast(final String text) {
            Toast t = Toast.makeText(context, text, Toast.LENGTH_SHORT);
            t.show();
        }
    }
    

    I hope you be inspired.

提交回复
热议问题