Scroll View w/Volume Keys & PhoneGap?
HI, I have an @override that I use in native Android that scrolls the webView(scrollView)using the device volume keys. This is a real nice feature for the user when he/she is using one hand. I would like to use it with Phonegap. Obviously I cant use the: WebView scrollView = (WebView) findViewById(R.id.ch01);
So How can I use/implement feature with Phonegap or pass the function w/to jQuery??
Some code will be helpful - I am on a tight learn开发者_开发知识库ing curve with jQ & Phonegap. Thnx!
This is the override I use:
@Override
// --------------- Set the Device Volume Keys to Scroll the WebView -->
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
WebView scrollView = (WebView) findViewById(R.id.ch01);
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
scrollView.pageUp(false);
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
scrollView.pageDown(false);
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
The easiest, quickest way to do this would be to extend the onKeyDown method override in DroidGap.java. Add a few cases to the conditional, catching the volume keys, and using the same approach you see that exists in this method, you can directly invoke JavaScript by calling something like:
this.appView.loadUrl("javascript:alert('hello world!');");
Of course, you would probably change the alert
call to something else specific to your application, or perhaps even directly calling window.scrollBy
to do the scrolling.
A better, more modular approach would be to encapsulate this functionality into a PhoneGap plug-in. You could then contribute it back to the community as I'm sure others would appreciate and make use of this as well! ;)
精彩评论