Capturing Crosswalk Cordova webview touch events

坚强是说给别人听的谎言 提交于 2020-01-02 07:16:09

问题


I'm building a web based Android app using the Crosswalk Cordova framework, and I'm trying to capture any touch events that occur on the XWalk webview within the main activity of my app, but so far everything I've tried I can't get the touch events to be triggered during debugging sessions.

Specifically I'm trying to capture a three finger swipe down anywhere on the web view so I can display a settings pane.

Here is what I've got so far in my main activity:

public class MyActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        super.loadUrl(Config.getStartUrl());

        // appView is the main xwalk webview setup in parent
        appView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                // Check for three finger swipe... 
            }
        });
    }

    ....
}

I realise I could probably catch the touch event through javascript events that could then trigger the action through a cordova api call, but I'd much rather have this logic within the android app.

I've also tried capturing touch events in onTouchEvent method

public boolean onTouchEvent(View view, MotionEvent event) {
    int action = event.getAction();
    // Check for three finger swipe... 
}

But again this method is never triggered.

Any help or ideas would be much appreciated.


回答1:


I managed to get this working by overriding the dispatchTouchEvent method defined in Activity class. This was capturing the touch event and delegating them to my cordova webview without ever firing my onTouchEvent.

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    // handle three finger swipe

    super.dispatchTouchEvent(ev);
}

I hope this may help someone facing this issue :)



来源:https://stackoverflow.com/questions/28229155/capturing-crosswalk-cordova-webview-touch-events

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