Android 中的 Fling 手势和 Webview
我有一个 webview 控件,需要支持 Android 中的 fling 手势,以便调出新记录(加载新数据)。这是在扩展 Activity 的类中发生的。我见过的所有示例都展示了如何为文本视图实现手势支持,但没有为网络视图实现手势支持。
我需要对左右滑动执行不同的操作。任何代码帮助将不胜感激,因为这完全让我难住了。
这是我的基本 onCreate 和我的课程
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
public class ArticleActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window w = getWindow();
w.requestFeature(Window.FEATURE_LEFT_ICON);
WebView webview = new WebView(this);
setContentView(webview);
w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
R.drawable.gq);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
populateFields();
webview.loadData(question + answer, "text/html", "utf-8");
//
}
private void populateFields() {
....
}
}
I have a webview control that needs to support the fling gesture in Android in order to bring up a new record (load new data). This is occuring in a class that extends Activity. All the examples I've seen show how to implement gesture support for a textview, but nothing for webview.
I need to execute different actions for both left and right flings. Any code help would be appreciated as this totally has me stumped.
Here's my basic onCreate and my class
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
public class ArticleActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window w = getWindow();
w.requestFeature(Window.FEATURE_LEFT_ICON);
WebView webview = new WebView(this);
setContentView(webview);
w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
R.drawable.gq);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
populateFields();
webview.loadData(question + answer, "text/html", "utf-8");
//
}
private void populateFields() {
....
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建一个 GestureListener 和一个 GestureDetector。通过覆盖 webview 的 onTouchEvent 来调用 GestureDetector.onTouchEvent。
顺便说一句,您也可以覆盖 Activity onTouchEvent。如果您需要,我可以发布一些代码。
编辑:按要求编码。
@littleFluffyKitty。我认为默认情况下 WebView 触摸事件是指当它调出缩放控件等时?我没有测试过。我发现实现自己的手势检测效果最好(但不确定它是否在 WebView 上效果最好)。您需要将触摸事件视为三个不同的组件。按下、移动(如果有)和按下释放,因为按下、移动、释放总是发生。
如果您在 onDown 上返回 false,则该操作应向下传递到 WebView 触摸事件处理程序,但 iirc 它会停止将后续事件传递到 GestureDetector。这就是我实现自己的基于 Android 源代码的一半原因。 iirc 我从索尼爱立信教程中得到了这个想法,该教程可以从市场上下载。它是显示代码的 3D 列表,并且非常容易修改。
Create a GestureListener and a GestureDetector. Call the GestureDetector.onTouchEvent by overriding the webview's onTouchEvent.
You can also just override the Activity onTouchEvent btw. I can post some code if you need.
Edit: Code as requested.
@littleFluffyKitty. I presume by default WebView touch events you mean when it brings up the zoom controls etc? I didn't test that. I have found that implementing ones own gesture detection works best (not sure if it would work best on a WebView though). You need to look at a touch event as three distinct components. The press down, the movement (if any), and the press release, as the press down, move, release always happen.
If you do a return false on the onDown the action should get passed down to the WebView touch event handler but iirc it stops the subsequent events being passed to the GestureDetector. Which is half the reason why I implement my own which is based on the Android source. iirc I got the idea from the Sony Ericsson Tutorials which is downloadable off the market. It's the 3D list which shows the code and its pretty easy to adapt.
我更新了代码,如果用户没有 flinged,现在将允许调用本机事件处理程序
I updated the code, this will now allow to call native events handlers if user didn't flinged
@Han,请原谅@techiServices 他不必要的回答。
上面代码的问题在于,在所有情况下,它都会为 onFling 和 onDown 返回 true。相反,您想要的是对于您不处理的事件或您不处理的事件中的条件返回 false。或者实际上,在 onTouchEvent 中,您可以通过返回来将调用传递给基类
@Han, forgive @techiServices his uncalled-for answer.
The problem with the code above is that in all cases it returns true for onFling and onDown. What you want instead is to return false for the events that you don't handle, or for conditions in those events that you don't handle. Or indeed, in onTouchEvent you could pass the call to the base class by returning