触摸时 ACTION_CANCEL
我有以下类,它代表一个可触摸的视图并绘制一个滑动条。
public class SlideBar extends View {
private int progress;
private int max;
private Paint background;
private Paint upground;
private RectF bar;
private boolean firstDraw;
public SlideBar(Context context, AttributeSet attrs) {
super(context, attrs);
progress = 0;
upground = new Paint();
upground.setColor(Color.parseColor("#C2296C"));
background = new Paint();
background.setColor(Color.parseColor("#777777"));
}
private void onFirstDraw() {
max = getWidth();
bar = new RectF(0, 19, max, 21);
}
public void onDraw(Canvas canvas) {
if (!firstDraw) {
onFirstDraw();
progress = max;
firstDraw = true;
}
canvas.save();
canvas.drawRoundRect(bar, 5, 5, background);
canvas.drawCircle(progress, 20, 9, upground);
canvas.restore();
}
public void setValue(int value) {
progress = value;
}
public boolean onTouchEvent(MotionEvent evt) {
System.out.println(evt.getAction());
progress = (int) evt.getX();
invalidate();
return false;
}
}
但是当触摸并拖动它时,我收到一个 ACTION_DOWN,一些 ACTION_MOVE 然后收到一个 ACTION_CANCEL 并且没有进一步的事件。
为什么会出现这样的情况呢?我不想取消该事件并使其继续拖动栏。
I has the following class that represents a View that is touchable and draw a Slide Bar.
public class SlideBar extends View {
private int progress;
private int max;
private Paint background;
private Paint upground;
private RectF bar;
private boolean firstDraw;
public SlideBar(Context context, AttributeSet attrs) {
super(context, attrs);
progress = 0;
upground = new Paint();
upground.setColor(Color.parseColor("#C2296C"));
background = new Paint();
background.setColor(Color.parseColor("#777777"));
}
private void onFirstDraw() {
max = getWidth();
bar = new RectF(0, 19, max, 21);
}
public void onDraw(Canvas canvas) {
if (!firstDraw) {
onFirstDraw();
progress = max;
firstDraw = true;
}
canvas.save();
canvas.drawRoundRect(bar, 5, 5, background);
canvas.drawCircle(progress, 20, 9, upground);
canvas.restore();
}
public void setValue(int value) {
progress = value;
}
public boolean onTouchEvent(MotionEvent evt) {
System.out.println(evt.getAction());
progress = (int) evt.getX();
invalidate();
return false;
}
}
But when touching and dragging it, I receive a ACTION_DOWN, some ACTION_MOVEs then receive a ACTION_CANCEL and no further events.
Why it's happens? I don't want to cancel the event and enable it to keep dragging bar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当父容器拦截您的触摸事件时,就会发生这种情况。任何覆盖 ViewGroup.onInterceptTouchEvent(MotionEvent) 的 ViewGroup可以做到这一点(例如 ScrollView 或 ListView)。
处理此问题的正确方法是调用 ViewParent.requestDisallowInterceptTouchEvent(boolean) 一旦您认为需要保留运动事件,就在您的父视图上使用方法。
这是一个简单的示例(attemptClaimDrag 方法取自 android 源代码):
This will happen when parent container will intercept your touch event. Any ViewGroup that overrides ViewGroup.onInterceptTouchEvent(MotionEvent) can do that (ScrollView or ListView for instance).
Proper way to deal with this is to call ViewParent.requestDisallowInterceptTouchEvent(boolean) method on your parent view once you think you need to keep the motion event.
Here's a quick example (attemptClaimDrag method is taken from android source code):
当父视图接管其子视图之一的控制权时,就会发生
ACTION_CANCEL
。查看有关 ViewGroup 的文档。 onInterceptTouchEvent(MotionEvent) 方法。从链接:
onTouchEvent()
方法来处理;这意味着您应该实现 onTouchEvent() 来返回 true,这样您将继续看到手势的其余部分(而不是寻找父视图来处理它)。此外,通过从onTouchEvent()
返回 true,您将不会在onInterceptTouchEvent()
中收到任何后续事件,并且所有触摸处理都必须在onTouchEvent()
中进行代码> 像平常一样。onTouchEvent()
。ACTION_CANCEL
,并且所有其他事件将传递到您的onTouchEvent( )
方法并且不再出现在这里An
ACTION_CANCEL
happens when a parent view takes over control of one of its children views.Take a look at the documentation around ViewGroup.onInterceptTouchEvent(MotionEvent) method. From the link:
onTouchEvent()
method to handle; this means you should implementonTouchEvent()
to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true fromonTouchEvent()
, you will not receive any following events inonInterceptTouchEvent()
and all touch processing must happen inonTouchEvent()
like normal.onTouchEvent()
.ACTION_CANCEL
, and all further events will be delivered to youronTouchEvent()
method and no longer appear here需要禁止父视图拦截触摸事件:
Need to disallow parent view to intercept the touch event:
触摸时Android ACTION_CANCEL
另一种替代方法是:
您有
ViewGroup
,其中包含View
。ViewGroup
的方法onInterceptTouchEvent()
对于ACTION_DOWN
返回false
,对于ACTION_DOWN
返回true
其他情况View
始终在onTouch
或onTouchEvent
中返回true
ACTION_DOWN< /代码>,
ACTION_MOVE
...因此,您将看到下一个流程
ACTION_DOWN
迭代:ACTION_MOVE
迭代:ACTION_MOVE
迭代:[Android 触摸事件]
Android ACTION_CANCEL while touching
Another alternative way it is:
You have
ViewGroup
withView
inside.ViewGroup
's methodonInterceptTouchEvent()
returnfalse
forACTION_DOWN
andtrue
for other casesView
always returnstrue
inonTouch
oronTouchEvent
ACTION_DOWN
,ACTION_MOVE
...As a result you will see the next flow
ACTION_DOWN
iteration:ACTION_MOVE
iteration:ACTION_MOVE
iteration:[Android Touch event]