【问题标题】:Android: Handling touch events in the child, with parent sending (OnTouchListener)Android:处理子中的触摸事件,父发送(OnTouchListener)
【发布时间】:2016-06-06 14:13:47
【问题描述】:

在子对象中,我们需要跟踪坐标(ACTION_MOVE)。如果坐标为getX() > x,则必须向父对象发送事件。

问题:要将事件发送给父级我们需要返回false,但是如果返回false,我们将无法跟踪对象在子级中的坐标。

public boolean onTouch(View v,MotionEvent e) {
    if (e.getAction() == MotionEvent.ACTION_MOVE) {
        if (e.getY() > 200) {
            return false;  //we must send event to the parent object, but since then the ACTION_MOVE event no longer occurs here
        } else {
            myView.setTranslationY(e.getY());
            return true;   //we have to handle the event here
        }
    }
    return false;
}

我该怎么做才能正确处理这两种情况?

【问题讨论】:

    标签: android ontouchlistener


    【解决方案1】:

    您可以在子类中定义一个接口,它将由父类实现。当需要将事件发送到父对象时,调用接口方法,如下所示:

    儿童班:

         public boolean onTouch(View v,MotionEvent e){
        if(e.getAction()==MotionEvent.ACTION_MOVE){
            if(e.getY()>200){
               super.onTouchChildListener(v,e);
             }else{
              myView.setTranslationY(e.getY());
             }
              return true;
        }
        return false;
    }
    
    ...
    
     public interface OnChildListener {
        void onTouchChildListener(View v, MotionEvent e);
    }
    

    【讨论】:

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