Android 停止发送 onTouch 事件
我有一个自定义视图,我希望为其处理触摸事件。特别是,在视图上拖动手指应该会导致其内容滚动。为此,我实现了以下 onTouchListener:
private OnTouchListener graphOnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
System.out.println("onTouch called");
System.out.println("view width: " + v.getWidth());
System.out.println("view height: " + v.getHeight());
System.out.println("view: " + v);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// First finger down
System.out.println("ACTION_DOWN x: " + event.getX() + ", y: " + event.getY());
startTouch(event.getX());
break;
case MotionEvent.ACTION_UP:
// Last finger to be removed
System.out.println("ACTION_UP x: " + event.getX() + ", y: " + event.getY());
break;
case MotionEvent.ACTION_MOVE:
// A finger moves
System.out.println("ACTION_MOVE x: " + event.getX() + ", y: " + event.getY());
moveTouch(event.getX());
break;
default:
break;
}
// The event has been handled, so return true
return true;
}
};
这将按预期工作,除非用户在视图上向上或向下移动手指。此时,触摸事件停止发送(LogCat 中缺少输出即可证明)。但是,使用 adb shell getevent
仍然显示触摸移动生成的事件。
谁能建议我如何纠正这个问题?
根据要求,布局 XML 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff">
<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="10dip">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<com.graphlib.ShaderAreaGraphView
android:layout_width="fill_parent" android:layout_height="200dip"
android:id="@+id/activity_power_realtime" android:background="#000000" />
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:gravity="center">
<Button android:text="<<" android:id="@+id/goBackwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Zoom out" android:id="@+id/zoomOutButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Zoom in" android:id="@+id/zoomInButton" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
<Button android:text=">>" android:id="@+id/goForwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
</LinearLayout>
I have a custom view which I wish to handle touch events for. In particular, dragging a finger over the view should cause its contents to scroll. To do this, I have implemented the following onTouchListener:
private OnTouchListener graphOnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
System.out.println("onTouch called");
System.out.println("view width: " + v.getWidth());
System.out.println("view height: " + v.getHeight());
System.out.println("view: " + v);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// First finger down
System.out.println("ACTION_DOWN x: " + event.getX() + ", y: " + event.getY());
startTouch(event.getX());
break;
case MotionEvent.ACTION_UP:
// Last finger to be removed
System.out.println("ACTION_UP x: " + event.getX() + ", y: " + event.getY());
break;
case MotionEvent.ACTION_MOVE:
// A finger moves
System.out.println("ACTION_MOVE x: " + event.getX() + ", y: " + event.getY());
moveTouch(event.getX());
break;
default:
break;
}
// The event has been handled, so return true
return true;
}
};
This works as expected, unless the user moves their finger up or down while on the view. At this point, touch events stop being sent (as evidenced by a lack of output in LogCat). However, using adb shell getevent
still shows events being generated by touch movement.
Can anyone suggest how I can rectify this?
As requested, the layout XML looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff">
<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="10dip">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<com.graphlib.ShaderAreaGraphView
android:layout_width="fill_parent" android:layout_height="200dip"
android:id="@+id/activity_power_realtime" android:background="#000000" />
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:gravity="center">
<Button android:text="<<" android:id="@+id/goBackwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Zoom out" android:id="@+id/zoomOutButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Zoom in" android:id="@+id/zoomInButton" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
<Button android:text=">>" android:id="@+id/goForwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要在每种情况下编写break语句。否则每个案件都会被执行。
或者你可以放入 if ... else if 条件
you need write the break statement in each case. otherwise every case are executed.
or you can put into the if ... else if condition
事实证明,问题在于视图包含在 ScrollView 中。删除 ScrollView 可以解决该问题。
It turns out the issue is that the view is contained in a ScrollView. Removing the ScrollView fixes the problem.
解决此问题的一个简单方法是创建对滚动视图的引用,例如 myScrollView = (ScrollView) findViewById(R.id.myScrollView)。然后在onTouchEvent,ACTION_DOWN状态:
在 ACTION_UP 中
An easy way to solve this is to create a reference to the scroll view e.g. myScrollView = (ScrollView) findViewById(R.id.myScrollView). Then in onTouchEvent, ACTION_DOWN state:
And in ACTION_UP
在开关的底部,尝试添加
可能不是解决办法,但这样做是一个很好的做法。
At the bottom of your switch, try adding
Might not be the fix, but it's good practice to have that.