如何阻止一次拖动多个项目

如何阻止一次拖动多个项目

本文介绍了如何阻止一次拖动多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个ImageView的Activity,每个都有自己的OnDragListener和OnTouchListener.当我同时用两个手指单击显示时,同时单击2张图像时,该应用程序有时会崩溃.

I've got an Activity with several ImageViews, each with its own OnDragListener and OnTouchListener. When I'm clicking over the display with two fingers at once, and I click on 2 images at the same time, the app sometimes crashes.

当实际拖动一项时,如何阻止显示中的另一次拖动和触摸?我试图将setClicable(false)放入整个布局,但是没有用.

How could I block start of another drags and touches at display when one item is actually dragged? I've tried to put setClicable(false) to the whole layout, but it didn't work.

class MyDragListener implements OnDragListener {
        RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.game_layout);

        @Override
        public boolean onDrag(View dropView, DragEvent event) {
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    mainLayout.setClickable(false);
                    return true;

                case DragEvent.ACTION_DRAG_ENTERED:
                    // some code
                    return true;

                case DragEvent.ACTION_DROP:
                    // some code
                    return false;

                case DragEvent.ACTION_DRAG_ENDED:
                    mainLayout.setClickable(true);
                    // some code
                    return true;

                default:
                    break;
            }

            return true;
        }
}

编辑:我想到一个主意-单击某项时,从所有其他图像中删除OnTouchListeners.但这会给CPU的每次触摸带来额外的负担.有没有更好的办法?->>也无法正常工作

An idea came to my mind - when an item is clicked, remove OnTouchListeners from every other images. But it would be additional load for CPU on every touch. Isn't there a better way? -->> also NOT WORKING

EDIT2 :ACTION_DOWN-> inAction = true; 仍然无法正常工作.当我将图像拖到另一个图像上并进行第二次,第三次触摸时,它会a)丢下一张图片或更糟糕b)NullPointerException

ACTION_DOWN -> inAction = true; and it's still not working. When I drag an image over another one and make second, third... touch, it a) drops a picture or worse b) NullPointerException

private final class MyTouchListener implements OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {
            Log.d(TAG, "" + inAction);
            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                if(!inAction) {
                    inAction = true;
                    ClipData data = ClipData.newPlainText("", "");
                    DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                    view.startDrag(data, shadowBuilder, view, 0);
                    view.setVisibility(View.INVISIBLE);
                    return true;
                }
                else
                    return true;
            }
            else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                inAction = false;
                return true;
            }
            else {
                return false;
            }
        }

EDIT3 :我在整个应用程序中使用错误的 android:windowEnableSplitTouch 样式

I'm using style with false android:windowEnableSplitTouch for the whole application

<style name="MyTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
    <item name="android:windowEnableSplitTouch">false</item>
    <item name="android:splitMotionEvents">false</item>
</style>

推荐答案

您可以简单地返回;如果标志为真.即,在触发任何一个侦听器时设置一个标志.你没想过吗?

You can simply return; if a flag is true. Namely, set a flag when any of the listeners is triggered. Haven't you thought about it?

这篇关于如何阻止一次拖动多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:14