2011-07-26 196 views
1

嗨所有m试图实现3.0.i拖放已经通过在开发人员的网站给出的例子,但m面临问题,而drop.Here是我的代码,能够拖动图像查看,但不能放弃它。任何一个PLZ告诉我什么是问题或提供任何示例代码?Drag and Drop in 3.0

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

感谢

回答

1

您还没有实现为DragEvent.ACTION_DROP

任何功能在这种情况下,你可以这样做:

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

u能PLZ告诉我,我应该怎么办???? – dharan

+0

更新了我的答案 –