onInterceptTouchEvent没有收到ACTION_MOVE [英] onInterceptTouchEvent never receives action_move

查看:281
本文介绍了onInterceptTouchEvent没有收到ACTION_MOVE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有onInterceptTouchEvent的覆盖(自定义的ViewGroup)。它接收ACTION_DOWN但从来没有收到ACTION_MOVE。这是我的理解是,除非它返回真,就应该接收所有MotionEvents。

I have a custom ViewGroup with an override of onInterceptTouchEvent(). It receives ACTION_DOWN but never receives ACTION_MOVE. It is my understanding that, unless it returns "true", it should receive all MotionEvents.

该ViewGroup中包含两个观点,一个ImageView的和网格布局。

The ViewGroup contains two views, an ImageView and a GridLayout.

我的拦截code是:

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev)
  {
    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK)
    {
      case MotionEvent.ACTION_DOWN:
        logD ("DDV Intercept DOWN");
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        logD ("DDV Intercept P DOWN"); // logD: shell around Log.d()
        break;
      case MotionEvent.ACTION_MOVE:
        logD ("DDV Intercept MOVE");
        break;
      case MotionEvent.ACTION_UP:
        logD ("DDV Intercept UP");
        break;
      case MotionEvent.ACTION_POINTER_UP:
        logD ("DDV Intercept P UP " + ev.getActionIndex());
        break;
      case MotionEvent.ACTION_CANCEL: 
        logD ("DDV Intercept CANCEL");
        break;
      default:
        logD ("DDV Intercept " + (action & MotionEvent.ACTION_MASK));
    }        
    return false;
  }

我也有onTouch code返回除了在ACTION_MOVE一例虚假的;然而,这就是所谓的只为ACTION_DOWN被调用;因此它只返回false。

I also have code for onTouch that returns false except for one case in ACTION_MOVE; however, it's called only for ACTION_DOWN is called; thus it only return false.

推荐答案

这是比这更复杂一些。首先,你需要重写 onTouchEvent()和处理有 ACTION_DOWN 移动事件了。然后发生以下情况。

It's a bit more complicated than that. First of all you need to override onTouchEvent() and handle there ACTION_DOWN and MOVE events too. Then the following happens.


  1. ACTION_DOWN 事件被分派到 onInterceptTouchEvent()第一。您应该返回从那里。

  2. 现在,有两种情况:

    • 如果有下 ACTION_DONW 视图树事件的位置,然后 ACTION_DOWN 事件都遵循没有触及的看法达事件被分派到 onTouchEvent()。你必须返回真正从那里。只有这样,你会收到跟进发送到 onTouchEvent()方法事件。独立您是否返回真正 onInterceptTouchEvent()将不会收到任何跟进事件了。

    • 如果有一个可触摸视图,则所有事件将被分派到 onInterceptTouchEvent()(包括 ACTION_MOVE 事件)。你需要返回真正从那里,当你发现你的姿态。一旦你返回真正从这里开始,可触摸视图,将获得 ACTION_CANCEL 事件和接下来的所有事件将被分派到 onTouchEvent()方法。

  1. ACTION_DOWN event gets dispatched to onInterceptTouchEvent() first. You should return false from there.
  2. Now there are two cases:
    • If there is no touchable view underneath ACTION_DONW event's location in the view tree, then ACTION_DOWN event and all follow up events get dispatched to onTouchEvent(). You must return true from there. Only then you will receive follow up events sent to onTouchEvent() method. Independently on whether you return true or false, onInterceptTouchEvent() will not receive any follow up events anymore.
    • If there is a touchable view, then all events will be dispatched to onInterceptTouchEvent() (including ACTION_MOVE events). You need to return true from there, when you detect your gesture. Once you return true from here, touchable view will will receive ACTION_CANCEL event and all further events will be dispatched to onTouchEvent() method.

希望这有助于。

这篇关于onInterceptTouchEvent没有收到ACTION_MOVE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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