我尝试为我的onTouchListner
实现onLongClickListner
和ImageView
这样的内容:
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ShowMessageToast("Long Touch ");
return true ;
}
});
imageView.setOnTouchListener(new ImageView.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
ShowMessageToast("Down ");
break;
case MotionEvent.ACTION_MOVE:
ShowMessageToast("Moved ");
break;
default:
break;
}
return true;
}
但onLongClickListner
在我停用onTouchListner
(//评论)后才能生效。
如果onTouchListner
没有作为评论(// ....),它永远不会显示“长触摸”
ShowMessageToast("Moved ")
是一个在Toast中显示消息的函数。
答案 0 :(得分:1)
LongClickListener
和TouchListener
相互干扰,因为长按始终以触摸(ACTION_DOWN
)开头。您希望在TouchListener
中找到什么样的手势?
如果你想(例如)通过滚动检测长按,那么TouchListener和GestureListener将是处理它的方法。以下是如何执行此操作的示例:
findViewById(R.id.myImageView).setOnTouchListener(new OnTouchListener() {
private GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("TEST", "onScroll");
// TODO Auto-generated method stub
return super.onScroll(e1, e2, distanceX, distanceY);
}
/* (non-Javadoc)
* @see android.view.GestureDetector.SimpleOnGestureListener#onLongPress(android.view.MotionEvent)
*/
@Override
public void onLongPress(MotionEvent e) {
Log.d("TEST", "onLongPress");
// TODO Auto-generated method stub
super.onLongPress(e);
}
// ... implement here other callback methods like onFling, onScroll as necessary
});
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("TEST", "Raw event: " + event.getAction() + ", (" + event.getRawX() + ", " + event.getRawY() + ")");
return gestureDetector.onTouchEvent(event);
}
});
<强>说明强>
onLonGClickListener
。 TouchListener
只是将所有触摸事件传递给手势监听器。只要检测到其中一个手势,手势监听器就会收集手势并调用回调方法。