2011-04-13 5 views
11

Comment puis-je écouter les événements de déplacement après le calibrage de LongPress dans mon GestureDetector?Déplacer des événements APRÈS LongPress

Lorsque l'utilisateur LongClick il démarre le mode de sélection, et peut faire glisser un carré dans l'écran. Mais j'ai remarqué que le onScroll n'est pas appelé après la consommation de LongPress.

+0

Qu'en est-événement '' onFling' de GestureDetector' - est-il invoqué après la levée du doigt de longue pression et se déplacer? Alternativement, vous pouvez également essayer "raw" 'onTouchEvent' ... – Xion

+0

consommez-vous le LongPress? vous pouvez choisir d'accepter l'événement LongPress, mais retourner false pour autoriser tout ce qui précède à gérer l'événement. Il est également possible de désactiver LongPress dans la vue lorsque vous l'attrapez. –

+0

@ Dr.J Malheureusement, LongPress ne peut pas être consommé. La méthode renvoie void, et non booléenne – Lukas

Répondre

17

essayé de le faire combattre pendant un certain temps, et pour l'instant la solution est:

  1. Désactiver le LongPress en utilisant setIsLongpressEnabled (isLongpressEnabled) sur votre gestureDetector

Voici ma méthode de OnTouch de mon Afficher:

public boolean onTouchEvent(MotionEvent event) { 
     if (mGestureDetector.onTouchEvent(event)== true) 
     { 
      //Fling or other gesture detected (not logpress because it is disabled) 
     } 
     else 
     { 
      //Manually handle the event. 
      if (event.getAction() == MotionEvent.ACTION_DOWN) 
      { 
       //Remember the time and press position 
       Log.e("test","Action down"); 
      } 
      if (event.getAction() == MotionEvent.ACTION_MOVE) 
      { 
       //Check if user is actually longpressing, not slow-moving 
       // if current position differs much then press positon then discard whole thing 
       // If position change is minimal then after 0.5s that is a longpress. You can now process your other gestures 
       Log.e("test","Action move"); 
      } 
      if (event.getAction() == MotionEvent.ACTION_UP) 
      { 
       //Get the time and position and check what that was :) 
       Log.e("test","Action down"); 
      } 

     } 
     return true; 
    } 

Mon périphérique a renvoyé ACTION_MOVE lorsque je maintiens le doigt sur l'écran. Si vous ne le faites pas, vérifiez simplement l'état d'un drapeau pressé après 0.5s en utilisant une minuterie ou un fil.

Espérons que ça aide!

3

je l'ai fait cette tâche en utilisant les concepts suivants:

Supposons que je la vue de l'image et appuyez longuement sur elle, l'image dans cette vue de l'image serait glisser-pouvoir et placé à l'intérieur une autre vue (par exemple Relative Layout) Définissez MyClickListner sur la méthode setOnLongClickListener() d'Image View.

private final class MyClickListener implements View.OnLongClickListener { 

    // called when the item is long-clicked 
    @Override 
    public boolean onLongClick(View view) { 
     // TODO Auto-generated method stub 

     // create it from the object's tag 
     ClipData.Item item = new ClipData.Item((CharSequence)view.getTag()); 

     String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN }; 
     ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item); 
     View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 

     view.startDrag(data, //data to be dragged 
       shadowBuilder, //drag shadow 
       view, //local data about the drag and drop operation 
       0 //no needed flags 
     ); 
     // view.setVisibility(View.INVISIBLE); 
     return true; 
    } 
} 

ensuite mis MyDragListner sur la mise en page relative (par exemple bigImageRelativeLayoutVw.setOnDragListener (nouveau MyDragListener());)

class MyDragListener implements View.OnDragListener { 

    @Override 
    public boolean onDrag(View v, DragEvent event) { 

     int X=(int)event.getX(); 
     int Y=(int)event.getY(); 
     int touchX = 0,touchY=0; 
     // Handles each of the expected events 
     switch (event.getAction()) { 

      //signal for the start of a drag and drop operation. 
      case DragEvent.ACTION_DRAG_STARTED: 
       // do nothing 
       break; 

      //the drag point has entered the bounding box of the View 
      case DragEvent.ACTION_DRAG_ENTERED: 


       break; 

      //the user has moved the drag shadow outside the bounding box of the View 
      case DragEvent.ACTION_DRAG_EXITED: 
       // v.setBackground(normalShape); //change the shape of the view back to normal 
       break; 

      //drag shadow has been released,the drag point is within the bounding box of the View 
      case DragEvent.ACTION_DROP: 
       // if the view is the bottomlinear, we accept the drag item 
       if(v == bigImageRelativeLayoutVw) { 
        View view = (View) event.getLocalState(); 


        touchX=X-viewCoords[0]-20; 
        touchY=Y-viewCoords[1]-20; 


        View view1=new View(getActivity()); 
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30,30); 

        layoutParams.leftMargin =touchX; 
        layoutParams.topMargin = touchY; 


        view1.setBackgroundResource(R.drawable.heavy_damage); 




        view1.setLayoutParams(layoutParams); 
        RelativeLayout containView = (RelativeLayout) v; 
        containView.addView(view1); 


        view.setVisibility(View.VISIBLE); 

       } else { 
        View view = (View) event.getLocalState(); 
        view.setVisibility(View.VISIBLE); 

        break; 
       } 
       break; 

      //the drag and drop operation has concluded. 
      case DragEvent.ACTION_DRAG_ENDED: 
       //  v.setBackground(normalShape); //go back to normal shape 

      default: 
       break; 
     } 
     return true; 
    } 
} 
Questions connexes