[英]event.getAction() never use MotionEvent.ACTION_UP
i have a problem with MotionEvent in the OnTouchListener. 我在OnTouchListener中遇到MotionEvent问题。 The
ACTION_DOWN
works perfectly, but it never use ACTION_UP I really don't know where is my problem, so if someone can help me with it, that would be really great, thank you. 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();
}
}
If you return false to onTouch
on Action_Down, you will not get a notification on Action_Up. 如果在
onTouch
上将onTouch返回false,则不会在Action_Up上收到通知。
Returning true on Action_Down tells Android that you want to continue receiving updates to this touch event. 在Action_Down上返回true会告诉Android您要继续接收此触摸事件的更新。 Returning false tells it that you don't want to receive further updates.
返回false表示您不想接收进一步的更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.