我有一个自定义视图,我希望为其处理触摸事件。特别是,在视图上拖动手指应该使其内容滚动。要做到这一点,我已经实现了以下onTouchListener:Android停止发送onTouch事件
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>
请向我们展示布局xml。 –
如果我理解正确,action_up是他们删除他们的手指?你想在这之后想要“休息”吗?以确保在移除手指后action_move不会被解雇? – yep
是的,可能吧,但我对此没有太多兴趣。无论如何,我会编辑代码以阻止分心。 –