跟踪 ViewGroup 中的 MotionEvent
我有一个扩展 ViewGroup
的类,并希望从中获取每个 MotionEvent
。到目前为止,我有这样的:
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;
}
}
每次我将手指放在屏幕上时, onTouchEvent(MotionEvent event)
方法都能够捕获 MotionEvent
。但是,如果我在手指仍然向下的情况下在屏幕上移动手指,它不会继续追踪我手指的坐标。我知道在扩展 View
的类中,可以在手指在 View
中移动时继续跟踪手指。我只是想知道如何将相同的想法应用于 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 apply the same idea to a ViewGroup
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我实现了一个非常简单的 ViewGroup 类并复制了您的代码,onTouchEvent 工作正常
我做的唯一不同的是实现所有构造函数,尽管一般来说我也会为 onTochEvent 类调用 super ,这似乎没有什么区别。
所以我想知道您是否遗漏了任何可能会产生影响的代码/xml?
捕获模拟器和设备上的所有触摸事件,与您的几乎一样,但构造函数除外。
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.
您应该添加
viewGroup.setClickable(true);
以确保视图可以接收更多触摸事件。you should add
viewGroup.setClickable(true);
to make sure the view can receive more touchevent.