简体   繁体   English

如何获取掉落对象的ID?

[英]How to get ID of dropped object?

I'm creating some ImageViews dynamically.我正在动态创建一些 ImageView。 I want to know which object is dropped.我想知道哪个对象被丢弃了。 Please take a look at the line:请看一下这条线:

Toast.makeText(getApplicationContext(), "You've passed the card", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "你已经通过了卡片", Toast.LENGTH_SHORT).show();

In this place I want to know what I've dropped (ID of ImageView would be great).在这个地方,我想知道我丢弃了什么(ImageView 的 ID 会很棒)。 How to get it?如何获得?

 private final class MyTouchListener implements View.OnTouchListener {

    // called when the item is long-clicked
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        // TODO Auto-generated method stub

        // create it from the object's tag
        ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());

        String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
        ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

        view.startDrag( data, //data to be dragged
                shadowBuilder, //drag shadow
                view, //local data about the drag and drop operation
                0   //no needed flags
        );


        view.setVisibility(View.INVISIBLE);
        return true;
    }
}

class MyDragListener implements View.OnDragListener {
    Drawable normalShape = getResources().getDrawable(R.drawable.table);
    Drawable targetShape = getResources().getDrawable(R.drawable.hands);

    @Override
    public boolean onDrag(View v, DragEvent event) {

        // Handles each of the expected events
        switch (event.getAction()) {

            //drag shadow has been released,the drag point is within the bounding box of the View
            case DragEvent.ACTION_DROP:
                // if the view is the bottomlinear, we accept the drag item
                if (event.getX() > 500) {
                    Toast.makeText(getApplicationContext(), "You've passed the card", Toast.LENGTH_SHORT).show();
                }
                View view = (View) event.getLocalState();
                ViewGroup viewgroup = (ViewGroup) view.getParent();
                viewgroup.removeView(view);

                RelativeLayout containView = (RelativeLayout) v;
                containView.addView(view);
                view.setVisibility(View.VISIBLE);
                break;

            default:
                break;
        }
        return true;
    }
}

From the documenation here: http://developer.android.com/reference/android/view/View.OnDragListener.html从这里的文档: http : //developer.android.com/reference/android/view/View.OnDragListener.html

public abstract boolean onDrag (View v, DragEvent event) public abstract boolean onDrag (View v, DragEvent 事件)

Added in API level 11 Called when a drag event is dispatched to a view.在 API 级别 11 中添加 在将拖动事件分派到视图时调用。 This allows listeners to get a chance to override base View behavior.这允许侦听器有机会覆盖基本视图行为。

Parameters参数

v : The View that received the drag event. v : 接收拖动事件的视图。
event : The DragEvent object for the drag event. event : 拖动事件的 DragEvent 对象。

Returns true if the drag event was handled successfully, or false if the drag event was not handled.如果拖动事件处理成功,则返回 true,如果拖动事件未处理,则返回 false。 Note that false will trigger the View to call its onDragEvent() handler.请注意, false 将触发 View 调用其 onDragEvent() 处理程序。

So when you do v.getId() , it is going to return the id of the view that the user started drag operation on.因此,当您执行v.getId() ,它将返回用户开始拖动操作的视图的 id。 If this happens to be the ImageView , it will return the id of it, or else the id of the view the user is dragging.如果这恰好是ImageView ,它将返回它的 id,否则返回用户正在拖动的视图的 id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM