Question

In my app, there's 3 images dragged and dropped into 3 layouts.

I'm trying to show a Toast after the THREE of them have been dropped to their proper place.

At the moment, the Toast appears after Every single one of them

This is my code so far

@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();
}
Était-ce utile?

La solution

Simple way:

User a counter: at the top:

int numDragged = 0;


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

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

Autres conseils

in case DragEvent.ACTION_DROP:, Set a flag inside if conditions of top, middle and bottom pieces, after those conditions check if all three are true, if yes then display toast

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top