Android的检测枪王没有单一的水龙头第一 [英] Android detecting double tap without single tap first

查看:125
本文介绍了Android的检测枪王没有单一的水龙头第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测双击,但没有第一次触发一个水龙头。我试过双击听众,但你总能得到一个onSingleTapUp双重检测之前,这是合理的我猜。但在我的应用程序,我真的不希望一次点击回调时,有一个双击的方式。

i would like to detect a double tap BUT without triggering a single tap first. i've tried double click listeners but you always get a onSingleTapUp before the double is detected, which is reasonable i guess. but in my app, i really don't want the single click callback when there is a double click on the way.

我意识到没有应用程序可以predict未来(或者我会很丰富),但我想,只要启动一个计时器上的一次点击,如果有一些时间内无双击,然后做单一的点击。但是这似乎并没有工作,因为一旦我开始定时器,定时器运行,第二自来水从来没有产生一个事件。这里是我使用的是aync任务code,我也试图与一个计时器。

i realize no app can predict the future (or i would be really rich) but i was thinking, just launch a timer on the single click and if there is no double click within some time out, then do the single click. but that doesn't seem to work because once i start the timer and the timer is running, the second tap never generates an event. here's my code using an aync task, i also tried it with a timer.

mGD = new GestureDetector(getContext(), new SimpleOnGestureListener() {

            @Override
            public boolean onSingleTapUp(MotionEvent ev) {
                //Log.d("KitView", "onSingleTapUp "+ev.toString());

                class DblTapTask extends AsyncTask<Void, Void, Void> {

                    @Override
                    protected void onPreExecute() {
                        Log.d("KitView", "onPreExecute");
                    }

                    protected Void doInBackground(Void... args) {
                        Log.d("KitView", "doInBackground");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        return null;
                    }

                    protected void onPostExecute(Void result) {
                        Log.d("KitView", "onPostExecute");
                                            doSigleTapAction();
                        mDblTapTask=null;
                    }
                };

                if(mDblTapTask==null) {
                    Log.d("KitView", "START TASK");
                    mDblTapTask = new DblTapTask();
                    mDblTapTask.execute();
                } else {
                    Log.d("KitView", "TASK RUNNING");
                                    doDoubleTapAction();
                }


                Log.d("KitView", "onSingleTapUp DONE");
                return true;
            }   // end onSingleTapUp

        });     // end new GestureDetector

在这个例子中,我得到onSingleTapUp蛮好的第一次敲击。我的超时时间为1秒,测试在doInBackground,所以我有很多时间做第二次敲击。注意,Log.d(KitView,onSingleTapUp DONE);立即运行我做的第一抽头,因此onSingleTapUp不挂。

in this example, i get onSingleTapUp just fine on the first tap. my timeout is 1 sec for testing in the doInBackground so i have lots of time to do the second tap. note that Log.d("KitView", "onSingleTapUp DONE"); runs immediately i do the first tap, so onSingleTapUp is not hanging.

问题是,在1秒内再次录音什么都不做。没有调用onSingleTapUp后睡眠已经完成,onPostExecute已运行到。所以Log.d(KitView,任务中运行);从未发生过这是我将检测双击与单一的点击,当然。

the problem is, taping again within the 1sec does nothing. there is no call to onSingleTapUp until after the sleep has finished and onPostExecute has run. so Log.d("KitView", "TASK RUNNING"); never happens which is where i would detect double click vs. single click of course.

我不知道为什么aynctask阻塞事件,任何想法?谢谢

i'm not sure why aynctask is blocking events, any ideas? thanks

推荐答案

我很困惑你试图达到的目标。枪王方法 onDoubleTap()当单一的水龙头出现不叫。在另一方面,为每一位双击有两个相关的单一的水龙头 onSingleTapUp()方法调用。

I'm confused on what you're trying to achieve. The double tap method onDoubleTap() isn't called when the single tap occurs. On the other hand, for every double tap there are two associated single tap onSingleTapUp() method calls.

如果你想区分它们,你可以使用 onSingleTapConfirmed()被触发时的手势检测是确保自来水的的单曲。见参考文献:

If you want to distinguish them, you can use onSingleTapConfirmed() which is triggered when the gesture detector is sure the tap is single. See reference:

在一个单一的抽头发生通知。

Notified when a single-tap occurs.

OnGestureListener.onSingleTapUp(MotionEvent),这将只能被称为后,探测器确信后面没有第二次敲击导致双重用户的第一次敲击抽头手势

Unlike OnGestureListener.onSingleTapUp(MotionEvent), this will only be called after the detector is confident that the user's first tap is not followed by a second tap leading to a double-tap gesture

您可以使用这个方法调用组合与布尔标志基本上检测到任何类型的单/双击。

You can then use this method call in combination with a boolean flag to essentially detect any type of single/double tap.

这篇关于Android的检测枪王没有单一的水龙头第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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