gpt4 book ai didi

java - Android - 使用 LongClickListener 而不是 OnTouch 进​​行拖动?

转载 作者:行者123 更新时间:2023-12-01 12:39:24 25 4
gpt4 key购买 nike

如何使用 LongClickListener 而不是 OnTouchListener 来拖动 ImageButton?我需要该按钮来执行 OnClick 操作,当我单击它时,它认为我正在开始拖动。

我尝试了一些 StackoverFlow 答案,但没有任何效果。

所以,这是我开始拖动的代码:

homeView.setOnDragListener(new IconDragListener());
ll.setOnTouchListener(new MyTouchListener());
...
class IconDragListener implements View.OnDragListener{

@Override
public boolean onDrag(View v, DragEvent event) {
GridLayout gridLayout = (GridLayout) v.findViewById(R.id.middleGrid);
final int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
//On drag entered

case DragEvent.ACTION_DROP:
if (event.getAction()==DragEvent.ACTION_DROP) {
View view = (View) event.getLocalState();
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
GridLayout to = (GridLayout) v;
to.addView(view);
view.setVisibility(View.VISIBLE);
//deleteAreaForAdapter.setVisibility(View.INVISIBLE);
deleteAreaForAdapter.setAnimation(slide_out);
}
return true;
case DragEvent.ACTION_DRAG_ENDED:
//do nothing
default:
break;
}
return true;
}

...

// Touch Listener, I'd like this to be OnLongClick
private final class MyTouchListener implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
deleteAreaForAdapter.setVisibility(View.VISIBLE);
deleteAreaForAdapter.startAnimation(slide_in);
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}

谢谢!

最佳答案

将代码放入ImageButtonsetOnLongClickListener

ImageButton.setOnLongClickListener(new OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
deleteAreaForAdapter.setVisibility(View.VISIBLE);
deleteAreaForAdapter.startAnimation(slide_in);
v.setVisibility(View.INVISIBLE);
return true;
}
});

关于java - Android - 使用 LongClickListener 而不是 OnTouch 进​​行拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25260848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com