【问题标题】:Android drag and drop ImageView with no option to drag againAndroid拖放ImageView,没有再次拖动的选项
【发布时间】:2013-09-08 23:10:19
【问题描述】:

在我的应用程序中,我有 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;
    }
}

【问题讨论】:

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


    【解决方案1】:

    你可以添加

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多