2011-04-13 25 views
11

如何在LongPress在我的GestureDetector中进行标记之后监听移动事件?在长按之后移动事件

当用户LongClick他启动选择模式,并且可以将一个方块拖入屏幕。但是我注意到在使用LongPress之后不会调用onScroll。

+0

怎么样从'GestureDetector'了'onFling'事件 - 它是解除后调用从长按和移动手指?或者,您也可以尝试“原始”onTouchEvent ... ... – Xion 2011-04-21 08:01:14

+0

您是否正在使用LongPress?您可以选择接受LongPress事件,但返回false则允许以上任何事件处理事件。 当您捕捉到时,还可以在视图中禁用LongPress。 – 2011-04-21 11:11:12

+0

@ Dr.J unfortunatelly LongPress不能被消费。方法返回void,而不是布尔值 – Lukas 2011-04-22 21:36:26

回答

17

试图做斗争这一段时间,而对于现在的解决方案是:

  1. 您gestureDetector

这里禁止使用setIsLongpressEnabled(isLongpressEnabled)的长按是我OnTouch方法我查看:

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

我的设备每当我将手指放在屏幕上时都返回ACTION_MOVE。如果你没有,只需使用定时器或线程在0.5秒后检查一些按下的标志的状态。

希望有帮助!

3

我以下列概念完成这个任务:

假设我有这个图片View内的图像查看和长按就可以了,图像会拖能并放置在另一个视图中(例如,相对布局) 在Image View的setOnLongClickListener()方法上设置MyClickListner。

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

然后设置MyDragListner上相对布局(如bigImageRelativeLayoutVw.setOnDragListener(新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; 
    } 
}