我在OnTouchListener中遇到MotionEvent问题。 ACTION_DOWN
完美无缺,但它永远不会使用ACTION_UP
我真的不知道我的问题在哪里,所以如果有人可以帮助我,那真的很棒,谢谢。
public class MainActivity extends Activity {
View.OnTouchListener gestureListener;
private static final int MIN_DISTANCE = 50;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.setOnTouchListener(new View.OnTouchListener() {
float downX = 0, downY;
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN :
downX = event.getX();
downY = event.getY();
Toast.makeText(getApplicationContext(), "action down", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP :
Toast.makeText(getApplicationContext(), "action up", Toast.LENGTH_SHORT).show();
float deltaX = downX - event.getX();
Toast.makeText(getApplicationContext(), Math.abs(deltaX)+"k", Toast.LENGTH_SHORT).show();
if (Math.abs(deltaX) > 3){
if(deltaX < 0) { onLeftToRightSwipe(); return true; }
if(deltaX > 0) { onRightToLeftSwipe(); return true; }
}
else {
String str = "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE;
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
return false;
}
break;
}
return false;
}
});
}
private void onRightToLeftSwipe() {
Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
}
private void onLeftToRightSwipe() {
Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:10)
如果在Action_Down上向onTouch
返回false,则不会收到有关Action_Up的通知。
在Action_Down上返回true告诉Android您要继续接收此触摸事件的更新。返回false告诉它您不希望再接收更新。