2014-01-29 79 views
0

在我的應用程序中,有3個圖像被拖放到3個佈局中。如何在「DragListener完成後顯示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(); 
} 

回答

1

簡單的方法:

用戶計數器: 頂部:

int numDragged = 0; 


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

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

萬一DragEvent.ACTION_DROP :,如果設置一個標誌如果條件頂部,中部和底部的部分,在這些條件後檢查是否所有三個都是真的,如果是的話顯示烤麪包