如何在DragListener完成后显示Toast

时间:2014-01-29 04:15:20

标签: android drag-and-drop toast

在我的应用中,有3个图像被拖放到3个布局中。

我试图在他们三个人被放到适当的地方后展示吐司。

目前,Toast出现在每一个

之后

这是我目前的代码

@Override
public boolean onDrag(View v, DragEvent e) {
    int action = e.getAction();
    View view = (View) e.getLocalState();

    switch (action) {
    case DragEvent.ACTION_DRAG_STARTED:
        return true;
    case DragEvent.ACTION_DRAG_ENTERED:
        return false;
    case DragEvent.ACTION_DRAG_EXITED:

        Toast.makeText(getActivity(), "Finished", Toast.LENGTH_SHORT).show();
        // return false;

            case DragEvent.ACTION_DROP:
        if (view.getId() == R.id.topPiece && v.getId() == R.id.topContainer) {

            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            view.setOnTouchListener(null);
            view.setOnDragListener(null);

        }

        if (view.getId() == R.id.middlePiece
                && v.getId() == R.id.middleContainer) {

            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            view.setOnTouchListener(null);
            view.setOnDragListener(null);

        }

        if (view.getId() == R.id.bottomPiece
                && v.getId() == R.id.bottomContainer) {

            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            view.setOnTouchListener(null);
            view.setOnDragListener(null);

        }

    case DragEvent.ACTION_DRAG_ENDED:
        if (dropEventNotHandled(e)) {
            view.setVisibility(View.VISIBLE);
        }
    }
    return false;
}
private boolean dropEventNotHandled(DragEvent e) {
    // TODO Auto-generated method stub
    return !e.getResult();
}

2 个答案:

答案 0 :(得分:1)

简单方法:

用户计数器: 在顶部:

int numDragged = 0;


//....
 //in your listener:
numDragg++;

if(numDragged>=3) {
  numDragged = 0;
  //show toast...
}

答案 1 :(得分:0)

如果是DragEvent.ACTION_DROP :,如果顶部,中间和底部条件的条件设置在里面,在这些条件之后检查是否所有三个都为真,如果是,则显示吐司