我有一个自定义视图,希望处理触摸事件。特别是,拖动手指在视图上应该导致其内容滚动。为此,我实现了以下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>
break;
以确保在他们抬起手指后不会触发 action_move,这样不是更好吗? - yep