Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:处理子系统中的触摸事件,父系统发送(OnTouchListener)_Android_Ontouchlistener - Fatal编程技术网

Android:处理子系统中的触摸事件,父系统发送(OnTouchListener)

Android:处理子系统中的触摸事件,父系统发送(OnTouchListener),android,ontouchlistener,Android,Ontouchlistener,在子对象中,我们需要跟踪坐标(动作\移动)。如果坐标getX()>x,我们必须将事件发送到父对象 问题:要将事件发送到父级,我们需要返回false,但如果返回false,我们将无法跟踪子级中对象的坐标 public boolean onTouch(View v,MotionEvent e) { if (e.getAction() == MotionEvent.ACTION_MOVE) { if (e.getY() > 200) { retur

在子对象中,我们需要跟踪坐标(动作\移动)。如果坐标
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;
}

如何正确处理这两种情况?

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

儿童班:

     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);
}