2013-03-04 66 views
0
// Touched 
public class ChoiceTouchListener implements OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 

     switch (motionEvent.getAction()) { 

     case MotionEvent.ACTION_MOVE: 

      return true; 

     case MotionEvent.ACTION_DOWN: 
      Viewed = String.valueOf(view.getId()); 

      ClipData data = ClipData.newPlainText("", ""); 
      DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 

      // Drag Function When Item Touched 
      view.startDrag(data, shadowBuilder, view, 0); 
      return true; 

     case MotionEvent.ACTION_UP: 
      return true; 

     default: 
      return true; 
     } 
    } 
} 



private class ChoiceDragListener implements OnDragListener { 

    // onDrag Method, imagine we dragging numbers from display TextView 
    @Override 
    public boolean onDrag(View v, DragEvent event) { 

     // Android has onDrag action types. there are some kind of action 
     switch (event.getAction()) { 

      // if drag stared 
      case DragEvent.ACTION_DRAG_STARTED: 

      break; 

      // if drag entered software 
      case DragEvent.ACTION_DRAG_ENTERED: 

      break; 

      // if drag exited, stopped or something 
      case DragEvent.ACTION_DRAG_EXITED: 

      break; 

      // main case, if drag dropped what we do... 
      case DragEvent.ACTION_DROP: 

       // handle the dragged view being dropped over a drop view 
       View view = (View) event.getLocalState(); 

       // stop displaying the view where it was before it was dragged 
       // ************* view.setVisibility(View.KEEP_SCREEN_ON); 

       // view dragged item, where numbers will be dragged 
       dropTarget = (TextView) v; 

       // view dropped item, that will be dropped in drag TextView 
       dropped = (TextView) view; 

       // view result place, when make math operation, show results 
       resultPlace = (TextView) findViewById(R.id.calcResult); 


       // simple debug 
       // ****** resultPlace.setText(dropped.getText() + " " + dropTarget.getText()); 

       // if an item has already been dropped here, there will be a tag 
       Object tag = dropTarget.getTag(); 


       // if there is already an item here, set it back visible in its 
       // original place 
       if (tag != null) 
       { 
        // the tag is the view id already dropped here 
        int existingID = (Integer) tag; 

        // set the original view visible again 
        findViewById(existingID).setVisibility(View.VISIBLE); 


       } 

       // set the tag in the target view being dropped on - to the ID 
       // of the view being dropped 
       dropTarget.setTag(dropped.getId()); 

       // show results 
       resultPlace.setText(dropped.getText() + " " + dropTarget.getText() + " " + dropped.getText()); 



      break; 

      // if drag ended, we did it already successfully 
      case DragEvent.ACTION_DRAG_ENDED: 

      break; 

      // what do default 
      default: 

      break; 
     } 
     return true; 
    } 
} 

我有非常簡單的觸摸和拖動類。 我唯一的問題是,當我拖動項目並放入特定的佈局時,我的項目也保持舊位置。是否有任何方法在android中檢測我感動的項目並在刪除後刪除它?Android,刪除拖放項後

我有一個小應用程序,單擊數字並在儀表板上拖動鼠標,然後重複它並將所有項目拖放到X和Y位置的儀表板上。這就是爲什麼我想清空舊數字空間來創建另一個。

謝謝...

+0

https://stackoverflow.com/questions/48293198/how-to-drag-and-drop-on-dropped-postive-and-deelete-item-from-draageview-in-andr @coreprojectz如果你有解決方案可以請你在這個問題上建議我 – 2018-01-17 03:42:11

回答

0

您可以在觸摸監聽器中獲取視圖的父代。在下面的操作中,將父視圖保留在全局變量中,並在視圖放下時,使用removeView(View v)函數將其從父視圖中移除。

+0

https://stackoverflow.com/questions/48293198/how-to-drag-and-drop-on-dropped-postion-and-deelete-item-from-draageview-in-andr你能否檢查我在這個問題上長期堅持的問題 – 2018-01-17 03:41:44