如何检测ondraglistener是否已经完成? (机器人)

时间:2013-10-24 05:01:47

标签: java android listener gravity

我需要在使用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;
    }
}

1 个答案:

答案 0 :(得分:1)

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的拖动事件,以指示拖动操作已结束。