在 Android 中跨布局拖放

发布于 2024-12-05 08:27:43 字数 76 浏览 0 评论 0原文

我有一种线性布局(我将其称为子布局)和一种线性布局内的一种相对布局。我想将项目从相对布局拖放到子线性布局 我该怎么做?我无法在布局之间拖放

I have one linear layout(I'll call it child) and one relative layout inside one linear layout. I want to drag and drop items from the relative layout to the child linear layout
How can I do this? I am not able to drag and drop between layouts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黄昏下泛黄的笔记 2024-12-12 08:27:43

我正在努力拖延自己,但我可以提出建议。您需要忘记使用标准 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 {
     .
     .
     .
    }

克里斯

I'm working on dragging myself but I can make a suggestion. You need to forget about using the standard main.xml and do everything in code, using inner classes. It's not that hard and the inner classes can see the parent listeners.

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

Chris

旧竹 2024-12-12 08:27:43

在您的情况“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;
   }
 }
}

in your case 'DragEvent.ACTION_DROP' you should verify if your view is a LinerLayour or RelativLayout. you can verify this easily with 'instanceof'.

this could be the solution:

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;
   }
 }
}
岁月静好 2024-12-12 08:27:43

这可以帮助

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;
    }
}

this could help

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文