问题
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