Search code examples
javaandroidlistenergravity

How to detect if ondraglistener is already finish? (android)


I need to stop this service after a listener is already activated by using stopSelf(); However the service ends even though there is no Drag event... How can i detect if there is a Drag event happend before I can call 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;
    }
}

Solution

  • Stop your service in case DragEvent.ACTION_DRAG_ENDED:

    below is a sample template.

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

    According to 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.