在android中拖放布局

时间:2011-09-20 06:32:08

标签: android drag-and-drop

我有一个线性布局(我称之为子)和一个线性布局内的一个相对布局。我想将项目从相对布局拖放到子线性布局 我怎样才能做到这一点?我无法在布局之间拖放

3 个答案:

答案 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 {
     .
     .
     .
    }

克里斯

答案 1 :(得分:0)

在您的案例'DragEvent.ACTION_DROP'中,您应该验证您的视图是LinerLayour还是RelativLayout。您可以使用'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;
   }
 }
}

答案 2 :(得分: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;
    }
}