android - How can I drag and drop with multiple activities? -
i used service
call create floating window floats on other views/activities. window has own activity
, different dialog.
now want add drag&drop action window, example if long click imageview
in floating window, can drag , drop activity
(or base activity). i've been tried use onlongclicklistener
trigger drag event, , added ondraglistener
catch drop event. here's have far :
public class myfloatableactivity extends floatactivity { private imageview mimg; private mydrageventlistener mdraglisten; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.float_activity)); // imageview drag&drop test mimg = (imageview)findviewbyid(r.id.drag_img)); mimg.setonlongclicklistener(new imageview.onlongclicklistener() { @override public boolean onlongclick(view v) { clipdata dragdata = clipdata.newplaintext("dragtext", "dragtext"); v.startdrag(dragdata, new view.dragshadowbuilder(v), null, 0); return false; } }); mimg.setondraglistener(mdraglisten); switchtofloat(); // make activity float }
mydrageventlistener class :
public class mydrageventlistener implements view.ondraglistener { private clipdata mdragdata; @override public boolean ondrag(view v, dragevent event) { final int action = event.getaction(); imageview img; if(v instanceof imageview) { img = (imageview)v; } else{ return false; } switch(action) { case dragevent.action_drag_started: if(event.getclipdescription().hasmimetype(clipdescription.mimetype_text_plain)) { log.d("ddd", "drag started!!!"); return true; }else { return false; } case dragevent.action_drag_entered: log.d("ddd", "entered!!!"); case dragevent.action_drag_location: case dragevent.action_drag_exited: return true; case dragevent.action_drag_ended: case dragevent.action_drop: log.d("ddd", "action drop!!!"); return true; } return true; }
the reason implemented ondraglistener
listen action_drop
event in base activity when imageview
dropped. allows me determine whether imageview dropped on destination image, or layout. here's base activity :
public class draganddropdemo extends activity { private imageview mimg; private mydrageventlistener mdraglisten; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.drag_and_drop); //findviewbyid(r.id.drag_layout).setondraglistener(mdraglisten); mimg = (imageview)findviewbyid(r.id.dest_img); mimg.setondraglistener(mdraglisten); }
the problem ondraglistener
in draganddropdemo
not being invoked, couldn't catch drop event in base activity. i've seen many examples of drag , drop, never got right solution. i'm wondering if throwing drag&drop event different activity in android possible. if android it, be?
is there can help?
i found solution myself. integrated ondraglistener
myfloatableactivity
, , send intent draganddropdemo
activity receive intents whenever drop event occurs.
so here's code.
public class myfloatableactivity extends floatactivity { ... @override protected void oncreate(bundle savedinstancestate) { ... mimg.setondraglistener(new view.ondraglistener() { @override public boolean ondrag(view v, dragevent event) { switch (event.getaction()) { case dragevent.action_drag_started: if (event.getclipdescription().hasmimetype( clipdescription.mimetype_text_plain)) { return true; } else { return false; } case dragevent.action_drag_entered: case dragevent.action_drag_location: case dragevent.action_drag_exited: return true; case dragevent.action_drag_ended: case dragevent.action_drop: intent intent = new intent(); intent.setaction("com.test.draganddrop"); intent.putextra("drop", 0); sendbroadcast(intent); return true; } return false; } }); ... }
and in draganddropdemo,
public class draganddropdemo extends activity { ... @override protected void onresume() { super.onresume(); intentfilter filter = new intentfilter(); filter.addaction("com.test.draganddrop"); registerreceiver(mbr, filter); } @override protected void onpause() { super.onpause(); unregisterreceiver(mbr); } broadcastreceiver mbr = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { int flag = intent.getintextra("drop", 0); switch (flag) { case 0: mtext.settext("dropped!"); mimg.setimageresource(r.drawable.icon_argentina); break; } } }; }
Comments
Post a Comment