2013-04-09 3 views

답변

2

저는 매우 간단한 해결책을 가지고 있습니다 ... 다음 두 가지 기능을 추가하기 만하면됩니다. 귀하의 전체 코드가 DragEvent.ACTION_DROP에서 수행됩니다

/** 
* DragListener will handle dragged views being dropped on the drop area - 
* only the drop action will have processing added to it as we are not - 
* amending the default behavior for other parts of the drag process 
* 
*/ 
private class ChoiceDragListener implements OnDragListener { 

    private LinearLayout initiallinear; 

    public ChoiceDragListener(LinearLayout initialLinear) { 
     this.initiallinear = initialLinear; 

    } 

    @SuppressLint({ "NewApi", "NewApi", "NewApi" }) 
    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     switch (event.getAction()) { 
     case DragEvent.ACTION_DRAG_STARTED: 
      // no action necessary 
      break; 
     case DragEvent.ACTION_DRAG_ENTERED: 
      // no action necessary 
      break; 
     case DragEvent.ACTION_DRAG_EXITED: 
      // no action necessary 
      break; 
     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.INVISIBLE); 
      // view dragged item is being dropped on 
      LinearLayout dropTarget = (LinearLayout) v; 
      // view being dragged and dropped 
      Button dropped = (Button) view; 

      // update the text in the target view to reflect the data being 
      // dropped 
      int recentTag = (Integer) dropped.getTag(); 

      if (dropped.getParent() == dropTarget) { 
       dropTarget.removeView(dropped); 
       dropTarget.invalidate(); 
      } else { 

        initiallinear.removeView(dropped); 
        initiallinear.invalidate(); 


      } 
      try { 
       dropTarget.addView(dropped); 
       dropTarget.invalidate(); 
      } catch (Exception e) { 
       System.out.println(e); 
      } 

      dropped.setVisibility(View.VISIBLE); 
      dropTarget.invalidate(); 
      // 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()); 
      */ 
      break; 
     case DragEvent.ACTION_DRAG_ENDED: 
      // no action necessary 
      break; 
     default: 
      break; 
     } 
     return true; 
    } 
} 

ChoiceTouchListner()

/** 
* ChoiceTouchListener will handle touch events on draggable views 
* 
*/ 
@SuppressLint({ "NewApi", "NewApi" }) 
private final class ChoiceTouchListener implements OnTouchListener { 
    @SuppressLint("NewApi") 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      /* 
      * Drag details: we only need default behavior - clip data could 
      * be set to pass data as part of drag - shadow can be tailored 
      */ 
      ClipData data = ClipData.newPlainText("", ""); 
      DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
        view); 
      // start dragging the item touched 
      view.startDrag(data, shadowBuilder, view, 0); 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 

ChoiceDragListener(). 여기에서 dropTarget은 버튼/뷰를 드롭하는 Linearlayout입니다. initialLayer는 버튼/뷰를 선택하는 LinearLayout입니다.

LinearLayout을 그리드 레이아웃으로 변경할 수 있습니다.

망가

initialLinear.setOnDragListener(new ChoiceDragListener(linear,initialLinearSecond)); 
dropTarget.setOnDragListener(new ChoiceDragListener(initialLinear,initialLinearSecond)); 
+0

ClipData을 설정하는 것을 잊지 내가 드래그에 대한 전체 코드를 게시하고 하나의 gridview에서 아이템을 드롭 API 8. 작동하지 않습니다이 API를 8 – user2031501

+0

이 작동한다 필요한 API 11에 추가 다른 gridview – user2031501

+1

일부 인기있는 온라인 자습서에서 복사 및 붙여 넣기 보이는 있지만이 코드와 함께 몇 가지 문제가 있습니다. –

관련 문제