简体   繁体   English

android将事件从子视图传递到父视图

[英]android passing events from child view to parent

I have two custom views Child (Relative Layout) and Parent (FrameLayout). 我有两个自定义视图Child(相对布局)和Parent(FrameLayout)。 When a single tap occurs, I would like to have the child process the event but then still pass it further to the Parent and for any other event in the Child simply pass it further without processing. 当一次点击发生时,我想让孩子处理事件但是仍然将其进一步传递给Parent,而对于Child中的任何其他事件,只需将其进一步传递而不进行处理。

The child view is set to match_parent. 子视图设置为match_parent。

This is what I have so far: 这是我到目前为止:

public class Child extends RelativeLayout implements View.OnTouchListener {
  private class GestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // process the event
        return true;
    }
  }

  public boolean onTouch(View view, MotionEvent motionEvent) {
    return true;
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent e) {
    gestrDetect_.onTouchEvent(e);
    return super.dispatchTouchEvent(e);
  }
}


public class Parent extends FrameLayout {
  public Parent(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOnTouchListener(this);
  }

  @Override
  public boolean onTouch(View view, MotionEvent event) {
    return gestrDetect_.onTouchEvent(event);
  }      
}

While I am getting the single tap up events in the child view, all the other events like fling or scale are not passed to the parent. 当我在子视图中获得单个点击事件时,所有其他事件(如fling或scale)都不会传递给父级。 What is the correct way to handle a certain subset of events in child, while still pass some of them to the parent? 处理子节点中某个事件子集的正确方法是什么,同时仍将其中一些事件传递给父节点?

Edit 编辑

I have change the code to the following: 我已将代码更改为以下内容:

public class Child extends RelativeLayout implements View.OnTouchListener {
  private class GestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(android.view.MotionEvent e1, android.view.MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // process the event
        return true;
    }
  }

  public boolean onTouch(View view, MotionEvent motionEvent) {
    return gestrDetect_.onTouchEvent(e);
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent e) {        
    return super.dispatchTouchEvent(e);
  }
}


public class Parent extends FrameLayout {
  @Override
  public boolean onTouch(View view, MotionEvent event) {
    return gestrDetect_.onTouchEvent(event);
  }      
}

And the events are still not passed to the Parent. 事件仍然没有传递给父母。 When I change 当我改变

@Override
public boolean onDown(MotionEvent e) {
    return true;
}

to

return false;

I am not getting even the singleTapUp event in the child. 我甚至没有得到孩子的singleTapUp事件。

You need to create your own implementation of the parent ViewGroup and override onInterceptTouchEvent() method (check out this tutorial http://developer.android.com/training/gestures/viewgroup.html ). 您需要创建自己的父ViewGroup实现并覆盖onInterceptTouchEvent()方法(请参阅本教程http://developer.android.com/training/gestures/viewgroup.html )。

With this you can determine with methods should be handled by parent and which will be passed down to child. 有了这个你可以确定方法应该由父处理,哪些将传递给子。

Thing that has worked for me. 这对我有用。

/**
 * If set to true the layout will discard all received {@link MotionEvent}'s .
 * As a result none of the children will receive the touch event, instead the parent view will.
 */
public void setPassTouchEventsUp(final boolean passTouchEventsUp) {
    mPassTouchEventsUp = passTouchEventsUp;
}

@Override
public boolean onInterceptTouchEvent(final MotionEvent ev) {
    if (mPassTouchEventsUp) {
        return true;
    } else {
        return super.onInterceptTouchEvent(ev);
    }
}

@Override
public boolean onTouchEvent(final MotionEvent event) {
    if (mPassTouchEventsUp) {
        return false;
    } else {
        return super.onTouchEvent(event);
    }
}

When I call setPassTouchEventsUp(true) the ViewGroup will simply not handle any MotionEvent 's, notifying its parent view that the events must be processed by superior views/layouts. 当我调用setPassTouchEventsUp(true) ,ViewGroup将不会处理任何MotionEvent ,通知其父视图必须由高级视图/布局处理事件。 This way events "fly through" this ViewGroup up to its parent or whatever there is underneath it. 通过这种方式,事件可以“飞过”此ViewGroup直到其父级或其下方的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Android 中将变量从父视图传递给子视图 - Passing variables from Parent to Child views in Android 将“事件”从子选项卡内容活动传递到父选项卡活动 - Passing “events” from child tab content Activity to parent TabActivity Android:从视图传递触摸事件 - Android: Passing Touch Events from View 将触摸事件传递给父视图 - Passing touch events to the parent view 如何从Android的父视图中的自定义子视图中获取价值 - how to get value from custom child view in parent view in android android传递在视图剪辑边界之外绘制的子视图的触摸事件(ViewPager第1页到第2页) - android Passing touch events of a Child view drawn outside of view clip bounds (ViewPager Page 1 to Page 2) Android:将数据从子片段传递到父片段 - Android: Passing data from child fragment to parent fragment 数据从父片段传递到选项卡式片段(子)-Android - Data Passing from Parent Fragment to Tabbed Fragment (child) - Android Android:其父视图之外的子视图不响应点击事件 - Android: child view outside of its parent doesn't respond to click events 从在 Android 中以编程方式创建的父视图中查找子视图 - Finding child views from parent view created programmatically in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM