我有一个onClickListener和一个onFling (使用GestureDetector)。
如果我单击该按钮,onClickListener就会触发。
如果我扔在屏幕的主体上,那么onFling就会开火。
然而,如果我从按钮开始放火,那么两种情况都不会发生。
布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/myButton"
android:layout_margin="50dp"
android:layout_height="100dp"
android:layout_width="match_parent"
android:text="Button"/>
</LinearLayout>代码看起来是:
public class myLayout extends Activity implements GestureDetector.OnGestureListener {
private GestureDetector gDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
gDetector = new GestureDetector(getBaseContext(), this);
findViewById(R.id.myButton).setOnClickListener(button_OnClickListener);
}
final View.OnClickListener button_OnClickListener = new View.OnClickListener() {
public void onClick(final View buttonView) {
Toast.makeText(getApplicationContext(), "button pressed", Toast.LENGTH_LONG).show();
}
};
@Override
public boolean onDown(MotionEvent motionEvent) {
return true;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent start, MotionEvent finish, float v, float v2) {
if (start.getRawY() < finish.getRawY()) {
Toast.makeText(getApplicationContext(), "Fling detected", Toast.LENGTH_LONG).show();
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent me) {
Log.i("Touch", "onTouchEvent");
return gDetector.onTouchEvent(me);
}
}如何让onFling 首先运行?
我看过其他帖子(比如onClick blocking onFling),但没有找到合适的答案。
发布于 2013-08-08 18:04:55
您应该尝试使用onInterceptTouchEvent() (或者,可能是dispatchTouchEvent(),在这一点上,我不确定哪一个更适合Button的父级)来决定是否将触摸路由到按钮或手势检测器。
如果MotionEvent的坐标属于Button的rect,而ACTION_DOWN后面跟着ACTION_UP,那就是点击,应该被路由到Button,但是如果你接收到ACTION_MOVE,移动一直持续到你知道是滑动的程度--它应该通过手势检测器来处理。
UPD这里有一个解释https://stackoverflow.com/a/3834952/375929技术的答案
https://stackoverflow.com/questions/18130185
复制相似问题