gpt4 book ai didi

android - 3.0 中的拖放

转载 作者:行者123 更新时间:2023-11-30 04:36:04 25 4
gpt4 key购买 nike

大家好,我都在尝试在 3.0 中实现拖放。我已经完成了开发人员网站中给出的示例,但是我在拖放时遇到了问题。这是我的代码,我能够拖动 ImageView ,但不能放弃它。任何人都可以告诉我问题是什么或提供任何示例代码吗??

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

谢谢

最佳答案

您还没有为 DragEvent.ACTION_DROP 实现任何功能

在这种情况下你可以这样做:

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

关于android - 3.0 中的拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6828252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com