android - 使用拖放来确定身份?

标签 android drag-and-drop

在我的 Android 应用程序中,我在单独的 LinearLayout 上有一个 ImageView 作为“生成”点。围绕“spawn”的是更多的 LinearLayout。

假设 ImageView 被称为“蓝色”,并且还有另外 2 个 LinearLayout。一个称为“blueArea”,另一个称为“redArea”。因此,如果蓝色被放入蓝色区域,就会开始一个 Action 。如果将蓝色放入红色区域,则会开始另一个操作。

我基本上希望我的掉落物知道正在掉落什么以及掉落在哪里。我可以做什么来“标记” ImageViews 和 LinearLayouts 以便我可以检测 View 何时位于布局上?我该怎么做?

这是我到目前为止所想到的。

// R.drawable.blue is the ImageView.
// R.id.blue is the LinearLayout.

public View blue = findViewById(R.drawable.blue);
findViewById(R.id.blue).setOnTouchListener(new TouchListener());

findViewById(R.id.blueArea).setOnDragListener(new DragListener());
findViewById(R.id.redArea).setOnDragListener(new DragListener());
findViewById(R.id.spawn).setOnDragListener(new DragListener());

ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder;
private final class TouchListener implements OnTouchListener {
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            shadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(data, shadowBuilder, v, 0);
            v.setVisibility(View.INVISIBLE);
            }
        return true;
        }
    }

boolean containsDraggable = true;
class DragListener implements OnDragListener {
public boolean onDrag(View v, DragEvent event) {
    int action = event.getAction();
    View view = (View) event.getLocalState();
    if (action == DragEvent.ACTION_DRAG_STARTED) {
        }
    if (action == DragEvent.ACTION_DRAG_ENTERED) {
        containsDraggable = true;
        }
    if (action == DragEvent.ACTION_DRAG_EXITED) {
        containsDraggable = false;
        }
    if (action == DragEvent.ACTION_DRAG_LOCATION) {
        // TODO
        }
    if (action == DragEvent.ACTION_DRAG_ENDED) {
        if (dropEventNotHandled(event)) {
            view.setVisibility(View.VISIBLE);
            }
        }
    if (action == DragEvent.ACTION_DROP && containsDraggable) {
        view.setVisibility(View.VISIBLE);
        releaseTrue();
        }
    return true;
    }
}
private boolean dropEventNotHandled(DragEvent event) {
    return !event.getResult();
    }
    public void releaseTrue() {
        score++;
        textScore.setText(String.valueOf(score));
    }
    public void releaseFalse() {
        score--;
        textScore.setText(String.valueOf(score));
    }

最佳答案

自从您添加

.setOnDragListener(new DragListener());

对于所有三个 View

public boolean onDrag(View v, DragEvent event) {

将使用上述三个 View (即 View v)调用

因此,如果您想表现不同,可以使用 switch 语句。

switch(v.getId()){
case R.id.blueArea:
//action for blue..
break
}

关于android - 使用拖放来确定身份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798328/

相关文章:

Android php 连接不工作

Android:设置震动强度

Android 可选 GPS 权限

wpf - 用鼠标在网格 wpf 中拖放控件

javascript - 无法使用 ExtDnd5 对节点进行排序,只能设置为子节点

Android - 在 firebase 数据库中杀死一个 child 会杀死我的应用程序

Cocoa NSTextField 拖放需要子类...真的吗?

c# - 在 C# 中拖放?

delphi - 拖动节点时 TTreeView 选择故障

android - 通过谷歌文档在 WebView 中查看 pdf 文件达到带宽限制