【问题标题】:why I always get ACTION_DOWN from onTouchEvent为什么我总是从 onTouchEvent 得到 ACTION_DOWN
【发布时间】:2012-01-12 04:59:35
【问题描述】:

我从 View 扩展我的课程。我重写了 onTouchEvent 函数。但是,我只能得到 ACTION_DOWN 事件。不是任何 ACTION_UP 或 ACTION_MOVE。我应该怎么做才能获得这些事件?

我只覆盖 onTouchEvent 和 onDraw 函数。并且应用中只有这个视图

@Override
public boolean onTouchEvent(MotionEvent event) {
    System.out.println("event:"+event.getAction());
    return super.onTouchEvent(event);
}

【问题讨论】:

  • 请出示您的代码。否则我们只能猜测。谢谢!

标签: android touch


【解决方案1】:

您可能需要返回 true 而不是返回 super 事件。这意味着您处理了该事件,因此您可以接收下一个事件。

【讨论】:

    【解决方案2】:

    在工作中,我们有一个名为teaserLinearLayout 遇到了类似的问题(已经有一段时间了)。无论如何,我们发现这种“hack”似乎有助于从touch 中获得一些额外的响应。

    teaser.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent motionEvent) {
            TransitionDrawable transition = (TransitionDrawable) v.getBackground();
    
            switch(motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if(transition != null) transition.startTransition(250);
                    break;
                case MotionEvent.ACTION_UP:
                    loadArticle(position);
                case MotionEvent.ACTION_CANCEL:
                    if(transition != null) transition.reverseTransition(0);
                    break;
            }
            return false;
        }
    });
    
    // Has to be here for the touch to work properly.
    teaser.setOnClickListener(null); // <-- the important part for you
    

    老实说,我无法给你任何押韵或理由,但希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      这篇文章可能会帮助您了解发生了什么: http://rxwen.blogspot.com/2010/07/implement-drag-and-drop-on-android.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多