如何使用LongClickListener而不是OnTouchListener来拖动我的ImageButton?我需要按钮来执行Action 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;
}
}
}
谢谢!
答案 0 :(得分:1)
将代码放入setOnLongClickListener
ImageButton
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;
}
});
答案 1 :(得分:0)
试试这个
public class ChoiceLongClickListener implements View.OnLongClickListener {
Context context;
View dragView;
public ChoiceLongClickListener(Context context, View dragView) {
this.context = context;
this.dragView = dragView;
}
@Override
public boolean onLongClick(View view) {
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = ClipData.newPlainText("", "");
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(dragView);
view.startDrag(dragData, myShadow, null, 0);
return true;
}}