如何获取被删除对象的ID?

时间:2015-10-24 17:51:16

标签: android

我正在动态创建一些ImageView。我想知道丢弃了哪个对象。 请看一下这条线:

  

Toast.makeText(getApplicationContext(),"您已通过卡",Toast.LENGTH_SHORT).show();

在这个地方,我想知道我放弃了什么(ImageView的ID会很棒)。 如何获得它?

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

1 个答案:

答案 0 :(得分:-1)

来自此处的文件:http://developer.android.com/reference/android/view/View.OnDragListener.html

  

public abstract boolean onDrag(View v,DragEvent event)

     

在API级别11中添加在将拖动事件分派给a时调用   视图。这允许侦听器有机会覆盖基本视图   行为。

     

参数

     

v :收到拖动事件的视图。
  事件:拖动事件的DragEvent对象。

     

如果成功处理了拖动事件,则返回true;如果未处理拖动事件,则返回false。   请注意,false将触发View调用其onDragEvent()   处理程序。

因此当你执行v.getId()时,它将返回用户开始拖动操作的视图的id。如果这恰好是ImageView,它将返回它的id,或者是用户拖动的视图的id。