12

How can I listen the move events after the LongPress is caled in my GestureDetector?

When the user LongClick he starts the selection mode, and can drag a square into the screen. But I noticed that the onScroll is not called after LongPress is consumed.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • What about the `onFling` event from `GestureDetector` - is it invoked after lifting the finger up from long press & move? Alternatively, you might also try "raw" `onTouchEvent`... – Xion Apr 21 '11 at 08:01
  • are you consuming the LongPress? you can chose to accept the LongPress event, but then return false allowing anything above to handle the event. There is also the option of disabling LongPress in the View when you catch it. – Dr.J Apr 21 '11 at 11:11
  • @Dr.J unfortunatelly LongPress cannot be consumed. Method returns void, not boolean – Lukas Apr 22 '11 at 21:36
  • the Gesture Handler returns a boolean when it consumes onMove events with a high enough velocity to trigger a onFling event. – Dr.J Apr 22 '11 at 21:38

2 Answers2

21

Tried to do fight this for a while, and for now the solution is:

  1. Disable the longpress using setIsLongpressEnabled(isLongpressEnabled) on your gestureDetector

Here is my OnTouch method of my View:

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

My device returned ACTION_MOVE whenever I hold finger on the screen. If your doesnt, just check the state of some pressed flag after 0.5s using a timer or thread.

Hope that helps!

Lukas
  • 455
  • 4
  • 11
4

I have done this task by using the following concepts:

Suppose I have the Image View and on long press on it, image inside this image View would be drag-able and placed inside the another view(e.g. Relative Layout) Set MyClickListner on Image View's setOnLongClickListener() method.

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

Then set MyDragListner on Relative Layout(e.g. bigImageRelativeLayoutVw.setOnDragListener(new 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;
    }
}
Aman Goel
  • 3,351
  • 1
  • 21
  • 17