我有一个扩展 linearLayout 的自定义 ViewGroup,这个 View 组有一个 gridView 作为 subview 。我想如果 mGridView 单击 mGridView.onClickListener 调用并执行但在我的自定义 View 组(onTouchEvent() 执行)中处理其他触摸事件(如移动 mGridView)。所以我 ovveride onInterceptTouchEvent :
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
int y = (int) ev.getY();
if( action == MotionEvent.ACTION_DOWN && y > mGridView.getTop() ) //y > mGridView,getTop() checks if position of touch in mGridView Position or not.
return false;
return true;
}
但 mGridView.OnClickListener() 从未调用过。
最佳答案
点击事件是一个复合事件,它不仅仅包含一个 MotionEvent.ACTION_DOWN。
它应该有一个 MotionEvent.ACTION_DOWN,可能还有几个其他的 MotionEvent.ACTION_MOVE,最后是一个 MotionEvent.ACTION_UP,所有这些都在您点击的 View 的范围内。
您应该在方法 onInterceptTouchEvent(MotionEvent ev) 中定义其他 MotionEvent 以“返回 false”。 也许是这样的:
if( (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_DOWNaction == MotionEvent.ACTION_MOVE || action == MotionEvent.ACTION_UP) && y > mGridView.getTop() )
来自 this 的应用你可能已经读过,看看Detecting Common Gestures
或者(但不是真的建议)尝试不在您的 View 上使用 OnClickListener,而是使用 OnTouchListener。使用您当前的代码,您应该在那里收到您现在返回 false 的 MotionEvent.ACTION_DOWN
关于android - 在父 View 中处理触摸但在 subview 中处理单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28282255/