拖动视图/ imageview时检索id

时间:2014-04-17 12:17:33

标签: android drag

当DRAG DROP并使用此类时,我想要检索视图的ID

public class ChoiceDragListener implements OnDragListener {

    boolean DEBUG = true;
    Context context;
    LayoutInflater mInflater;


    RelativeLayout rootContainer,   layout;
    MyAnimation animAccess;
    ChoiceDragListener(Context context){
        this.context = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        rootContainer = (RelativeLayout) mInflater.inflate(
                R.layout.activity_main, null);
        layout = (RelativeLayout) rootContainer.findViewById(R.id.rRIGHT);
    }

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            if(DEBUG) Log.v("here","drag started");
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            break;
        case DragEvent.ACTION_DRAG_LOCATION:
         int mCurX = (int) event.getX();
        int mCurY = (int) event.getY();

        if(DEBUG) Log.v("Cur(X, Y) : " ,"here ::" + mCurX + ", " + mCurY );

        break;
        case DragEvent.ACTION_DRAG_EXITED:        

            if(DEBUG)
            Log.v("here","drag exits");

            break;
        case DragEvent.ACTION_DROP:

            //handle the dragged view being dropped over a drop view
            ImageView view = (ImageView) event.getLocalState();
            ClipData cd =  event.getClipData();
            Item item = cd.getItemAt(0);
            String resp = item.coerceToText(context).toString();


            //stop displaying the view where it was before it was dragged
            view.setVisibility(View.INVISIBLE);

            //view dragged item is being dropped on
            ImageView dropTarget = (ImageView) v;

            //view being dragged and dropped
            ImageView dropped = (ImageView) view;

            dropped.setEnabled(false);
            R.id.iDROPA"+R.id.iDROPA+"view.getDrawable().toString()"+view.getDrawable().toString());
                            //update the text in the target view to reflect the data being dropped
        //  dropTarget.setBackgroundDrawable(view.getBackground());

            dropTarget.setBackgroundDrawable(view.getDrawable());
            if(((view.getDrawable().toString())=="t2_12_04")&&(dropTarget.getId()==R.id.iDROPAREA4))
            {

                CustomView12 c12 = new CustomView12(context);
                c12.RIGHT4.setVisibility(View.VISIBLE);
            }


            //if an item has already been dropped here, there will be a tag
            Object tag = dropTarget.getTag();

            //if there is already an item here, set it back visible in its original place
            if(tag!=null)
            {
                //the tag is the view id already dropped here
                int existingID = (Integer)tag;

                //set the original view visible again
                ((Activity) context).findViewById(existingID).setVisibility(View.VISIBLE);
            }

            //set the tag in the target view being dropped on - to the ID of the view being dropped
            dropTarget.setTag(dropped.getId());
            break;
        case DragEvent.ACTION_DRAG_ENDED:


                            return false;

        default:
            break;
        }
        return true;
    }
}

但无法检索视图ID USING(((view.getDrawable()。toString())==“tt_11_11”)),是他们的任何方式.thanx提前

1 个答案:

答案 0 :(得分:0)

如果您想获取图片视图的ID,可以使用

view.getId();

这将有助于您获取Imageview ID。

如果您需要获取ImageView的Drawable名称,您必须将图像名称设置为imageview的附加数据

android:tag="imagename.png"

您可以使用以下代码

获取drawable的名称
view.getTag();