Android 中的 Fling 手势和 Webview [英] Fling Gesture and Webview in Android

查看:13
本文介绍了Android 中的 Fling 手势和 Webview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 webview 控件,它需要支持 Android 中的投掷手势,以便调出新记录(加载新数据).这发生在扩展 Activity 的类中.我见过的所有示例都展示了如何为 textview 实现手势支持,但对于 webview 则没有.

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.

这是我的基本 onCreate 和我的课程

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() {

....


}

}

推荐答案

创建一个 GestureListener 和一个 GestureDetector.通过覆盖 webview 的 onTouchEvent 调用 GestureDetector.onTouchEvent.

Create a GestureListener and a GestureDetector. Call the GestureDetector.onTouchEvent by overriding the webview's onTouchEvent.

顺便说一句,您也可以只覆盖 Activity onTouchEvent.如果您需要,我可以发布一些代码.

You can also just override the Activity onTouchEvent btw. I can post some code if you need.

按要求编写代码.

public class Main extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MyWebView webview = new MyWebView(this);
        setContentView(webview);
    }

    class MyWebView extends WebView {
        Context context;
        GestureDetector gd;

        public MyWebView(Context context) {
            super(context);

            this.context = context;
            gd = new GestureDetector(context, sogl);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return gd.onTouchEvent(event);
        }

        GestureDetector.SimpleOnGestureListener sogl = 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");
                }
                return true;
            }
        };

        void show_toast(final String text) {
            Toast t = Toast.makeText(context, text, Toast.LENGTH_SHORT);
            t.show();
        }
    }
}

@littleFluffyKitty.我认为默认情况下 WebView 触摸事件是指当它显示缩放控件等时?我没有测试那个.我发现实现自己的手势检测效果最好(但不确定它是否在 WebView 上效果最好).您需要将触摸事件视为三个不同的组件.按下、移动(如果有)和按下释放,随着按下、移动、释放总是发生.

@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.

如果您在 onDown 上执行 return false,则该操作应该被传递给 WebView 触摸事件处理程序,但 iirc 它会停止将后续事件传递给 GestureDetector.这是我实现自己的基于 Android 源代码的一半原因.iirc 我从索尼爱立信教程中得到了这个想法,该教程可以从市场上下载.它是显示代码的 3D 列表,而且很容易适应.

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.

这篇关于Android 中的 Fling 手势和 Webview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆