dharan
dharan

Reputation: 753

Drag and Drop in 3.0

Hii all m trying to implement Drag and Drop in 3.0.i have gone through the example given in developers site,but m facing a problem while dropping.Here is my code,m able to drag the image view,but not able to drop it.could any one plz tell me what is the problem or provide any sample code??

    setContentView(R.layout.main);
    iv=(ImageView)findViewById(R.id.iv);
    lv=(RelativeLayout)findViewById(R.id.lv);
    lv.setOnDragListener(this);
    iv.setTag("HELLOOO");
    iv.setOnLongClickListener(this);   
}
      public boolean onDrag(View v, DragEvent event) {
    CharSequence dragData;
    switch(event.getAction()){
    case DragEvent.ACTION_DRAG_STARTED:
        break;
    case DragEvent.ACTION_DRAG_ENTERED:
        insideOfMe = true;
        break;
    case DragEvent.ACTION_DRAG_LOCATION:
        break;
    case DragEvent.ACTION_DRAG_ENDED:
        break;
    case DragEvent.ACTION_DRAG_EXITED:
        insideOfMe = false;
        break;
    case DragEvent.ACTION_DROP:
        break;
    }

    return true;
}
public static class Shadow extends View.DragShadowBuilder{
    Drawable d;
    public Shadow(View v,Context context){
        super(v);
    d=context.getResources().getDrawable(R.drawable.icon);
    }

    @Override
    public void onProvideShadowMetrics(Point shadowSize,
            Point shadowTouchPoint) {
        // TODO Auto-generated method stub
                    int width,height;
                   width=getView().getWidth();
                    height=getView().getHeight();
                   d.setBounds(0,0,width,height);
                 shadowSize.set(width, height);
                 shadowTouchPoint.set(width/2, height/2);   
    }
    public void onDrawShadow(Canvas canvas){
canvas.save();
d.draw(canvas);
canvas.restore();
    }
    }  
public boolean onLongClick(View v) {
     ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
        ClipDescription NOTE_STREAM_TYPES = new ClipDescription( (CharSequence)                                           

     v.getTag(),new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN });
 ClipData data = new ClipData(NOTE_STREAM_TYPES, item);

     shadow=new Shadow(v,getApplicationContext());

     v.startDrag(data, shadow,null,0);

    return false;
}

Thanks

Upvotes: 1

Views: 2212

Answers (1)

Che Jami
Che Jami

Reputation: 5151

You have not implemented any functionality for DragEvent.ACTION_DROP

In this case you can do:

    case DragEvent.ACTION_DROP:
        if (insideOfMe) {
            Item item = event.getClipData().getItemAt(0);
            // Do whatever you want to do with the item
        }

Upvotes: 1

Related Questions