接收onTouch和的onClick事件与Android [英] Receiving onTouch and onClick events with Android

查看:141
本文介绍了接收onTouch和的onClick事件与Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要处理onTouch手势的的的onClick事件的视图。什么是正确的方式来实现这一目标?

I have a view that need to process onTouch gestures and onClick events. What is the proper way to achieve this?

我有一个 onTouchListener onClickListener 的视图设置。每当我接触来看,第一个 onTouch 触发事件以及后来的的onClick 。不过,从 onTouch 事件处理我要回任。返回表示该事件被消耗,所以android的事件系统将不会继续传播事件。

I have an onTouchListener and an onClickListener set on the view. Whenever I do touch the view, first the onTouch event is triggered and later the onClick. However, from the onTouch event handler I have to return either true or false. Returning true means that the event is being consumed, so the android event system will not propagate the event any further.

因此​​,的onClick 事件从未发生,ATLEAST我的的onClick 监听器,当我返回永远不会触发在我的 onTouch 事件处理程序。在另一方面,返回有不是一个选项,因为这$ P $接收pvents的 onTouch 监听器任何进一步的事件是必要的,以便识别手势。什么是解决这一通常的方法是什么?

Therefore, an onClick event is never generated, atleast my onClick listener is never triggered when I return true in my onTouch event handler. On the other hand, returning false there is not an option, since this prevents the onTouch listener from receiving any further events that are necessary in order to recognize a gesture. What's the usual way of solving this?

推荐答案

在你GestureDetector,你可以调用callOnClick()直接。 注意View.callOnClick API需要API级别15。 只要有一个尝试。

In you GestureDetector, you can call callOnClick() directly. Note the View.callOnClick API requires API level 15. Just have a try.

 // Create a Gesturedetector
GestureDetector mGestureDetector = new GestureDetector(context, new MyGestureDetector());

// Add a OnTouchListener into view
m_myViewer.setOnTouchListener(new OnTouchListener()
{

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        return mGestureDetector.onTouchEvent(event);
    }
});

private class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
{
    public boolean onSingleTapUp(MotionEvent e) {
        // ---Call it directly---
        callOnClick();
        return false;
    }

    public void onLongPress(MotionEvent e) {
    }

    public boolean onDoubleTap(MotionEvent e) {
        return false;
    }

    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }

    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;

    }

    public void onShowPress(MotionEvent e) {
        LogUtil.d(TAG, "onShowPress");
    }

    public boolean onDown(MotionEvent e) {            
        // Must return true to get matching events for this down event.
        return true;
    }

    public boolean onScroll(MotionEvent e1, MotionEvent e2, final float distanceX, float distanceY) {
        return super.onScroll(e1, e2, distanceX, distanceY);
    }        

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // do something
        return super.onFling(e1, e2, velocityX, velocityY);
    }
}

这篇关于接收onTouch和的onClick事件与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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