长按后移动事件

发布于 2024-11-01 05:35:24 字数 127 浏览 0 评论 0原文

在 GestureDetector 中调用 LongPress 后,如何监听移动事件?

当用户长按时,他启动选择模式,并且可以将一个正方形拖到屏幕中。但我注意到,在使用 LongPress 后,不会调用 onScroll 。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸短情长 2024-11-08 05:35:24

尝试解决这个问题一段时间,现在的解决方案是:

  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 秒后检查某些按下标志的状态。

希望有帮助!

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!

岁月如刀 2024-11-08 05:35:24

我通过使用以下概念完成了此任务:

假设我有图像视图并长按它,该图像视图内的图像将是可拖动的并放置在另一个视图内(例如相对布局)
在 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(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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文