2013-03-10 55 views
0

我跟着this tutorial的拖放功能,它的工作原理除了一个奇怪的错误。Android的拖放TextView的bug

拖动的TextView阿到的TextView乙是假设:

  1. 隐藏的TextView甲
  2. 传递一个标签从TextView的A到TextView的乙
  3. 重新显示的TextView B中的传递数据

将文本视图A中的数据传递给B可以100%的工作,但奇怪的是,textview A在100%的时间内不会隐藏。我不知道它是如何将它拖到textview B或其他东西上。任何想法如何让这更稳定?

public boolean onDrag(View v, DragEvent event) { 
     //handle drag events 
     switch (event.getAction()) { 
      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 
      TextView dropTarget = (TextView) v; 
      //view being dragged and dropped 
      TextView dropped = (TextView) view; 
      //update the text in the target view to reflect the data being dropped 
      dropTarget.setText(dropped.getText()); 
      //make it bold to highlight the fact that an item has been dropped 
      dropTarget.setTypeface(Typeface.DEFAULT_BOLD); 
      //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 to the ID of the view being dropped 
      dropTarget.setTag(dropped.getId()); 
      // check choice 
      checkChoice(dropped,dropTarget); 
      break; 

回答

0

想我找到了问题

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); COMMENT THIS OUT 
     }