在使用stopSelf()激活侦听器之后,我需要停止此服务;但是,即使没有Drag事件,该服务也会结束...在调用StopService之前,如何检测是否发生Drag事件?
mainButton.setOnTouchListener(new MyTouchListener());
vTopLeft.setOnDragListener(new MyDragListener());
vTopRight.setOnDragListener(new MyDragListener());
vBottomLeft.setOnDragListener(new MyDragListener());
vBottomRight.setOnDragListener(new MyDragListener());
Log.d("tok", "add mview");
stopSelf(); ---> this should wait after draglistener is finish.
MyDragListener.java
Public class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
String containerName = null;
int ID = 0;
int Dragaction = event.getAction();
int gravity2 = 1234567879;
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
owner.setVisibility(View.GONE);
FrameLayout container = (FrameLayout) v;
container.addView(view);
LocationSerializable ls = new LocationSerializable();
switch (v.getId()) {
case R.id.topLeft:
gravity2 = Gravity.LEFT | Gravity.TOP;
ls.setGravity(gravity2);
break;
case R.id.topRight:
gravity2 = Gravity.RIGHT | Gravity.TOP;
ls.setGravity(gravity2);
break;
case R.id.bottomLeft:
gravity2= Gravity.BOTTOM | Gravity.LEFT;
ls.setGravity(gravity2);
break;
case R.id.bottomRight:
gravity2 = Gravity.RIGHT | Gravity.BOTTOM;
ls.setGravity(gravity2);
break;
default:
throw new RuntimeException("Unknown ID");
}
LinearLayout containerParent = (LinearLayout) v.getParent();
LinearLayout conatainerGrandMother = (LinearLayout) containerParent.getParent();
conatainerGrandMother.setVisibility(View.GONE);
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return false;
}
}
最佳答案
停止case DragEvent.ACTION_DRAG_ENDED:
中的服务
下面是一个示例模板。
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
break;
case DragEvent.ACTION_DRAG_ENDED:
// option 1
Context.stopService();
// option 2
// stopService(new Intent(Activity.this,MailService.class));
//stopSelf();
// other alternative is
// v.getContext().stopService(new Intent(v.getContext(),DragDropButtonMainService.class));
break;
}
return true;
}
根据android docs
在用户释放拖动阴影之后,并且在系统发出(如果需要)动作类型为ACTION_DROP的拖动事件之后,系统会发出动作类型为ACTION_DRAG_ENDED的拖动事件以指示拖动操作已结束。
关于java - 如何检测ondraglistener是否已经完成? (安卓),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19557324/