gpt4 book ai didi

java - 如何检测 ondraglistener 是否已经完成? (安卓)

转载 作者:行者123 更新时间:2023-11-30 03:18:44 30 4
gpt4 key购买 nike

我需要在使用 stopSelf() 激活监听器后停止此服务;然而,即使没有拖动事件,服务也会结束......在调用 StopService 之前,如何检测是否发生了拖动事件?

           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;
}
}

最佳答案

情况下停止服务 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

After the user releases the drag shadow, and after the system sends out (if necessary) a drag event with action type ACTION_DROP, the system sends out a drag event with action type ACTION_DRAG_ENDED to indicate that the drag operation is over.

关于java - 如何检测 ondraglistener 是否已经完成? (安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19557324/

30 4 0
文章推荐: java - 将 List 传递给 Play Framework 中的 Jquery
文章推荐: c++ - 需要推导参数*之前*用户参数的模板参数
文章推荐: java - 如何在 Iterator 中存储对象?