为什么我总是从onTouchEvent中得到ACTION_DOWN?

9
我将我的类从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);
}

1
请展示你的代码,否则我们只能瞎猜。谢谢! - user658042
3个回答

31

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


1
在工作中,我们有一个名为teaser的LinearLayout存在类似问题(已经有一段时间了)。总之,我们发现这个“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

说实话,我无法给你任何合理的解释,但希望这能有所帮助。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接