一旦视图返回肯定地说,它消耗的触摸事件,意见其余部分将不接收它。你可以做的是让一个自定义ViewGroup
(你说你有一个网格,我只是假设GridView
?),其截取和处理所有触摸事件:
public class InterceptingGridView extends GridView {
private Rect mHitRect = new Rect();
public InterceptingGridView (Context context) {
super(context);
}
public InterceptingGridView (Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent (MotionEvent ev) {
//Always let the ViewGroup handle the event
return true;
}
@Override
public boolean onTouchEvent (MotionEvent ev) {
int x = Math.round(ev.getX());
int y = Math.round(ev.getY());
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.getHitRect(mHitRect);
if (mHitRect.contains(x, y)) {
/*
* Dispatch the event to the containing child. Note that using this
* method, children are not guaranteed to receive ACTION_UP, ACTION_CANCEL,
* or ACTION_DOWN events and should handle the case where only an ACTION_MOVE is received.
*/
child.dispatchTouchEvent(ev);
}
}
//Make sure to still call through to the superclass, so that
//the ViewGroup still functions normally (e.g. scrolling)
return super.onTouchEvent(ev);
}
}
你选择如何处理该事件依赖于你需要的逻辑,但是外包是让容器视图消耗所有触摸事件,并让它处理将事件分派给子项。
我不完全确定这是做什么,因为大多数术语我以前没有见过。我是Android和Java的新手(之前只使用过C++),我的歉意! – Dani 2013-05-01 17:01:29
看看[这个链接](http://developer.android.com/training/gestures/viewgroup.html) – kcoppock 2013-05-01 17:07:49
哦,男孩,这需要一段时间才能搞清楚。谢谢你! – Dani 2013-05-01 19:48:20