我有一个拖放代码。如果用户触摸该项,请OnTouchListener
代码开始:
View.OnTouchListener dragListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// start move on a touch event
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
// API 24
// view.startDragAndDrop(data, shadowBuilder, view, View.DRAG_FLAG_GLOBAL); // API 24
view.setVisibility(View.VISIBLE);
return true;
}
return false;
}
};
但我的代码上会有OnLongClickListener
。如果用户保留(长按)该项目,则显示屏上会显示一个Toast消息:
homebutton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(UserArea.this, "laaaange geklickt", Toast.LENGTH_SHORT).show();
return true;
}
});
但它不起作用: - (
答案 0 :(得分:1)
从你的听众那里返回错误
View.OnTouchListener dragListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//your logic
return false;
}
};
否则您的活动将被视为已完成
答案 1 :(得分:1)
编辑您的触摸事件并根据阈值确定TOuch或长按
private float mDownX;
private float mDownY;
private final float SCROLL_THRESHOLD = 10;
private boolean isOnClick;
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mDownX = ev.getX();
mDownY = ev.getY();
isOnClick = true;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (isOnClick) {
Log.i(LOG_TAG, "onClick ");
//TODO onClick code
}
break;
case MotionEvent.ACTION_MOVE:
if (isOnClick && (Math.abs(mDownX - ev.getX()) > SCROLL_THRESHOLD || Math.abs(mDownY - ev.getY()) > SCROLL_THRESHOLD)) {
Log.i(LOG_TAG, "movement detected");
isOnClick = false;
}
break;
default:
break;
}
return true;
}