2011-09-20 113 views
2

我有一個線性佈局(我將它稱爲小孩)和一個線性佈局內的一個相對佈局。我想將項目從相對佈局拖放到子線性佈局 我該怎麼做?我無法在佈局之間拖放拖放在android中的佈局

+0

請手動編寫代碼 –

回答

0

我正在拖動自己,但我可以提出建議。您需要忘記使用標準main.xml,並使用內部類來完成代碼中的所有事情。這並不困難,內部類可以看到父級偵聽器。

private MotionEvent ev; 
TextView mainText; 
LinearLayout layout; 
KeyboardView keyboard; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    layout = new LinearLayout(this); 
    layout.setOrientation(LinearLayout.VERTICAL); 
    KeyboardView keyboard = new KeyboardView(this); 

    mainText = new TextView(this); 
    mainText.setText("The main text. "); 
    mainText.setHeight(120); 
    mainText.setBackgroundColor(Color.LTGRAY); 
    mainText.setTextColor(Color.BLACK); 
    layout.addView(mainText); 

    layout.addView(keyboard); 

    setContentView(layout); 

} 

    public class KeyboardView extends View { 
    . 
    . 
    . 
    } 

克里斯

你的情況 'DragEvent.ACTION_DROP' 你應該檢查,如果你的觀點是LinerLayour或RelativLayout
0

。你可以用'instanceof'輕鬆驗證。

,這可能是解決辦法:

public class MyDragListener implements OnDragListener { 
@Override 
public boolean onDrag(View v, DragEvent event) { 

    int action = event.getAction(); 
    switch (action) { 
    case DragEvent.ACTION_DRAG_STARTED: 
    break; 
    case DragEvent.ACTION_DRAG_ENTERED: 
    break; 
    case DragEvent.ACTION_DRAG_EXITED: 
    break; 
    case DragEvent.ACTION_DROP: 
    View view = (View) event.getLocalState(); 
    ViewGroup owner = (ViewGroup) view.getParent(); 
    owner.removeView(view); 
    if(v instanceof RelativLayout){ 
     RelativeLayout container = (RelativeLayout) v; 
    } else { 
     LinearLayout container = (LinearLayout) v; 
    } 
    container.addView(view); 
    view.setVisibility(View.VISIBLE); 
    break; 
    case DragEvent.ACTION_DRAG_ENDED: 
    break; 
    case DragEvent.ACTION_DRAG_LOCATION: 
    break; 
    } 
} 
} 
0

這將有助於

private final class MyTouchListener implements OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      ClipData data = ClipData.newPlainText("", ""); 
      View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
      view.startDrag(data, shadowBuilder, view, 0); 
      view.setVisibility(View.INVISIBLE); 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 

class MyDragListener implements OnDragListener { 
    Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget); 
    Drawable normalShape = getResources().getDrawable(R.drawable.bg); 

    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     int action = event.getAction(); 
     switch (event.getAction()) { 
      case DragEvent.ACTION_DRAG_STARTED: 
       // Do nothing 
       break; 
      case DragEvent.ACTION_DRAG_ENTERED: 
       v.setBackgroundDrawable(enterShape); 
       break; 
      case DragEvent.ACTION_DRAG_EXITED: 
       v.setBackgroundDrawable(normalShape); 
       break; 
      case DragEvent.ACTION_DROP: 
       // Dropped, reassign View to ViewGroup 
       View view = (View) event.getLocalState(); 
       ViewGroup owner = (ViewGroup) view.getParent(); 
       owner.removeView(view); 
       RelativeLayout container = (RelativeLayout) v; 
       container.addView(view); 
       view.setVisibility(View.VISIBLE); 
       break; 
      case DragEvent.ACTION_DRAG_ENDED: 
       v.setBackgroundDrawable(normalShape); 
      default: 
       break; 
     } 
     return true; 
    } 
}