Android拖放ImageView,无法再次拖动

时间:2013-09-08 23:10:20

标签: android drag-and-drop imageview android-linearlayout

在我的应用程序中,我有10个线性布局,用作ImageView的放置区。 我想将ImageView(纸牌游戏)拖到它的放置区。 这部分工作正常。 问题是,我想允许用户只进行一次移动 - 然后取消DragListener,以便ImageView将卡在其放置区域。 有任何想法吗? 这里有点来自我的代码:

    private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
    SetDragListner();

        if (TouchTheStart != false) {
            if (CountTheTime < 20) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    CountCards();
                    if(ReachedDestination==false){
                    Random = getRandom();
                    getCard();
                    }
                    ReachedDestination=true;
                    ClipData data = ClipData.newPlainText("", "");
                    DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                            view);
                    view.startDrag(data, shadowBuilder, view, 0);
                    return true;
                } else {
                    return false;
                }

            }

        }
        return true;

    }
}

class MyDragListener implements OnDragListener {

    Drawable enterShape = getResources().getDrawable(
            R.drawable.shape_droptarget);
    Drawable normalShape = getResources().getDrawable(R.drawable.shape);

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

        int action = event.getAction();
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            // Do nothing
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            v.setBackground(enterShape);
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            v.setBackground(normalShape);
            break;
        case DragEvent.ACTION_DROP:
            // Dropped, reassign View to ViewGroup

            View view = (View) event.getLocalState();

            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            Cards.remove(Random);
            CountTheTime++;
            ReachedDestination=false;
            break;
        case DragEvent.ACTION_DRAG_ENDED:

            v.setBackground(normalShape);
        default:
            break;
        }
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以添加

Boolean allow=true;

并在onTouch中添加

public boolean onTouch(View view, MotionEvent motionEvent) {
    if(allow==true){
        //here you do everything and where you want to disallow the other onTouch add allow= false;
    }
    //add your return statement here
}