Tracing a MotionEvent in a ViewGroup
I have a class extends ViewGroup
and want to get every MotionEvent
from it. So far I have this:
class TestViewGroup extends ViewGroup {
public TestViewGroup(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("TestViewGroup", "X: " + (int)event.getX() + " Y: " + (int)event.getY());
return true;
}
}
The onTouchEvent(MotionEvent event)
method is able to capture a MotionEvent
every time I place my finger on the screen. But if I move my finger around the screen while my finger is still down, it won't continue to trace the coordinates of my finger. I know that in a class that extends a View
, it is possible to keep tracing the finger as it moves through the View
. I'm just wondering how it is possible to开发者_StackOverflow中文版 apply the same idea to a ViewGroup
.
I implemented a very simple ViewGroup class and copied your code, the onTouchEvent worked fine
The only thing I did differently was implement all of the constructors, though in general I would also call the super for the onTochEvent class that did not seem to make a difference.
So I am wondering if there is any code/xml you are leaving out that might make a difference?
Captures all touch events in emulator and on device, pretty much just like yours with the exception of the constructors.
public class customViewGroup extends ViewGroup {
public customViewGroup(Context context) {
super(context,null,0);
}
public customViewGroup(Context context, AttributeSet attrs) {
super(context, attrs,0);
}
public customViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("bThere", "X: " + (int)event.getX() + " Y: " + (int)event.getY());
return true;//super.onTouchEvent(event);
}
}
you should add viewGroup.setClickable(true);
to make sure the view can receive more touchevent.
精彩评论