一扔手势和web视图Android中视图、手势、web、Android

2023-09-12 03:29:35 作者:薄涼之人怎能溫暖他人i

我有一个需要支持的Andr​​oid一扔姿态,为用户带来了一个新的记录(加载新数据)一个WebView控件。这是发生在扩展活动的类。所有例子我见过展示了如何实现一个TextView手势支持,但没有对web视图。

我需要执行对左,右甩不同的动作。任何code帮助将是pciated因为这AP $ P $完全有我难住了。

下面是我的基本的onCreate和我的课

 进口android.app.Activity;
进口android.content.Intent;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.os.Bundle;
进口android.text.Html;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.Window;

进口android.webkit.WebView;

公共类ArticleActivity延伸活动{

公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);



    窗口瓦特= getWindow();

     w.requestFeature(Window.FEATURE_LEFT_ICON);

     的WebView的WebView =新的WebView(本);
     的setContentView(web视图);



     w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
     R.drawable.gq);




    setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
    populateFields();
    webview.loadData(问题+答案text / html的,UTF-8);



  //
}
私人无效populateFields(){

....


}

}
 

解决方案

创建GestureListener和GestureDetector。通过覆盖的WebView的的onTouchEvent调用GestureDetector.onTouchEvent。

西门子S7 1200 1500与精简面板仿真的详细图解

您也可以只覆盖活动的onTouchEvent BTW。如果你需要我可以张贴一些code。

编辑:code的要求

 公共类主要扩展活动{

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        MyWebView的WebView =新MyWebView(本);
        的setContentView(web视图);
    }

    类MyWebView扩展的WebView {
        上下文语境;
        GestureDetector的gd;

        公共MyWebView(上下文的背景下){
            超(上下文);

            this.context =背景;
            GD =新GestureDetector(背景下,sogl);
        }

        @覆盖
        公共布尔的onTouchEvent(MotionEvent事件){
            返回gd.onTouchEvent(事件);
        }

        GestureDetector.SimpleOnGestureListener sogl =新GestureDetector.SimpleOnGestureListener(){
            公共布尔onDown(MotionEvent事件){
                返回true;
            }

            公共布尔onFling(MotionEvent EVENT1,MotionEvent EVENT2,浮velocityX,浮velocityY){
                如果(event1.getRawX()> event2.getRawX()){
                    show_toast(刷卡左);
                } 其他 {
                    show_toast(向右滑动);
                }
                返回true;
            }
        };

        无效show_toast(最终文本字符串){
            吐司T = Toast.makeText(背景,文本,Toast.LENGTH_SHORT);
            t.show();
        }
    }
}
 

@littleFluffyKitty。我presume默认的WebView触摸事件时,它带来了缩放控件等,你是什么意思?我没有测试。我发现,那些实现自己的姿态检测效果最好(不知道是否会工作最好的一个的WebView虽然)。你需要看一个触摸事件为三个不同的组成部分。在preSS下来,动作(如果有的话),和preSS发布,作为preSS向下,移动,释放总是发生。

如果你做一回假的onDown的行动应该得到传递给web视图触摸事件的处理程序,但IIRC它停止了后续事件传递给GestureDetector。这是成功的一半,为什么我实现我自己的是基于Android操作系统的原因。 IIRC我得到了来自索尼爱立信教程是可下载从市场的想法。这是3D列表,其中显示了code和pretty的容易适应。

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

....


}

}

解决方案

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.

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