Ontouchlistener Motionevent.action_move: Gets Only First View
I have a grid of buttons in my activity. When the user slides their finger across this grid of buttons, I want all of the buttons touched to be essentially recorded so I know which
Solution 1:
Once your View returns true to say that it's consuming the touch event, the rest of the views won't receive it. What you can do is make a custom ViewGroup (you say you have a grid, I'll just assume GridView?) that intercepts and handles all touch events:
publicclassInterceptingGridViewextendsGridView {
privateRectmHitRect=newRect();
publicInterceptingGridView(Context context) {
super(context);
}
publicInterceptingGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OverridepublicbooleanonInterceptTouchEvent(MotionEvent ev) {
//Always let the ViewGroup handle the eventreturntrue;
}
@OverridepublicbooleanonTouchEvent(MotionEvent ev) {
intx= Math.round(ev.getX());
inty= Math.round(ev.getY());
for (inti=0; i < getChildCount(); i++) {
Viewchild= 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)returnsuper.onTouchEvent(ev);
}
}
How you choose to handle the event depends on the logic that you require, but the takeaway is to let the container view consume all of the touch events, and let it handle dispatching the events to the children.
Solution 2:
maybe this will help you:
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
int pointerId = event.getPointerId(pointerIndex);
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_MOVE:
int pointerCount = event.getPointerCount();
for (int i = 0; i < pointerCount; i++) {
}
break;
}
returntrue;
}
it works for multitouch
Post a Comment for "Ontouchlistener Motionevent.action_move: Gets Only First View"